cmake 之一个小例子

转自:http://my.oschina.net/iamhere/blog/489838

cmake,比手写makefile更好的选择

安装cmake,此部分略过

一、新建一个工程

这里我是在windows下使用eclipse新建了一个c工程(PS:我一般新建一个Makefile类型的工程,这样比较干净)

二、建立必要的文件夹

我的工程目录:


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
D:\code\cpp\cmakestudy\ test >tree /f
卷 软件 的文件夹 PATH 列表
卷序列号为 0006-17B7
D:.
│  .cproject
│  .project
│  CMakeLists.txt
├─bin
├─include
│      sum .h
├─lib
└─src
     │  CMakeLists.txt
    
     ├─main
     │      CMakeLists.txt
     │      main.c
    
     └─ sum
             CMakeLists.txt
             sum .c
 
 
D:\code\cpp\cmakestudy\ test >
bin:用于存放生成的可执行文件


include:存放头文件

lib:存放生成的库文件

src:源代码

三、写源代码

include/sum.h


?
1
2
3
4
5
6
#ifndef INCLUDE_SUM_H_
#define INCLUDE_SUM_H_
 
int sum( int a, int b );
 
#endif /* INCLUDE_SUM_H_ */



src/sum/sum.c


?
1
2
3
4
5
#include"../../include/sum.h"
 
int sum( int a, int b ){
     return a + b;
}



src/main/main.c


?
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
 
#include"../../include/sum.h"
 
int main( void ){
     int a = 0;
     int b = 0;
     puts ( "输入两个整数:" );
     scanf ( "%d %d" ,&a,&b);
     printf ( "%d + %d = %d\n" ,a,b,sum(a,b));
     return 0;
}



四、编写CMakeLists.txt

顶层CMakeLists.txt


?
1
2
3
4
5
# 定义工程名称
project(HELLO)
 
# 定义子目录src,用以递归的调用src中的MakeLists.txt
add_subdirectory(src)



src/CMakeLists.txt


?
1
2
3
4
5
# 定义子目录
add_subdirectory(main)
 
#定义子目录
add_subdirectory( sum )



src/sum/CMakeLists.txt


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 设置编译器
set (CMAKE_C_COMPILER gcc )
 
# 源文件列表
set (SRC_LIST sum .c)
 
# 头文件目录
include_directories(${HELLO_SOURCE_DIR} /include )
 
# 设置生成的库文件的路径
set (LIBRARY_OUTPUT_PATH ${HELLO_SOURCE_DIR} /lib )
 
# 生成的库文件
add_library( sum STATIC ${SRC_LIST})



src/main/CMakeLists.txt


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 源文件列表
set (SRC_LIST main.c)
 
# 头文件列表
include_directories(${HELLO_SOURCE_DIR} /include )
 
# 设置生成的可执行文件的路径
set (EXECUTABLE_OUTPUT_PATH ${HELLO_SOURCE_DIR} /bin )
 
# 生成的可执行文件
add_executable(hello ${SRC_LIST})
 
# 所需要的库文件的目录
link_directories(${HELLO_SOURCE_DIR} /lib )
 
# 需要链接的库文件
target_link_libraries(hello sum )




五、使用cmake生成Makefile

在项目中新建一个build目录,现在的项目目录:


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
D:\code\cpp\cmakestudy\ test >tree /f
卷 软件 的文件夹 PATH 列表
卷序列号为 0006-17B7
D:.
│  .cproject
│  .project
│  CMakeLists.txt
├─bin
├─build
├─include
│      sum .h
├─lib
└─src
     │  CMakeLists.txt
    
     ├─main
     │      CMakeLists.txt
     │      main.c
    
     └─ sum
             CMakeLists.txt
             sum .c
 
 
D:\code\cpp\cmakestudy\ test >
执行cmkae命令:



?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
D:\code\cpp\cmakestudy\ test \build>cmake .. -G "MinGW Makefiles"
-- The C compiler identification is GNU 4.7.1
-- The CXX compiler identification is GNU 4.7.1
-- Check for working C compiler: D: /program/cpp/MinGW/bin/gcc .exe
-- Check for working C compiler: D: /program/cpp/MinGW/bin/gcc .exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: D: /program/cpp/MinGW/bin/g ++.exe
-- Check for working CXX compiler: D: /program/cpp/MinGW/bin/g ++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) in CMakeLists.txt:
   No cmake_minimum_required command is present.  A line of code such as
 
     cmake_minimum_required(VERSION 3.1)
 
   should be added at the top of the file .  The version specified may be lower
   if you wish to support older CMake versions for this project.  For more
   information run "cmake --help-policy CMP0000" .
