cmake 相关

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了cmake 相关脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

hello world

代码

main.c

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

@R_223_1512@Lists.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
│&nbsp;  └── 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})
@H_777_263@代码
[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 = %dn",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]# 

脚本宝典总结

以上是脚本宝典为你收集整理的cmake 相关全部内容,希望文章能够帮你解决cmake 相关所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。