cmake 实例分析,cmake入门

样例1

假设一个main.cpp(),在同文件夹下include 了A.h,然后A.cpp包含了B.h,那么这个项目的cmake可以这么写:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
CMAKE_POLICY(VERSION 2.8)

FIND_PACKAGE(OpenCV REQUIRED)
上面指明了cmake的版本

然后find_package表明需要用到opencv的库,如果需要其他其他库的话就加进去就好了

aux_source_directory(. DIR_SRCS)
这个是把 . (当前文件夹)的所有文件加进DIR_SRCS里

然后message可以打印一下DIR_SRCS这个变量。

MESSAGE("PROJECT_SOURCE_DIR : " ${DIR_SRCS})

结果是当前文件夹下面的所有cpp

set(CMAKE_CXX_FLAGS "-std=c++11")
这个表明用c++11,不要也行,但是据说要厉害一点

添加生成可执行文件

ADD_EXECUTABLE(Launch ${DIR_SRCS})
TARGET_LINK_LIBRARIES(Launch ${OpenCV_LIBS} )
添加链接,用到了opencv就把lib加进去

全部内容如下:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
CMAKE_POLICY(VERSION 2.8)

FIND_PACKAGE(OpenCV REQUIRED)

INCLUDE_DIRECTORIES(
${PROJECT_SOURCE_DIR}
${OpenCV_INCLUDE_DIRS}
)

#source directory
aux_source_directory(. DIR_SRCS)


MESSAGE("PROJECT_SOURCE_DIR : " ${DIR_SRCS})

set(CMAKE_CXX_FLAGS "-std=c++11")

ADD_EXECUTABLE(Launch ${DIR_SRCS})
TARGET_LINK_LIBRARIES(Launch ${OpenCV_LIBS} )

MESSAGE("OpenCV_LIBS : " ${OpenCV_LIBS})
 



样例2

SET(FLY_CAPTURE_SDK_INCLUDE /usr/include/flycapture)
SET(FLT_CAPTURE_SDK_LIBS /usr/lib/libflycapture.so) 

INCLUDE_DIRECTORIES(
  ${OpenCV_INCLUDE_DIRS}
  ${FLY_CAPTURE_SDK_INCLUDE}
)


如果要包含一些其他库

用set手动设置头文件位置和.so文件,注意.so文件是动态链接文件,win下是.dll,.a文件是静态链接文件,许多.o组成.a

INCLUDE_DIRECTORIES表示在哪里去找include文件

ADD_LIBRARY(acfDetec acfDetect.cpp)
ADD_LIBRARY(convTri convTri.cpp)
...

将一些cpp加入library,这些就会在cmake的位置生成.a文件,这些.a文件是可以直接拷贝给其他程序调用的,就是一些库文件,用法下一篇博客会讲


ADD_EXECUTABLE(subcat main.cpp)
TARGET_LINK_LIBRARIES(subcat acfDetec convTri gradientMex imPad imResample rgbConvert wrappers ${OpenCV_LIBS} ${FLT_CAPTURE_SDK_LIBS})
然后这个就把上面生成的一些.a链接进去,其实应该像样例1一样写的,这样写太麻烦,注意第一个是可执行文件名


全部内容

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
CMAKE_POLICY(VERSION 2.8)

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -mfpmath=sse -mmmx -msse -msse2 -msse3")
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")

FIND_PACKAGE(OpenCV REQUIRED)

SET(FLY_CAPTURE_SDK_INCLUDE /usr/include/flycapture)
SET(FLT_CAPTURE_SDK_LIBS /usr/lib/libflycapture.so) 

INCLUDE_DIRECTORIES(
  ${OpenCV_INCLUDE_DIRS}
  ${FLY_CAPTURE_SDK_INCLUDE}
)



ADD_LIBRARY(acfDetec acfDetect.cpp)
ADD_LIBRARY(convTri convTri.cpp)
ADD_LIBRARY(gradientMex gradientMex.cpp)
ADD_LIBRARY(imPad imPad.cpp)
ADD_LIBRARY(imResample imResample.cpp)
ADD_LIBRARY(rgbConvert rgbConvert.cpp)
ADD_LIBRARY(wrappers wrappers.cpp)
#ADD_LIBRARY()


ADD_EXECUTABLE(subcat main.cpp)
TARGET_LINK_LIBRARIES(subcat acfDetec convTri gradientMex imPad imResample rgbConvert wrappers ${OpenCV_LIBS} ${FLT_CAPTURE_SDK_LIBS})
 







你可能感兴趣的:(脚本编程python,linux相关,项目相关)