cmake的使用
编写CMakeLists.txt
编写通用的CMakeList.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)
#set a variable name ProjectName
set(ProjectName myproject)
project(${ProjectName})
file(GLOB_RECURSE all_files "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
message("cmake:USE_xxxlib on")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include/" /opt/xxxx/mkl/include)
#include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include/" ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/xxx/include)
#find opencv lib
find_package(OpenCV REQUIRED)
#check out compiler type and add compiler option
if(${CMAKE_COMPILER_IS_GNUCC})
message (STATUS "add c++11 flags")
add_definitions(-Wall -s -std=c++11 -g)
endif()
# add lib path
link_directories(/opt/xxxx/mkl/lib/intel64)
#add all *.cpp source code together and compile them to object
add_executable(${ProjectName} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${all_files})
#link your object with 3rdparty library
#you can link them with full path name or just prefix name of lib
target_link_libraries(${ProjectName} ${OpenCV_LIBS} mkl_intel_lp64 mkl_sequential mkl_core)
#target_link_libraries(${ProjectName} ${LIB}/librefblas.a )
#target_link_libraries(${ProjectName} /usr/lib/x86_64-linux-gnu/libgfortran.so.3 )
#target_link_libraries(${ProjectName} lapack blas)
编写多级CMakeList.txt
ls
3rdparty bin build.sh CMakeLists.txt Desktop.ini log.txt README.md resource src
顶层CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
#project name
project(myproject)
set(PROJECT_NAME myproject)
#define ROOT_SOURCE_DIR //it will be used in subdirectory CMakeLists.txt
set(ROOT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
#add subdirectory // it must contain a CMakelists.txt in subdirectory
add_subdirectory(src)
add_subdirectory(3rdparty)
CMakelists.txt in src directory
cmake_minimum_required(VERSION 2.8)
project(main)
#define path
set(LIBRARY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
#set(LIBRARY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} ${ROOT_SOURCE_DIR}/3rdparty/roptlib)
#option
option(USE_OPENBLAS "Use openblas" OFF)
option(USE_MKL "Use mkl" ON)
#find opencv module
find_package(OpenCV REQUIRED)
FIND_PACKAGE(OpenMP)
IF(OPENMP_FOUND)
MESSAGE(STATUS "Compiling with OpenMP support")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
ENDIF(OPENMP_FOUND)
# define out path
SET(EXECUTABLE_OUTPUT_PATH ${ROOT_SOURCE_DIR}/bin)
# add roptlib/*.h path
message("cmake:USE_ROPTLIB on")
include_directories(
${LIBRARY_SOURCE_DIR}
${ROOT_SOURCE_DIR}/3rdparty/roptlib
)
if(USE_MKL)
message("cmake:USE_MKL on")
include_directories(
${LIBRARY_SOURCE_DIR}
/opt/xxxx/mkl/include
)
# add lib path
link_directories(/opt/xxxx/mkl/lib/intel64)
endif (USE_MKL)
if(USE_OPENBLAS)
message("cmake:USE_OPENBLAS on")
include_directories(
${LIBRARY_SOURCE_DIR}
${ROOT_SOURCE_DIR}/3rdparty/openblas/include
)
# add lib path
link_directories(${ROOT_SOURCE_DIR}/3rdparty/openblas/lib)
endif (USE_OPENBLAS)
#add all *.cpp file into DIR_SRCS
AUX_SOURCE_DIRECTORY(. DIR_SRCS )
AUX_SOURCE_DIRECTORY(${ROOT_SOURCE_DIR}/3rdparty/roptlib DIR_SRCS )
add_executable(main ${DIR_SRCS})
# create main2 lib
ADD_LIBRARY(main2 ${DIR_SRCS})
# link the libraries you need to object
if(USE_MKL)
target_link_libraries(main ${OpenCV_LIBS} mkl_intel_lp64 mkl_sequential mkl_core)
endif (USE_MKL)
if(USE_OPENBLAS)
target_link_libraries(main ${OpenCV_LIBS} openblas)
endif (USE_OPENBLAS)
cmake编译后gdb调试
如何对cmake .. 然后生成的makefile 生成的可执行文件进行gdb调试呢?
在一次cmake ..之后,系统会生成一个CMakeCache.txt文件,打开该文件之后会找到一个CMAKE_CXX_FLAGS:STRING=的选项,在其后加-g,再cmake .. && make 之后 生成的文件就可被单步调试了。
cmake需要编译的源码中含有.cu文件时,需要nvcc来编译
cuda_add_executable(cu_exec ${head_files} ${cu_files})
cuda_add_executable(cu_exec ${head_files} ${cu_files})