1. 程式人生 > >大型專案CMakeLIsts.txt的編寫規範

大型專案CMakeLIsts.txt的編寫規範

開發十年,就只剩下這套架構體系了! >>>   

1、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_EXECUTABLE

2、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 動態連結庫

3、Process a list
FOREACH(loop_var)
...
ENDFOREACH(loop_var)
1
2
3
4、3rd party or .h
If the 3rd party library or .h is in a “standard”: 
directory (PATH and/or LD_LIBRARY_PATH) is automatic

If in a non­standard dir: 
Headers: use INCLUDE_DIRECTORIES 
Libraries: use FIND_LIBRARY and link with the result of it


cmake_minimum_required(VERSION 2.8)
project (faceIdentification)
file(GLOB_RECURSE srcs ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

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")

 find_package(Caffe)
 include_directories(
${PROJECT_SOURCE_DIR}/include
${Caffe_INCLUDE_DIRS})
 add_definitions(${Caffe_DEFINITIONS})


foreach(source ${srcs})
get_filename_component(name ${source} NAME_WE)

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
附錄
1、幾種常用的庫的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*)  

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)

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
2、多級目錄下的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 
版權宣告:本文為博主原創文章,轉載請