博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cmake 相关
阅读量:7173 次
发布时间:2019-06-29

本文共 2947 字,大约阅读时间需要 9 分钟。

hello world

代码

main.c

#include
int main(void){ printf("hello world\n"); return 0;}

CMakeLists.txt

# 指定最小版本cmake_minimum_required (VERSION 3.3)# 指定工程名和语言类型project (hello C)# debugset(CMAKE_BUILD_TYPE "Debug")# c99set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -g")# 指定源码列表aux_source_directory(. SRCS)# 指定可执行程序add_executable(hello ${SRCS})

运行

[root@cstudy hello]# mkdir build[root@cstudy hello]# cd build/[root@cstudy build]# cmake ..-- The C compiler identification is GNU 4.8.5-- 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-- Configuring done-- Generating done-- Build files have been written to: /root/code/cmake/hello/build[root@cstudy build]# makeScanning dependencies of target hello[ 50%] Building C object CMakeFiles/hello.dir/main.c.o[100%] Linking C executable hello[100%] Built target hello[root@cstudy build]# ./hello hello world[root@cstudy build]#

添加子目录

目录结构

├── CMakeLists.txt├── include│   └── util.h└── src    ├── CMakeLists.txt    ├── main.c    └── util.c

顶层 CMakeLists.txt

# 指定最小版本cmake_minimum_required (VERSION 3.3)# 指定工程名和语言类型project (hello C)# debugset(CMAKE_BUILD_TYPE "Debug")# c99set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -g")# 添加子目录add_subdirectory(src)

子目录 CMakeLists.txt

# 头文件目录include_directories(${PROJECT_SOURCE_DIR}/include)# 源文件列表aux_source_directory(. SRCS)# 可执行文件目录set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)# 可执行文件add_executable(hello ${SRCS})

代码

[root@cstudy hello]# # main.c[root@cstudy hello]# cat src/main.c #include 
#include "../include/util.h"int main() { int a = 1; int b = 2; printf("%d + %d = %d\n",a,b,sum(a,b)); printf("Hello, World!\n"); return 0;}[root@cstudy hello]# # util.h[root@cstudy hello]# cat include/util.h #ifndef HELLO_UTIL_H#define HELLO_UTIL_Hint sum(int a, int b );#endif //HELLO_UTIL_H[root@cstudy hello]# # util.c[root@cstudy hello]# cat src/util.c #include "../include/util.h"int sum(int a, int b ){ return a + b;}[root@cstudy hello]#

运行

[root@cstudy hello]# mkdir build[root@cstudy hello]# cd build[root@cstudy build]# cmake ..-- The C compiler identification is GNU 4.8.5-- 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-- Configuring done-- Generating done-- Build files have been written to: /root/code/cmake/hello/build[root@cstudy build]# makeScanning dependencies of target hello[ 33%] Building C object src/CMakeFiles/hello.dir/util.c.o[ 66%] Building C object src/CMakeFiles/hello.dir/main.c.o[100%] Linking C executable ../bin/hello[100%] Built target hello[root@cstudy build]# ./bin/hello 1 + 2 = 3Hello, World![root@cstudy build]#

转载地址:http://jwdzm.baihongyu.com/

你可能感兴趣的文章
[ASP.NET MVC]视图是如何呈现的 (续)
查看>>
GMF Q&A(1): 如何让palette支持拖拽(DnD)等10则
查看>>
架构总结图
查看>>
AngularJS中实现Model缓存
查看>>
【学习Android NDK开发】Android.mk文件
查看>>
Windows CE嵌入式系统程序开发
查看>>
建立一个使用.Net 2.0 MemberShip功能的标准例程(一)引子+预告 —— 这回不挖大坑了 保证填满...
查看>>
Winform 三层架构例子(3)--利用资源文件实现多国语言(含源代码)
查看>>
一起谈.NET技术,Linq To SQL 批量更新方法汇总
查看>>
一起谈.NET技术,走向ASP.NET架构设计——第五章:业务层模式,原则,实践(后篇)...
查看>>
PCI DSS 2.0标准出炉 未涉及移动支付技术
查看>>
分享Silverlight/WPF/Windows Phone一周学习导读(11月14日-11月20日)
查看>>
XAML实例教程系列 - 标记扩展(Markup Extensions)
查看>>
osg场景图(DAG-有向无环图)
查看>>
自绘控件的4种方法
查看>>
用HTML5实现手机摇一摇的功能
查看>>
linux-vim -ctags-Tlist-winmanager [转]
查看>>
黄聪:C#超级延时方法,延迟系统时间但系统又能同时能执行其它任务
查看>>
嵌入式 H264中的SPS、PPS提取与作用
查看>>
NodeJs+http+fs+request+cheerio 采集,保存数据,并在网页上展示(构建web服务器)
查看>>