Linux下CMake使用实例

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

站在用户的角度思考问题,与客户深入沟通,找到富川网站设计与富川网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、成都网站建设、企业官网、英文网站、手机端网站、网站推广、域名注册虚拟主机、企业邮箱。业务覆盖富川地区。

一、单文件目录

1. 编辑C程序文件,命名为main.c

#include  

int main(void) {
   printf("Hello World.\n");
   return 0;
}

2. 编写CMakeLists.txt文件,保存在main.c同路径下

#Minimum required CMake Version
cmake_minimum_required(VERSION 3.6.1)

#Project Name
project(hello)
#把当前目录(.)下所有源代码文件和头文件加入变量SRC_LIST
AUX_SOURCE_DIRECTORY(. SRC_LIST)
#生成应用程序hello(在windows下生成hello.exe)
ADD_EXECUTABLE(hello ${SRC_LIST})

3. 运行cmake命令生成MakeFile,再运行make命令生成hello可执行程序(为防止文件混乱,可建立build目录,在此目录下运行cmake命令)

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- 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: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mgh/桌面/cmake_test/test2/build
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ make
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/test1.c.o
[100%] Linking C executable hello
[100%] Built target hello
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ ./hello
Hello World.

二、多文件目录

1. 目录结构

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test$ tree test
test
├── CMakeLists.txt
├── print
│   ├── CMakeLists.txt
│   ├── print.c
│   └── print.h
└── test.c

2. 编辑C程序文件

test.c

#include  
#include "print/print.h"

int main(void) {
   print();
   return 0;
}

print.h

#ifndef PRINT_H
#define PRINT_H

extern void print();

#endif

print.c

#include  

extern void print(){
   printf("Hello World.\n");
}

3. 编辑CMakeLists.txt文件

这种多目录的情况,需要在每个源文件路径中分别编写CMakeLists.txt文件,对应这个例子,需要在test根目录和print目录下编写CMakeLists.txt文件。

为了方便,我们可以先将print目录里的文件编译成静态库再由main函数调用。

test目录下的CMakeLists.txt文件:

#Minimum required CMake Version
cmake_minimum_required(VERSION 3.6.1)

#Project Name
project(hello)

#当前目录下所有源文件保存到SRC_LIST中
AUX_SOURCE_DIRECTORY(. SRC_LIST)

#添加print子目录
add_subdirectory(print)

#指定生成目标
ADD_EXECUTABLE(hello ${SRC_LIST})

#添加链接库
target_link_libraries(hello printFunc)

print目录下的CMakeLists.txt文件:

#当前目录下所有源文件保存到SRC_LIST中
AUX_SOURCE_DIRECTORY(. SRC_LIB)

#生成链接库
ADD_LIBRARY(printFunc ${SRC_LIB})

4. 编译运行

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- 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: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mgh/桌面/cmake_test/test/build
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ make
Scanning dependencies of target printFunc
[ 25%] Building C object print/CMakeFiles/printFunc.dir/print.c.o
[ 50%] Linking C static library libprintFunc.a
[ 50%] Built target printFunc
Scanning dependencies of target hello
[ 75%] Building C object CMakeFiles/hello.dir/test.c.o
[100%] Linking C executable hello
[100%] Built target hello
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ ./hello
Hello World.

文章标题:Linux下CMake使用实例
当前路径:http://www.csdahua.cn/qtweb/news28/149278.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网