设定输出目录
变量 | 内容 | 备注 |
---|---|---|
EXECUTABLE_OUTPUT_PATH | 可执行文件输出目录 | set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) |
CMAKE_ARCHIVE_OUTPUT_DIRECTORY | 设置静态库输出目录 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
CMAKE_LIBRARY_OUTPUT_DIRECTORY | 编译时lib库输出目录 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
CMAKE_RUNTIME_OUTPUT_DIRECTORY | 动态库输出目录 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
常用变量
变量 | 内容 | 备注 |
---|---|---|
PROJECT_BINARY_DIR | 编译输出目录 | 如${PROJECT_SOURCE_DIR}/build |
PROJECT_SOURCE_DIR | 代码目录 | 如果直接在代码目录下cmake,那么此变量与PROJECT_BINARY_DIR内容相同 |
CMAKE_INCLUDE_PATH | 包含头文件目录 | 环境变量,非cmake变量 |
CMAKE_LIBRARY_PATH | 链接库目录 | 环境变量 |
CMAKE_CURRENT_SOURCE_DIR | 当前Cmake代码目录 | 当前处理的CMakeLists.txt所在的路径 |
CMAKE_CURRENT_BINARY_DIR | target编译目录 | |
CMAKE_CURRENT_LIST_FILE | 输出调用这个变量的CMakeLists.txt的完整路径 | |
CMAKE_CURRENT_LIST_LINE | 输出这个变量所在的行 | |
CMAKE_MODULE_PATH | 定义自己的cmake模块所在的路径 | |
CMAKE_MODULE_PATH | cmake查找cmake模块文件的目录 | find_package(…)时,在此变量的目录内查找 |
PROJECT_NAME | 返回通过PROJECT指令定义的项目名称 | |
CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS | 用来控制IF ELSE语句的书写方式 |
常用函数
INCLUDE_DIRECTORIES
包含头文件目录
语法:
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 …])
它相当于g++选项中的-I参数的作用,也相当于环境变量中增加路径到CPLUS_INCLUDE_PATH变量的作用。
include_directories(“dir1” “dir2”)
LINK_DIRECTORIES
语法:
link_directories(directory1 directory2 ...)
它相当于g++命令的-L选项的作用,也相当于环境变量中增加LD_LIBRARY_PATH的路径的作用。
link_directories(“dir1” “dir2”)
FIND_LIBRARY
语法:
find_library ( name1 [path1 path2 ...])
find_library (
name | NAMES name1 [name2 ...] [NAMES_PER_DIR]
[HINTS path1 [path2 ... ENV var]]
[PATHS path1 [path2 ... ENV var]]
[PATH_SUFFIXES suffix1 [suffix2 ...]]
[DOC "cache documentation string"]
[NO_DEFAULT_PATH]
[NO_CMAKE_ENVIRONMENT_PATH]
[NO_CMAKE_PATH]
[NO_SYSTEM_ENVIRONMENT_PATH]
[NO_CMAKE_SYSTEM_PATH]
[CMAKE_FIND_ROOT_PATH_BOTH |
ONLY_CMAKE_FIND_ROOT_PATH |
NO_CMAKE_FIND_ROOT_PATH]
)
例子如下:
FIND_LIBRARY(RUNTIME_LIB rt /usr/lib /usr/local/lib NO_DEFAULT_PATH)
cmake会在目录中查找,如果所有目录中都没有,值RUNTIME_LIB就会被赋为NO_DEFAULT_PATH
LINK_LIBRARIES
语法:
link_libraries(library1 <debug | optimized> library2 ...) # 必须填库的全名
例子:
link_libraries(“/home/server/third/lib/libcommon.a”)
link_libraries(iconv)
link_libraries(${RUNTIME_LIB})
link_libraries("/opt/MATLAB/R2012a/bin/glnxa64/libeng.so" "/opt/MATLAB/R2012a/bin/glnxa64/libmx.so")
TARGET_LINK_LIBRARIES
语法:
target_link_libraries(<target> [item1 [item2 [...]]]
[[debug|optimized|general] <item>] ...)
例子:
# 以下写法都可以:
target_link_libraries(myProject comm) # 连接libhello.so库,默认优先链接动态库
target_link_libraries(myProject libcomm.a) # 显示指定链接静态库
target_link_libraries(myProject libcomm.so) # 显示指定链接动态库
# 再如:
target_link_libraries(myProject libcomm.so) #这些库名写法都可以。
target_link_libraries(myProject comm)
target_link_libraries(myProject -lcomm)