来源网址:http://www.cnblogs.com/binbinjx/p/5626916.html
罗列一下cmake常用的命令。
CMake支持大写、小写、混合大小写的命令。
语法:
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 …])
它相当于g++选项中的-I参数的作用,也相当于环境变量中增加路径到CPLUS_INCLUDE_PATH变量的作用。
include_directories(../../../thirdparty/comm/include)
语法:
link_directories(directory1 directory2 …)
它相当于g++命令的-L选项的作用,也相当于环境变量中增加LD_LIBRARY_PATH的路径的作用。
link_directories(“/home/server/third/lib”)
语法:
复制代码
复制代码
A short-hand signature is:
find_library ( name1 [path1 path2 …])
The general signature is:
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(library1
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( [item1 [item2 […]]]
[[debug|optimized|general] ] …)
复制代码
复制代码
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)
复制代码
复制代码
add_executable(demo
main.cpp
)
INCLUDE_DIRECTORIES(../../thirdparty/comm)
FIND_LIBRARY(COMM_LIB comm ../../thirdparty/comm/lib NO_DEFAULT_PATH)
FIND_LIBRARY(RUNTIME_LIB rt /usr/lib /usr/local/lib NO_DEFAULT_PATH)
link_libraries( COMMLIB {RUNTIME_LIB})
ADD_DEFINITIONS(
-O3 -g -W -Wall
-Wunused-variable -Wunused-parameter -Wunused-function -Wunused
-Wno-deprecated -Woverloaded-virtual -Wwrite-strings
-D__WUR= -D_REENTRANT -D_FILE_OFFSET_BITS=64 -DTIXML_USE_STL
)
add_library(lib_demo
cmd.cpp
global.cpp
md5.cpp
)
link_libraries(lib_demo)
add_executable(demo
main.cpp
)
target_link_libraries(demo libuuid.a)
复制代码
复制代码