This warning is for project developers.  Use -Wno-dev to suppress it.
 
-- Configuring done
-- Generating done
-- Build files have been written to: D: /code/cpp/cmakestudy/test/build
 
D:\code\cpp\cmakestudy\ test \build>


现在的项目目录:


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
D:\code\cpp\cmakestudy\ test >tree /f
卷 软件 的文件夹 PATH 列表
卷序列号为 0006-17B7
D:.
│  .cproject
│  .project
│  CMakeLists.txt
├─bin
├─build
│  │  CMakeCache.txt
│  │  cmake_install.cmake
│  │  Makefile
│  │
│  ├─CMakeFiles
│  │  │  cmake.check_cache
│  │  │  CMakeDirectoryInformation.cmake
│  │  │  CMakeOutput.log
│  │  │  Makefile.cmake
│  │  │  Makefile2
│  │  │  progress.marks
│  │  │  TargetDirectories.txt
│  │  │
│  │  ├─3.1.0
│  │  │  │  CMakeCCompiler.cmake
│  │  │  │  CMakeCXXCompiler.cmake
│  │  │  │  CMakeDetermineCompilerABI_C.bin
│  │  │  │  CMakeDetermineCompilerABI_CXX.bin
│  │  │  │  CMakeRCCompiler.cmake
│  │  │  │  CMakeSystem.cmake
│  │  │  │
│  │  │  ├─CompilerIdC
│  │  │  │      a.exe
│  │  │  │      CMakeCCompilerId.c
│  │  │  │
│  │  │  └─CompilerIdCXX
│  │  │          a.exe
│  │  │          CMakeCXXCompilerId.cpp
│  │  │
│  │  └─CMakeTmp
│  └─src
│      │  cmake_install.cmake
│      │  Makefile
│      │
│      ├─CMakeFiles
│      │      CMakeDirectoryInformation.cmake
│      │      progress.marks
│      │
│      ├─main
│      │  │  cmake_install.cmake
│      │  │  Makefile
│      │  │
│      │  └─CMakeFiles
│      │      │  CMakeDirectoryInformation.cmake
│      │      │  progress.marks
│      │      │
│      │      └─hello. dir
│      │              build. make
│      │              C.includecache
│      │              cmake_clean.cmake
│      │              depend.internal
│      │              depend. make
│      │              DependInfo.cmake
│      │              flags. make
│      │              includes_C.rsp
│      │              link.txt
│      │              linklibs.rsp
│      │              objects.a
│      │              objects1.rsp
│      │              progress. make
│      │
│      └─ sum
│          │  cmake_install.cmake
│          │  Makefile
│          │
│          └─CMakeFiles
│              │  CMakeDirectoryInformation.cmake
│              │  progress.marks
│              │
│              └─ sum . dir
│                      build. make
│                      C.includecache
│                      cmake_clean.cmake
│                      cmake_clean_target.cmake
│                      depend.internal
│                      depend. make
│                      DependInfo.cmake
│                      flags. make
│                      includes_C.rsp
│                      link.txt
│                      progress. make
├─include
│      sum .h
├─lib
└─src
     │  CMakeLists.txt
    
     ├─main
     │      CMakeLists.txt
     │      main.c
    
     └─ sum
             CMakeLists.txt
             sum .c
 
 
D:\code\cpp\cmakestudy\ test >




六、执行make,并运行程序

make:


?
1
2
3
4
5
6
7
8
9
10
11
D:\code\cpp\cmakestudy\ test \build> make
Scanning dependencies of target sum
[ 50%] Building C object src /sum/CMakeFiles/sum . dir /sum .obj
Linking C static library ..\..\..\lib\libsum.a
[ 50%] Built target sum
Scanning dependencies of target hello
[100%] Building C object src /main/CMakeFiles/hello . dir /main .ob
Linking C executable ..\..\..\bin\hello.exe
[100%] Built target hello
 
D:\code\cpp\cmakestudy\ test \build>
现在的项目目录:



