设置交叉编译工具
https://blog.csdn.net/bytxl/article/details/50635788

https://www.hahack.com/codes/cmake/
CMake 入门实战 _ HaHack.pdf
在cmake的时候,最常见的几个步骤就是:
mkdir build && cd build cmake .. make make install
cmake -DCMAKE_TOOLCHAIN_FILE=~/workspace/toradex-toolchain-v2.6.cmake .. && make

install

install用于指定在安装时运行的规则。它可以用来安装很多内容,可以包括目标二进制、动态库、静态库以及文件、目录、脚本等:
变量CMAKE_INSTALL_PREFIX,用于指定cmake install时的相对地址前缀。用法如:
cmake -DCMAKE_INSTALL_PREFIX=/usr ..

CMake 教程

===================================
eg. CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
SET(CMAKE_SYSTEM_NAME Linux)
SET(TOOLCHAIN_DIR “/home/neethan/workspace/toolchains/arm-linux-gcc-4.3.2”)
SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-g++)
SET(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_DIR}
${TOOLCHAIN_DIR}/arm-none-linux-gnueabi/libc/usr/include
${TOOLCHAIN_DIR}/arm-none-linux-gnueabi/lib )
set(LINK_DIR ${TOOLCHAIN_DIR}/arm-none-linux-gnueabi/lib)
link_directories(${LINK_DIR})
project(clion C)
set(CMAKE_C_STANDARD 99)
add_executable(jpg2rgb jpg2rgb.c)
# 链接动态库
target_link_libraries(jpg2rgb libjpeg.so)
# 静态链接库
# TARGET_LINK_LIBRARIES(jpg2rgb libhello.a)
==================================================

4. 指定编译器和编译选项

1.CMAKE_C_COMPILER: 指定C编译器
2.CMAKE_CXX_COMPILTER:指定C++编译器
3.CMAKE_C_FLAGS: 指定C编译选项
4.CMAKE_CXX_FLAGS:指定C++编译选项
5.EXECUTABLE_OUTPUT_PATH: 指定执行文件存放目录
6.LIBRARY_OUTPUT_PATH: 指定库文件存放目录
7.CMAKE_BUILD_TYPE:指定build类型[Debug|Release]
8.BUILD_SHARED_LIBS: 指定默认库编译方式[OFF|ON]
上述内部变量使用说明:
1.CMakeLists.txt文件上使用set命令
2.cmake 命令中指定,如: cmake -DCMAKE_C_COMPILER=gcc
add_definitions:添加编译参数
5. 配置编译模块
6. CMake 常用变量和语句
1.include_directories:指定头文件搜索路径
2.link_directories:指定库文件搜索路径
3.add_subdirectory:添加子目录
4.target_link_libraries:指定文件链接库文件

1. 编译单目录工程

1.创建工程文件夹
mkdir hello #工程目录 cd hello mkdir src # 存放源代码的目录 mkdir build # 存放编译中间代码和目标代码的目录
2.进入src目录,编写一个main.c文件
#includeintmain(int argc, char**argv) { printf(“hello world\n”); return0; }
3.编写工程顶层目录的CMakeLists.txt
cmake_minumum_required(VERSION 2.6) #指定项目名 project(hello) #指定子目录 add_subdirectory(src)
4.编写子目录src的CMakeLists.txt
aux_source_directory(. SRC_LIST) add_executable(hello ${SRC_LIST})
5.编译工程
1.进入build 目录
2.执行命令cmake ..创建Makefile
3.执行命令make编译工程
4.在build的子目录src生成了执行文件
2. 编译多目录工程
1.创建工程目录
mkdir hello # 工程目录 cd hello mkdir src # 存放源码目录 mkdir build # 存放编译产生的中间文件 cd src mkdir hello # 存放hello 模块 mkdir world # 存放world 模块
2.编写hello模块

  • 进入hello目录
  • 编写hello.h文件

ifndef HELLO_H#define HELLO_HvoidHello_Print(void); #endif

  • 编写hello.c文件

include”hello.h”#includevoidHello_Print(void) { printf(“hello “); }

  • 编写CMakeLists.txt 文件

aux_source_directory(. DIR_HELLO_SRC) add_library(hello_lib ${DIR_HELLO_SRC})
3.编写world模块

  • 进入world目录
  • 编写world.h文件

ifndef WORLD_H#define WORLD_HvoidWorld_Print(void); #endif

  • 编写world.c文件

include”world.h”#includevoidWorld_Print(void) { printf(“world”); }

  • 编写CMakeLists.txt 文件

aux_source_directory(. DIR_WORLD_SRC) add_library(world_lib ${DIR_WORLD_SRC})
4.编写主模块

  • 进入src目录
  • 编写main.c 文件

include”hello/hello.h”#include”world/world.h”intmain(int argc, char**argv) { Hello_Print(); World_Print(); return0}

  • 编写CMakeLists.txt 文件

add_source_directory(. DIR_SRC) # 添加子目录 add_subdirectory(hello) add_subdirectory(world) # 执行文件 add_executable(hello_prj ${DIR_SRC}) target_link_libraries(hello_prj ello_lib world_lib)
5.编写顶层目录的CMakeLists.txt文件
cmake_minumum_required(VERSION 2.6) project(hello_prj) add_subdirectory(src)

3. 动态库和静态库的构建和使用

