Cmake工程例子
在Unix类型的系统上为了将代码比较好的组织在一起形成一个有序的编译链接形式,我们常常会使用CMakeLists.txt来指定一个代码工程的编译依赖和一些宏定义。然后在对应的目录下通过cmake .
生成Makefile文件,然后再使用make来对整个工程进行编译。
所以下面内容是参照CMake的官方教程1重新表述的。
最基本的操作
对应于单一的文件也是可以写一个CMakeLists.txt来进行编译,在CMakeLists.txt中这些命令语句是不区分大小写的。
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)
以上的代码就是指定工程的名字,然后指定用tutorial.cxx文件来生成可执行文件Tutorial,指定生成可执行文件的这个代码文件中是要包括main函数的。
添加工程的版本号
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add the executable
add_executable(Tutorial tutorial.cxx)
在这份样例中,指定了Tutorial工程的版本号为1.0,同时加入了给CMake的配置头文件。
本地样例编码
project(psf)
cmake_minimum_required(VERSION 2.9)
set(CMAKE_CXX_STANDARD 11)
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin )
set(SOURCE_FILES src/main.cpp
include/comparemat.h
src/comparemat.cpp
include/cvutils.h
src/cvutils.cpp
include/deconvpsf.h
src/deconvpsf.cpp
include/psfgen.h
src/psfgen.cpp
include/vtkutils.h
src/vtkutils.cpp
include/config.h src/basiccv.cpp include/basiccv.h)
# BOOST LIB CONFIG
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package( Boost REQUIRED COMPONENTS thread program_options)
#OPENCV CONFIG
find_package( OpenCV REQUIRED )
if(OpenCV_LIBS)
include_directories( ${OpenCV_INCLUDE_DIRS} )
endif()
#VTK CONFIG
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable( psf ${SOURCE_FILES} )
target_link_libraries( psf ${Boost_LIBRARIES} matio fftw3 tiff)
# add_executable( psf2Ddemo src/psfg.cpp
# src/psf_gen2.cpp
# src/cvTools.cpp)
# target_link_libraries( psf2Ddemo ${Boost_LIBRARIES} )
#
# add_executable( psfSpeed src/psf_accelerate.cpp
# src/psf_gen2.cpp )
# target_link_libraries( psfSpeed ${Boost_LIBRARIES} )
#
# add_executable( deconvLucyDemo src/demo_deconv_LR.cpp )
endif()
if(OpenCV_LIBS)
# target_link_libraries( psf2Ddemo ${OpenCV_LIBS})
# target_link_libraries( deconvLucyDemo ${OpenCV_LIBS} )
target_link_libraries( psf ${OpenCV_LIBS} )
endif()
if(VTK_LIBRARIES)
target_link_libraries( psf ${VTK_LIBRARIES} )
# target_link_libraries( psf2Ddemo ${VTK_LIBRARIES})
endif()
平台识别
因为这是一个跨平台的工具,但是因为我们日常使用不同的系统中的库链接目标是不同的,所以为了使同一个CMakeLists.txt能够在不同平台下都能指引项目的编译,我们需要加入判断平台的语句.
if(UNIX)
target_link_libraries(YogaTest
tiff blosc tbb)
endif()
if(MINGW)
target_link_libraries(YogaTest
tiff.lib blosc.lib tbb.lib)
endif()
Enjoy Reading This Article?
Here are some more articles you might like to read next: