Some Misunderstandings
- the
STATUS keyword need to be capitalized within message
- Normally in cmake, we need to use ${var} to get the value of var, but in if statement, the bare var shall be used directly.
- For instructions, it doesn't matter whether the capitalized or the normal letter is used, but it does matter in var names and parameters.
New knowledge
${projectname_BINARY_DIR} ${projectname_SOURCE_DIR}stores the path of the build dir and source dir respectively
-
Project
project(project_name [CXX] [C] [Java])
- note project doesn't need to be capitalized but
[CXX] [C] [Java] have to be exactly the same since these are the hard coded environment names
- this instruction implicitly defined two path macro, the path of the source folder and the path of the binary folder
PROJECT_BINARY_DIR PROJECT_SOURCE_DIR are also available after that instruction
-
set
set(Var [value])
- doesn't need to be capitalized
-
message
MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] "message to display")
- so you now you know why we need "STATUS" to be capitalized
-
add_subdirectory
ADD_SUBDIRECTORY(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
- the binary folder will be created within in current build directory. Otherwise the binary file will be stored in build/source_dir
-
Manually set the output path of the binary files
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
- Pay attention to the macros' name
-
install : to put files wherever you want :)
install([TARGETS | FILES | PROGRAMS | DIRECTORY] files DESTINATION dir )
- note target is the complied binary files, files are normal files, programs are scripts. And for all those file, you can set the permission after installation. Check the man page
- For directory, when typing the path, end with or without a black slash makes a difference, check the manual.(with the slash means install the content, and without the slash means install the entire directory)
- note the default value of
CMAKE_INSTALL_PREFIX is /usr/local
-
add_library()
add_library(libname [SHARED | STATIC | MODULE] sources)
-
set_target_properties
set_target_properties(target1 target2 .. PROPERTIES prop1 value1 prop2 value2 ...)
- we can use it to change the output file name:
SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello")
- And on contrast, we have a function to get the target property
GET_TARGET_PROPERTY(VAR target property)
-
include_directories()
- this function helps to include the header files path
INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...)
- the before and after means where do you want to put the new path, before the default path or after the default path