CMakeList.txt

  • Post author:
  • Post category:其他


一个视频讲解 http://v.youku.com/v_show/id_XMjc1MjE0MjEwNA==.html

cmake 语法设置路径,配置库,编译器标记:https://www.cnblogs.com/binbinjx/p/5626916.html

opencv 编译程序:https://yq.aliyun.com/articles/63300

ROS catkin cmake编译:http://wiki.ros.org/catkin/CMakeLists.txt

CMake是什么:

Cross-plate Make 自动化建制系统

默认KDE4 的make ,kitware 公司所属.cmake 根据CMakeList.txt 生成makefile,指令make根据makefile生成可执行文件

安装 sudo apt-get install cmake

cmake 语法:

###################

#cmake version 最低版版本声明
cmake_minimun_required(VERSION 2.6)
#project name 默认生成两个变量
#变量1:projectname_BINARY_DIR\
#变量2:projectname_SOURCE_DiR
PROJECT(demoA) 
#set extern libraries
#格式为SET(VAR_NAME 【value】)
SET(LIBRARIES /usr/lib/x86_64-linux-gnu/libm.so)
SET(ALL_CODE_LIST demo.cpp sqrt.cpp sqrt.h)
或者
SET(SRC_LIST main.c  t1.c t2.c)
#print the value of variable
MESSAGE(${ALL_CODE_LIST})
#${} 取变量内容的意思,{变量名} 之前已经设置了
#add executable file 
ADD_EXECUTABLE(sqrtDemoA ${ALL_CODE_LIST})
MESSAGE(STATUS "This is BINARY_DIR" ${demoA_BINARY_DIR})
MESSAGE((STATUS "This is SOURCE_DIR" ${demoA_SOURCE_DIR}))
#add link libraries
TARGET_LINK_LIBRARIES(sqrtDemoA ${ALL_CODE_LIST})
#add output path 更改可执行文件输出目录
SET(EXECTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/bin")

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

MESSAGE 格式(输出语句):

MESSAGE([STATUS|SEND_ERROR|FATAL_ERROR] “message”)

STATUS              :输出前缀为 ——的信息

SEND_ERROR:产生错误,生成过程被跳过

FATAL_ERROR:立即终止所有cmake过程

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

当工程中源文件比较多时:

SET(ALL_CODE_LIST demo.cpp sqrt.cpp sqrt.h) #这句就不是很方便了

然而有其他办法:

AUX_SOURCE_DIRECTORY(directory VAR_NAME)

eg:

AUX_SOURCE_DIRECTORY(. DIR_SRC)

AUX_SOURCE_DIRECTORY(./hello/src  DIR_SRC)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1.大小写没有区分,推荐大小写区分

2.变量用${} 取值,但在if 语句中 直接使用变量名

eg

ADD_EXECUTABLE(hello ${SRC_LIST}) == ADD_EXECUTABLE(hello main.c)

3.指令(参数1 参数2)参数之间使用空格或者分号(双引号?)(去掉扩展名)

eg

SET(SRC_LIST main.c)

SET(SRC_LIST”main.c”)

SET(SRC_LIST main)

4.编译清理

make clean

===========================================================

http://v.youku.com/v_show/id_XMjc3MDQzMzc2MA==.html?spm=a2h0j.8191423.module_basic_relation.5~5!2~5~5!3~5~5~A

http://www.360doc.com/content/12/0507/10/9369336_209205930.shtml

===========================================================

barrett CMakeList.txt

5.cmake的工作流程是

mkdir build

cd build

cmake 。。//。。代表什么? cmake ..和cmake 。 和cmake CMakeList.txt区别?

//.cmake . == cmake CMakeList.txt

//cmake .. 代表cmakelist.txt在上一目录。

make

make install //可选

5.ROS的CmakeList.txt解读

http://rosclub.cn/post-783.html

http://wiki.ros.org/catkin/CMakeLists.txt

ROS catkin Cmakelists.txt 遵循以下的逻辑

Required CMake Version (cmake_minimum_required)
Package Name (project())
Find other CMake/Catkin packages needed for build (find_package())
Enable Python module support (catkin_python_setup())
Message/Service/Action Generators (add_message_files(), add_service_files(), add_action_files())
Invoke message/service/action generation (generate_messages())
Specify package build info export (catkin_package())
Libraries/Executables to build (add_library()/add_executable()/target_link_libraries())
Tests to build (catkin_add_gtest())
Install rules (install())

5.1 Cmake Version

cmake_minimum_required(VERSION 2.8.3)


5.2 Package Name

project(robot_brain)


5.3 发现cmake 包依赖项

find_package(catkin REQUIRED)

可使用该命令的包,文件夹下均有.cmake 文件夹。自动转化为catkin组件catkin components.Required 表示如果找不到该包将停止编译。

如果你不使用这种写法,你想要指定一些包为组件,像下面使用nodelet 作为组件。

find_package(catkin REQUIRED COMPONENTS nodelet)


NB: You should only find_package components for which you want build flags. You should not add runtime dependencies.

You could also do:

find_package(catkin REQUIRED)
find_package(nodelet REQUIRED)




find_package() 做了什么


If a package is found by CMake through find_package, it results in the creation of several CMake environment variables that give information about the found package. These environment variables can be utilized later in the CMake script. The environment variables describe where the packages exported header files are, where source files are, what libraries the package depends on, and the paths of those libraries. The names always follow the convention of <PACKAGE NAME>_<PROPERTY>:

<NAME>_FOUND – Set to true if the library is found, otherwise false

<NAME>_INCLUDE_DIRS or <NAME>_INCLUDES – The include paths exported by the package

<NAME>_LIBRARIES or <NAME>_LIBS – The libraries exported by the package

<NAME>_DEFINITIONS – ?

http://blog.csdn.net/bytxl/article/details/50637277



为什么caktin package 需要被指定为components 组件


Catkin packages are not really components of catkin. Rather the components feature of CMake was utilized in the design of catkin to save you significant typing time.

For catkin packages, if you find_package them as components of catkin, this is advantageous as a single set of environment variables is created with the catkin_ prefix. For example, let us say you were using the package nodelet in your code. The recommended way of finding the package is:

find_package(catkin REQUIRED COMPONENTS nodelet)


This means that the include paths, libraries, etc exported by nodelet are also appended to the catkin_ variables. For example, catkin_INCLUDE_DIRS contains the include paths not only for catkin but also for nodelet as well! This will come in handy later.

We could alternatively find_package nodelet on its own:

find_package(nodelet)

This means the nodelet paths, libraries and so on would not be added to catkin_ variables.

This results in nodelet_INCLUDE_DIRS, nodelet_LIBRARIES, and so on. The same variables are also created using

find_package(catkin REQUIRED COMPONENTS nodelet)




使用boost

If using C++ and Boost, you need to invoke find_package() on Boost and specify which aspects of Boost you are using as components. For example, if you wanted to use Boost threads, you would say:

find_package(Boost REQUIRED COMPONENTS thread)



版权声明:本文为qq_35508344原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。