1.使用一个hello world工程来展开说明
项目结构
|—CMakeLists.txt|— build|— include||— hello|| -- hello.h|— world| -- world.h|-- src||--CMakeLists.txt||-- hello||— hello.c| -- world|— world.c -- test |--CMakeLists.txt— mytest.c
2.顶层目录CMakeLists.txt
cmake_minimum_required(VERSION 2.6) project(helloworld) #设置库文件存放路径 set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/lib) #设置执行文件存放路径 set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/bin) #获取当前目录及子目录(递归获取),添加到头文件搜索路径 function(include_sub_directories_recursively root_dir) if (IS_DIRECTORY ${root_dir}) # 当前路径是一个目录吗,是的话就加入到包含目录 message(“include dir: “ ${root_dir}) include_directories(${root_dir}) endif() file(GLOB ALL_SUB RELATIVE ${root_dir} ${root_dir}/) # 获得当前目录下的所有文件,让如ALL_SUB列表中 foreach(sub ${ALL_SUB}) if (IS_DIRECTORY ${root_dir}/${sub}) include_sub_directories_recursively(${root_dir}/${sub}) # 对子目录递归调用,包含 endif() endforeach() endfunction() #项目的所有目录都为头文件搜索路径 include_sub_directories_recursively(${PROJECT_SOURCE_DIR}) #添加库文件搜索路径 link_directories( ${PROJECT_SOURCE_DIR}/build/lib ) #添加子目录 add_subdirectory(src) add_subdirectory(test) #设置安装目录 set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install)
3.helloworld库的源代码
hello.h文件
#ifndef HELLO_H#define HELLO_HvoidHello_Print(void); #endif
hello.c文件
#include”hello/hello.h”#includevoidHello_Print(void) { printf(“hello “); }
world.h文件
#ifndef WORLD_H#define WORLD_HvoidWorld_Print(void); #endif
world.c文件
#include”world/world.h”#includevoidWorld_Print(void) { printf(“world”); }
4.子目录src下的CMakeLists.txt
#递归获取当前目录及子目录下的所有c文件 file(GLOB_RECURSE c_files “
.c”) #生成动态库和静态库 add_library(helloworld_lib_shared SHARED ${c_files}) add_library(helloworld_lib_static STATIC ${c_files}) #将动态库和静态库的名字设置为一致 set_target_properties(helloworld_lib_shared PROPERTIESOUTPUT_NAME”helloworld”) set_target_properties(helloworld_lib_static PROPERTIESOUTPUT_NAME”helloworld”) #设置动态库版本 set_target_properties(helloworld_lib_shared PROPERTIESVERSION1.2SOVERSION1) #安装动态库和静态库INSTALL(TARGETS helloworld_lib_shared helloworld_lib_static LIBRARYDESTINATION lib ARCHIVEDESTINATION lib) #安装头文件INSTALL(DIRECTORY ${PROJECT_SOURCE_DIR}/include/DESTINATION include)
5.mytest.c文件测试生成的库文件
mytest.c文件

  1. #include "hello/hello.h"
  2. #include "world/world.h"
  3. #include <stdio.h>
  4. int main(int argc, char **argv)
  5. {
  6. Hello_Print();
  7. World_Print();
  8. printf("\n");
  9. return 0;
  10. }

CMakeLists.txt文件
#递归获取所有当前目录及子目录下的C文件
file(GLOB_RECURSE c_files ./*.c)

生成执行文件
add_executable(mytest ${c_files})

链接外部库
target_link_libraries(mytest libhelloworld.so)

6.构建工程
1.进入目录build
2.执行命令: cmake ..
3.执行命令: make
4.执行命令: make install

CMake(一)引用.so动态链接库
我的文件结构如下
project_test
|
——lib——liba.so
|
——include——xxx.h
|
——src——hello.c
|
——bin
这里贴上简短的cmake内容。
#cmake min version
cmake_minimum_required (VERSION 3.5)
#project
nameproject (test)
#set output path
set (EXECUTABLE_OUTPUT_PATH ../bin)
#header
filesinclude_directories(./include)#source fileadd_executable (test ./src/hello.c)
#link share lib
target_link_libraries (test /home/user1/Documents/project/project_test/lib/liba.so)
注意:括号外关键字大小写无差异,括号内关键字为大写。
注意:target_link_librariesz中路径建议写全
Clion编译器中 建立多目录工程的 CMakeLists写法
1、头文件 和 库文件 分目录放置
image.jpeg

  1. cmake_minimum_required(VERSION 3.15)
  2. project(CompanySystem)
  3. # Add header file include directories
  4. include_directories(./include)
  5. # Add block directories
  6. set(SRC src/workerManger.cpp src/employee.cpp src/manager.cpp src/boss.cpp)
  7. # Target
  8. add_executable(CompanySystem main.cpp ${SRC} )

2、头文件与其对应源文件放置到同一个目录下
image.jpeg

  1. cmake_minimum_required(VERSION 3.15)
  2. project(untitled1)
  3. # Add header file include
  4. directoriesinclude_directories(./employee ./manager ./worker ./workerManager ./boss)
  5. # Add block
  6. directoriesset(BOSS_SRC boss/boss.cpp)
  7. set(EMPLOYEE_SRC employee/employee.cpp)
  8. set(MANAGER_SRC manager/manager.cpp)
  9. set(WM_SRC workerManager/workerManager.cpp)
  10. # Target
  11. add_executable(untitled1 main.cpp ${BOSS_SRC} ${EMPLOYEE_SRC} ${MANAGER_SRC} ${WM_SRC})