大型项目CMakeLIsts.txt的编写规范

1、Very simple executable

PROJECT( helloworld )   # 非必需
SET( hello_SRCS hello.cpp )
ADD_EXECUTABLE( hello ${hello_SRCS} )

说明:
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} )

说明:
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)

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、几种常用的库的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 等
  • protobuf 链接库
include(FindProtobuf)
find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIR})
...
target_link_libraries(progname 
    ${PROTOBUF_LIBRARY}
)
  • 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
  • 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"
  • 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()

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)

你可能感兴趣的:(CMakeLists)