cmake语法学习 - 01 Basic - E- installing
cmake_minimum_required(VERSION 3.5) project(cmake_examples_install) ############################################################ # Create a library ############################################################ #Generate the shared library from the library sources add_library(cmake_examples_inst SHARED src/Hello.cpp ) target_include_directories(cmake_examples_inst PUBLIC ${PROJECT_SOURCE_DIR}/include ) ############################################################ # Create an executable ############################################################ # Add an executable with the above sources add_executable(cmake_examples_inst_bin src/main.cpp ) # link the new hello_library target with the hello_binary target target_link_libraries( cmake_examples_inst_bin PRIVATE cmake_examples_inst ) ############################################################ # Install ############################################################ message("CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}") # Binaries install (TARGETS cmake_examples_inst_bin DESTINATION bin) # Library # Note: may not work on windows install (TARGETS cmake_examples_inst LIBRARY DESTINATION lib) # Header files install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include) # Config install (FILES cmake-examples.conf DESTINATION etc)
Before diving into the code, please make sure you really uderstand what is intalling and why to do that.
Install a libiary (like OpenCV) to system ( some specific paths ), and after that, you can import them to you project by "find_packge()" in CMake.
*
install (TARGETS cmake_examples_inst_bin
DESTINATION bin)
-TARGETS is followed by the file to stall
-DESTINATION is followed by the folder.
That means, the final output path will be "${CMAKE_BINARY_DIR}/bin"
For beginners, the most confused thing will be where is the path to install, when did you set that, and where in the CMakeList.txt.
The answer is, there are some default paths in CMake marked by some kept variables.
So the path settings are quite ambigous. (At least it is my opinion.)
For a better understanding, I recommend you to message() some path-variables in CMake and remember them. We have limited options in this points. Please check the website of CMake.
*
target_include_directories(cmake_examples_inst
PUBLIC
${PROJECT_SOURCE_DIR}/include
)
- After setting the include paths, you can open the head files #include in you file. But of course, you need to have an IDE to support importing project from CMkeList.txt, like CLion. But it is no free which only offers me 30 days to use.
Waitting for recommendations :)