cmake 相关

数字银月渡
• 阅读 1599

hello world

代码

main.c

#include<stdio.h>
int main(void){
    printf("hello world\n");
    return 0;
}

CMakeLists.txt

# 指定最小版本
cmake_minimum_required (VERSION 3.3)
# 指定工程名和语言类型
project (hello C)
# debug
set(CMAKE_BUILD_TYPE "Debug")
# c99
set(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]# make
Scanning 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)
# debug
set(CMAKE_BUILD_TYPE "Debug")
# c99
set(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 <stdio.h>

#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_H

int 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]# make
Scanning 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 = 3
Hello, World!
[root@cstudy build]# 
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
可莉 可莉
3年前
18个常用 webpack插件,总会有适合你的!
!(https://oscimg.oschina.net/oscnet/71317da0c57a8e8cf5011c00e302a914609.jpg)来源| https://github.com/Michaellzg/myarticle/blob/master/webpack/Plugin何为插
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这