cmake 简介

tt posted @ 2007年10月13日 03:38 in cmake , 31315 阅读

    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
lanjianjj 说:
2007年11月15日 20:11 您好,我想改变Cmake指定的编译器应该怎么设置呢? 我在VC6下面安装了QT4,但是期望cmake调用VC8的编译器编译,每次它默认就使用了VC6的编译器,这个要怎么办呢? 请联系我:1314lanjian@gmail.com或者QQ:174883067(验证信息CMAKE)多谢了
Avatar_small
tt 说:
2007年11月15日 23:23 在进行CMake配置的时候可以选择编译器,通过下拉选项选择就可以了
Avatar_small
tt 说:
2007年11月15日 23:24 我在windows下没用过,不知道说的是否可以,呵呵
Avatar_small
一剑行天下 说:
2008年11月09日 05:11

写的很好!我转了!谢了!!!

Avatar_small
adsf 说:
2010年5月14日 08:31

http://ww.64edu.comhttp://ww.64edu.comhttp://ww.64edu.comhttp://ww.64edu.comhttp://ww.64edu.com

Avatar_small
dial a maid dubai 说:
2021年6月07日 17:52

To recieve a fantastic cleaning, applying a top-bottom cleaning up method might be highly helpful and a reduced amount of time-consuming. Consequently, start cleaning up the fixtures, appliances and also room with the top and after that proceed downhill. Additionally, it's important to remove your pet hair, dog's hair, dust dirt, crumbs, and various particles with the surfaces.

Avatar_small
part time maids in d 说:
2021年9月19日 14:58

Extra you grant clutters to accumulate at your house the for a longer time you will have to pay just for cleaning offerings. Conduct a building survey and find out all individuals that really need to be decluttered. One regulation for classifying them can be to determine whether you've gotten used which usually item in the last six times.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter