cmake 简介 - Something TO DO

cmake 简介

Something TO DO posted @ 2017年2月15日 16:11 in ROS , 618 阅读

 

转载自:http://tzc.is-programmer.com/show/476.html

CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。

 

CMake 使用方法

    CMake的所有的语句都写在一个叫:CMakeLists.txt的文件中。当CMakeLists.txt文件确定后,可以用ccmake命令对相关 的变量值进行配置。这个命令必须指向CMakeLists.txt所在的目录。配置完成之后,应用cmake命令生成相应的makefile(在Unix like系统下)或者 project文件(指定用window下的相应编程工具编译时)。

    其基本操作流程为:

  1. $> ccmake directory
  2. $> cmake directory
  3. $> make

  其中directory为CMakeList.txt所在目录;

  • 第一条语句用于配置编译选项,如VTK_DIR目录 ,一般这一步不需要配置,直接执行第二条语句即可,但当出现错误时,这里就需要认为配置了,这一步才真正派上用场;
  • 第二条命令用于根据CMakeLists.txt生成Makefile文件;
  • 第三条命令用于执行Makefile文件,编译程序,生成可执行文件;

CMake的执行就是这么简单,其难点在于如何编写CMakeLists.txt文件,下面结合例子简单介绍CMakeLists.txt的编写,看下面这个CMakeLists.txt

  1. #project name
  2. PROJECT(test_math)
  3. #head file path
  4. INCLUDE_DIRECTORIES(
  5. include
  6. )
  7. #source directory
  8. AUX_SOURCE_DIRECTORY(src DIR_SRCS)
  9. #set environment variable
  10. SET(TEST_MATH
  11. ${DIR_SRCS}
  12. )
  13. #set extern libraries
  14. SET(LIBRARIES
  15. libm.so
  16. )
  17. #add executable file
  18. ADD_EXECUTABLE(../bin/bin ${TEST_MATH})
  19. #add link library
  20. TARGET_LINK_LIBRARIES(../bin/bin ${LIBRARIES})
  21.  

            或者用下面这个CMakeLists.txt

  1. #project name
  2. PROJECT(test_math)
  3. #head file path
  4. INCLUDE_DIRECTORIES(
  5. include
  6. )
  7. #source directory
  8. AUX_SOURCE_DIRECTORY(src DIR_SRCS)
  9. #set environment variable
  10. SET(TEST_MATH
  11. ${DIR_SRCS}
  12. )
  13. #add executable file
  14. ADD_EXECUTABLE(../bin/bin ${TEST_MATH})
  15. #add link library
  16. TARGET_LINK_LIBRARIES(../bin/bin m)

 这是一个测试数学函数的程序的CMakeLists.txt,"#"后面为注释的内容,CMake的命令全部为大写

第2行指定生成的工程名为test_math

第4行指定头文件目录为include

第8行指定源文件目录为src,并将其赋值给环境变量DIR_SRCS

第10行设定环境变量TEST_MATH的值为环境变量DIR_SRCS的值,此处用于显示如何用环境变量对环境变量进行赋值

第14行将数学函数库赋值给环境变量LIBRARIES,当然,可以不用这个环境变量,而在后面直接使用该库名

第18行用于指定生成文件,将环境变量TEST_MATH目录下的所有文件编译生成../bin目录下的可执行文件bin

第20行指定../bin/bin执行时的链接库为环境变量LIBRARIES的值-libm.so

下面给出源文件
/src/main.c:

  1. #include<stdio.h>
  2. #include"../include/a.h"
  3. int main()
  4. {
  5.     double b=25.0;
  6.     double a=0.0;
  7.     a=get_sqrt(b);
  8.  
  9.     printf("a is %lf, b is %lf\n",a,b);
  10.     return 0;
  11. }
  12.  

/src/a.c

  1. #include"../include/a.h"
  2. double get_sqrt(double var1)
  3. {
  4.     return sqrt(var1);
  5. }
  6.  

 

/include/a.h

 #ifndef  A_FILE_HEADER_INC

  1.  
  2. #define  A_FILE_HEADER_INC
  3. #include<math.h>
  4.  
  5. double get_sqrt(double var1);
  6.  
  7. #endif

将CMakeLists.txt放在当前目录下,执行CMakeLists.txt

  1. $> cmake .
  2. $> make

即可生成可执行文件,在目录/bin下的bin文件,好了运行看其效果是否和所想一样。

Avatar_small
UP Intermediate Ques 说:
2022年8月18日 12:47

UP Board 12th Question Paper 2023, The Board of Higher Education Uttar Pradesh UPMSP, conducts the final Matriculation examination every year. UP Intermediate Question Paper 2022 PDF This year too, Uttar-Pradesh Higher Secondary School Certificate public Examination 2023 will be performed during March and April 2023. UP Board 12th Question Paper 2023, The Board of Higher Education Uttar Pradesh UPMSP, conducts the final Matriculation examination every year.

Avatar_small
jnanabhumiap.in 说:
2024年1月09日 14:14

JNANABHUMI AP provides all latest educational updates and many more. The main concept or our aim behind this website has been the will to provide resources full information on each topic which can be accessed through Internet. To ensure that every readers get’s what important and worthy about the topic they search and link to hear from us. jnanabhumiap.in Jnanabhumi AP is a startup by passionate webmasters and bloggers who have passion to provide engaging content which is accurate, interesting and worthy to read. We are mope like a web community where you can find different information’s, resources, topics on day to day incidents or news. We provide you the finest of web content on each and every topics possible with help of editorial and content team.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee