一、Very simple executable
PROJECT( helloworld ) # 非必需
SET( hello_SRCS hello.cpp )
ADD_EXECUTABLE( hello ${hello_SRCS} )
1
2
3
说明:
ADD_EXECUTABLE creates an executable from the listed sources
Tip: add sources to a list (hello_SRCS), do not list them in ADD_EXECUTABLEc++
二、Very simple library
PROJECT( mylibrary ) #非必需
SET( mylib_SRCS library.cpp )
ADD_LIBRARY( my SHARED ${mylib_SRCS} )
1
2
3
说明:
ADD_LIBRARY creates an static library from the listed sources
Add SHARED to generate shared libraries (Unix) or dynamic libraries (Windows)
不加SHARED 生成.a 静态库,加了SHARED ,生成.so 动态连接库oop
三、Process a list
FOREACH(loop_var)
...
ENDFOREACH(loop_var)
1
2
3
四、3rd party or .h
If the 3rd party library or .h is in a “standard”:
directory (PATH and/or LD_LIBRARY_PATH) is automaticui
If in a nonstandard dir:
Headers: use INCLUDE_DIRECTORIES
Libraries: use FIND_LIBRARY and link with the result of it.net
cmake_minimum_required(VERSION 2.8)
project (faceIdentification)
file(GLOB_RECURSE srcs ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)c++11
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -std=c++11 -O2")component
find_package(Caffe)
include_directories(
${PROJECT_SOURCE_DIR}/include
${Caffe_INCLUDE_DIRS})
add_definitions(${Caffe_DEFINITIONS})blog
foreach(source ${srcs})
get_filename_component(name ${source} NAME_WE)ip
add_executable(${name} ${source})
target_link_libraries(${name}
${PROJECT_SOURCE_DIR}/lib/libviplnet.so
${Caffe_LIBRARIES})
endforeach(source)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
附录
一、几种经常使用的库的CMakeLists.txt
boost
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS *boost libraries here*) get
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(progname file1.cxx file2.cxx)
target_link_libraries(progname ${Boost_LIBRARIES})
endif()
# 将*boost libraries here* 替换为你须要的boost子库 ,如:filesystem 等
1
2
3
4
5
6
7
8
9
10
11
protobuf 连接库
include(FindProtobuf)
find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIR})
...
target_link_libraries(progname
${PROTOBUF_LIBRARY}
)
1
2
3
4
5
6
7
protobuf 用 protoc 编译xxx.proto 文件生成cpp与.h
cmake_minimum_required(VERSION 2.8)
set(CMAKE_VERBOSE_MAKEFILE ON)it
find_package(Protobuf REQUIRED)
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS Express.proto)
add_library(foo ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(foo ${PROTOBUF_LIBRARIES} pthread) #foo 连接 protobuf与pthread
1
2
3
4
5
6
7
OpenCV
find_package(OpenCV REQUIRED)
message(${OpenCV_INCLUDE_DIRS}) # 非必需 message(${OpenCV_LIBS}) # 非必需 include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(progname ${OpenCV_LIBS} ) #或者能够设置一个很是有用的宏变量 OpenCV_DIR="your/path/include/OpenCVConfig.cmake file" 1 2 3 4 5 6 7 8 9 OpenMP find_package(OpenMP REQUIRED) if (OPENMP_FOUND) message("found") set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") endif() 1 2 3 4 5 6 7 二、多级目录下的CMakeLists.txt . ├── CMakeLists.txt #顶层目录下 ├── include │ ├── demo.h │ └── detec.h ├── lib ├── src │ ├── demo.cpp │ ├── detec.cpp │ └── proto │ ├── CMakeLists.txt #子目录 │ └── myProto.proto └── tools └── pipe.cpp #只需在顶层CMakeListst.txt 加上以下两句话,就会自动执行子目录下的CMakeLists.txt add_subdirectory(${PROJECT_SOURCE_DIR}/src/proto) include_directories(${PROJECT_BINARY_DIR}/src/proto) --------------------- 做者:_小马奔腾 来源:CSDN 原文:https://blog.csdn.net/dongfang1984/article/details/55105537 版权声明:本文为博主原创文章,转载请附上博文连接!