Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1484776
  • 博文数量: 230
  • 博客积分: 474
  • 博客等级: 下士
  • 技术积分: 1955
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 18:40
文章分类

全部博文(230)

文章存档

2020年(3)

2019年(3)

2018年(12)

2017年(13)

2016年(11)

2015年(55)

2014年(74)

2013年(39)

2012年(2)

2011年(18)

我的朋友

分类:

2011-06-17 17:48:54

转载自:http://www.cnblogs.com/powerkoria/archive/2009/07/28/1532799.html
CMAKE--跨平台安装编译工具

the cross-platform, open-source build system. CMake is a family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used in the compiler environment of your choice.

收藏一下文章
cmake 简介
tt posted @ 2007年10月12日 19:38 in , 4512 阅读

    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
  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.     ("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
  4.  
  5. double get_sqrt(double var1);
  6.  
  7. #endif

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

  1. $> cmake .
  2. $> make

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

转载自:http://www.cnblogs.com/powerkoria/archive/2009/07/28/1532799.html
阅读(2006) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~