LinuxGem
此处为老的 LinuxGem,新版 LinuxGem 请移步 www.linuxgem.org

CMake 简介

galeki posted @ 2007年10月13日 10:20 in 编程技巧与工具 with tags cmake , 2916 阅读

转自 tzc 的 Blog:http://blog.linuxgem.org/tzc/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,"#"后面为注释的内容,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

  1. #ifndef  A_FILE_HEADER_INC
  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文件,好了运行看其效果是否和所想一样。

 

转自:http://blog.linuxgem.org/tzc/show/476.html

  • 无匹配
Click here 说:
2022年9月03日 14:41

I see that you are using WordPress on your blog, wordpress is the best.’;\':. https://www.simplybuzzes.com/angel-number-7227-meaning/

anonymous 说:
2023年9月15日 14:33

You ought to basically fantastic not to mention solid advice, which means notice: Kissimmee Bed Bug Control

anonymous 说:
2023年9月20日 20:12

Mmm.. estimable to be here in your report or notify, whatever, I repute I should moreover process strong for my have website want I play some salubrious further updated busy in your location. Termite Treatment Orlando

anonymous 说:
2023年9月20日 20:14

This is very significant, and yet necessary towards just click this unique backlink: Termite Treatment Oviedo

anonymous 说:
2023年11月07日 14:29

You should mainly superior together with well-performing material, which means that see it: Online Accounting Firm

anonymous 说:
2023年11月14日 15:52

It is very good, but look at the information at this address.  drink up grinches


登录 *


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