CMakeLists.txt V2.0
最简单例子
main.c
#include <stdio.h>
int main()
{
printf("Hello World Test!\n");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(MAIN)
message(123)
message("123e2323423")
add_executable(main main.c)
指定文件列表,通过文件列表加载编译
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(MAIN)
set(SRC_LIST
main.c
test.c)
add_executable(main ${SRC_LIST})
通过指定目录来加载编译
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(MAIN)
aux_source_directory(. SRC_LIST)
add_executable(main ${SRC_LIST})
分include src目录
.
├── build
├── CMakeLists_A.txt
├── CMakeLists.txt
├── include
│ └── test.h
└── src
├── main.c
└── test.c
test.h
#include <stdio.h>
int test();
test.c
#include "test.h"
int test()
{
printf("test hello world...\n");
return 0;
}
main.c
#include <stdio.h>
#include "test.h"
int main()
{
printf("Hello World Test!\n");
test();
return 0;
}
CMakeLists.txt
通过include src标准目录来加载编译
# 1.cmake verson,指定cmake版本
cmake_minimum_required(VERSION 3.4.1)
# 2.project name,指定项目的名称,一般和项目的文件夹名称对应
PROJECT(helloworld)
# 3.head file path,头文件目录
INCLUDE_DIRECTORIES(include)
# 4.source directory,源文件目录
AUX_SOURCE_DIRECTORY(src DIR_SRCS)
# 5.set environment variable,设置环境变量,编译用到的源文件全部都要放到这里,否则编译能够通过,但是执行的时候会出现各种问题,比如"symbol lookup error xxxxx , undefined symbol"
SET(TEST_MATH ${DIR_SRCS})
# 6.add executable file,添加要编译的可执行文件
ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_MATH})
# 7.add link library,添加可执行文件所需要的库,比如我们用到了libm.so(命名规则:lib+name+.so),就添加该库的名称
TARGET_LINK_LIBRARIES(${PROJECT_NAME} m)
./helloworld
Hello World Test!
test hello world...
多个模块加载编译
.
├── build
├── CMakeLists_A.txt
├── CMakeLists.txt
├── include
│ └── main.h
├── src
│ └── main.c
├── test1
│ ├── test1.c
│ └── test1.h
└── test2
├── test2.c
└── test2.h
test1.c
#include "test1.h"
int test1()
{
printf("test1 hello world...\n");
return 0;
}
test2.c
#include "test2.h"
int test2()
{
printf("test2 hello world...\n");
return 0;
}
main.c
#include <stdio.h>
#include "test1.h"
#include "test2.h"
int main()
{
printf("Hello World Test!\n");
test1();
test2();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(main)
include_directories(include test1 test2)
aux_source_directory(src SRC_LIST0)
aux_source_directory(test1 SRC_LIST1)
aux_source_directory(test2 SRC_LIST2)
add_executable(main ${SRC_LIST0} ${SRC_LIST1} ${SRC_LIST2})
CMakeLists.txt 添加arm-gcc
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(tools /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu)
set(CMAKE_C_COMPILER ${tools}/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/aarch64-linux-gnu-g++)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
这篇博客介绍了如何使用CMake进行项目管理,从简单的单文件编译到包括多个源文件目录的多模块加载,再到添加arm-gcc进行交叉编译。内容涵盖CMakeLists.txt的编写,包括指定头文件路径、添加源文件、设置环境变量和链接库等关键步骤,适合CMake初学者和嵌入式开发者参考。
3012

被折叠的 条评论
为什么被折叠?



