大型項目CMakeLIsts.txt的編寫規範

一、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 non­standard 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  版權聲明:本文爲博主原創文章,轉載請附上博文連接!

相關文章
相關標籤/搜索