?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
D:\code\cpp\cmakestudy\ test >tree /f
卷 软件 的文件夹 PATH 列表
卷序列号为 0006-17B7
D:.
│  .cproject
│  .project
│  CMakeLists.txt
├─bin
│      hello.exe
├─build
│  │  CMakeCache.txt
│  │  cmake_install.cmake
│  │  Makefile
│  │
│  ├─CMakeFiles
│  │  │  cmake.check_cache
│  │  │  CMakeDirectoryInformation.cmake
│  │  │  CMakeOutput.log
│  │  │  Makefile.cmake
│  │  │  Makefile2
│  │  │  progress.marks
│  │  │  TargetDirectories.txt
│  │  │
│  │  ├─3.1.0
│  │  │  │  CMakeCCompiler.cmake
│  │  │  │  CMakeCXXCompiler.cmake
│  │  │  │  CMakeDetermineCompilerABI_C.bin
│  │  │  │  CMakeDetermineCompilerABI_CXX.bin
│  │  │  │  CMakeRCCompiler.cmake
│  │  │  │  CMakeSystem.cmake
│  │  │  │
│  │  │  ├─CompilerIdC
│  │  │  │      a.exe
│  │  │  │      CMakeCCompilerId.c
│  │  │  │
│  │  │  └─CompilerIdCXX
│  │  │          a.exe
│  │  │          CMakeCXXCompilerId.cpp
│  │  │
│  │  └─CMakeTmp
│  └─src
│      │  cmake_install.cmake
│      │  Makefile
│      │
│      ├─CMakeFiles
│      │      CMakeDirectoryInformation.cmake
│      │      progress.marks
│      │
│      ├─main
│      │  │  cmake_install.cmake
│      │  │  Makefile
│      │  │
│      │  └─CMakeFiles
│      │      │  CMakeDirectoryInformation.cmake
│      │      │  progress.marks
│      │      │
│      │      └─hello. dir
│      │              build. make
│      │              C.includecache
│      │              cmake_clean.cmake
│      │              depend.internal
│      │              depend. make
│      │              DependInfo.cmake
│      │              flags. make
│      │              includes_C.rsp
│      │              link.txt
│      │              linklibs.rsp
│      │              main.obj
│      │              objects.a
│      │              objects1.rsp
│      │              progress. make
│      │
│      └─ sum
│          │  cmake_install.cmake
│          │  Makefile
│          │
│          └─CMakeFiles
│              │  CMakeDirectoryInformation.cmake
│              │  progress.marks
│              │
│              └─ sum . dir
│                      build. make
│                      C.includecache
│                      cmake_clean.cmake
│                      cmake_clean_target.cmake
│                      depend.internal
│                      depend. make
│                      DependInfo.cmake
│                      flags. make
│                      includes_C.rsp
│                      link.txt
│                      progress. make
│                      sum .obj
├─include
│      sum .h
├─lib
│      libsum.a
└─src
     │  CMakeLists.txt
    
     ├─main
     │      CMakeLists.txt
     │      main.c
    
     └─ sum
             CMakeLists.txt
             sum .c
 
 
D:\code\cpp\cmakestudy\ test >




执行程序:


?
1
2
3
4
5
6
D:\code\cpp\cmakestudy\ test \bin>hello.exe
输入两个整数:
2 3
2 + 3 = 5
 
D:\code\cpp\cmakestudy\ test \bin>


七、cmake总结

1、关于项目目录结构

我其实并不太了解C/C++项目应该有什么样的目录结构,不过我在找cmake的资料的时候,看到的项目目录结构都差不多有include,src,bin,build这几个目录。然后不同的模块放到src下不同的目录中。

对于我来说,还远远没有到那种分模块开发、测试的水平,第三方库也不会几个,但是之后会按照这个目录结构学下去,也建议各位在写C/C++程序的时候注意目录结构。

2、顶层的CMakeLists.txt

对于这个小例子来说,这一个CMakeLists.txt其实就两个作用。一是定义程序的名称,二是定义子目录。

project(HELLO)这一句还隐式的定义了两个cmake变 量:<projectname>_BINARY_DIR以及<projectname>_BINARY_DIR,这里就是 HELLO_BINARY_DIR和HELLO_SOURCE_DIR,两个变量指的都是当前工程的路径。

add_subdirectory(src),定义当前目录包含的子目录,以调用其子目录中的CMakeLists.txt。其实看目录结果就应该能看出来,对于含有包含CMakeLists.txt子目录的目录来说,其CMakeLists.txt都应该有若干add_subdirectory(src)。

3、src中每个模块的CMakeLists.txt

必不可少的部分有:

源文件列表,头文件所在目录,生成的库文件的路径和名字,如果需要链接其它库文件,如各种第三方库,则需要给出第三方库的头文件所在目录和库文件。

4、主函数所在文件夹的CMakeLists.txt

和每个模块都差不多,只是多了关于可执行文件生成的目录和名字。

PS:关于所需库的目录和库文件名这一部分,需要写到最后,否则会出问题,暂时不知原因。

5、下一步学习cmake的目标

a.cmake命令,包括学会常用的命令、关于项目设置比如生成安装程序,doc文档等

b.第三方库,要做一到这一步,就需要先会使用第三方库,如gtk+,libxml等


八、参考:

http://www.cppblog.com/Roger/archive/2011/11/17/160368.html

http://blog.csdn.net/dbzhang800/article/details/6314073


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值