当前位置:网站首页>Supplement 14: cmake practice project notes (to be continued 4 / 22)
Supplement 14: cmake practice project notes (to be continued 4 / 22)
2022-04-23 04:35:00 【Xie Baiyu】
List of articles
-
- One 、 install cmake
- Two 、 project
-
- 1) A single file directory implements
- 2) Improve the engineering structure
- 3) Practice compiling multiple directories ( Library file generation call usage )
- 4) Practice compiling multiple directories ( Traverse the source file and load )
- 5) Generate dynamic library
- 6) Generate static libraries + Install to specified directory
- 7) Call the static library
- 8) Call dynamic library
- 9) compile debug edition release edition
- 10)
One 、 install cmake
1) Uninstall old version cmake
apt-get autoremove cmake
2) Download and unzip the file :
https://cmake.org/files/v3.9/cmake-3.9.1-Linux-x86_64.tar.gz
tar zxvf cmake-3.9.1-Linux-x86_64.tar.gz
3) View directory
tree -L 2 cmake-3.9.1-Linux-x86_64
2 cmake-3.9.1-Linux-x86_64
3 ├── bin
4 │ ├── ccmake
5 │ ├── cmake
6 │ ├── cmake-gui
7 │ ├── cpack
8 │ └── ctest
9 ├── doc
10 │ └── cmake
11 ├── man
12 │ ├── man1
13 │ └── man7
14 └── share
15 ├── aclocal
16 ├── applications
17 ├── cmake-3.9
18 ├── icons
19 └── mime
20 12 directories, 5 files
4) Create soft link
notes : The file path can be specified , General choice in /opt or /usr Under the path , Choose here /opt
1 mv cmake-3.9.1-Linux-x86_64 /opt/cmake-3.9.1
2 ln -sf /opt/cmake-3.9.1/bin/* /usr/bin/
Two 、 project
1) A single file directory implements
(1) Basic engineering
1 # A single directory implements
2 # CMake Minimum version number requirement
3 cmake_minimum_required (VERSION 2.8)
# Specify project name , It can be different from binary names , Multiple executable files can be generated
PROJECT(0VOICE)
4 # Manually fill in the document
5 SET(SRC_LIST main.c)
SET(SRC_LIST2 main2.c)
6 MESSAGE(STATUS "THIS IS BINARY DIR " ${PROJECT_BINARY_DIR})
7 MESSAGE(STATUS "THIS IS SOURCE DIR " ${PROJECT_SOURCE_DIR})
8 ADD_EXECUTABLE(0voice ${SRC_LIST}) // The execution file is called 0voice
ADD_EXECUTABLE(0voice2 ${SRC_LIST2})
(2) Catalog presentation
cmakeLists.txt And main.c In the same directory
(3) Code
main.c
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("0voice2 CMake!!!\n");
return 0;
}
main2.c
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("0voice CMake!!!\n");
return 0;
}
(4) Instructions
cmake . //. The position of indicates cmakelists.txt The location of , Good configuration
make // compile
./ Executable files // perform
2) Improve the engineering structure
(1) Basic engineering
- Specify the installation directory
cmake -DCMAKE_INSTALL_PREFIX=/tmp/usr … ( The default is to install to /usr/loacl/)
- Home directory cmake
# CMake Minimum version number requirement
cmake_minimum_required (VERSION 2.8)
PROJECT(0VOICE)
# Add subdirectories
ADD_SUBDIRECTORY(src)
#INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/0voice)
# install doc To share/doc/cmake/0voice Catalog
# Default /usr/local/
# Specify custom Directory , such as cmake -DCMAKE_INSTALL_PREFIX=/tmp/usr ..
INSTALL(DIRECTORY doc/ DESTINATION share/doc/cmake/0voice)

- src The directory cmake
# A single directory implements
# CMake Minimum version number requirement
cmake_minimum_required (VERSION 2.8)
# engineering
# PROJECT(0VOICE)
# Add files manually
SET(SRC_LIST main.c)
MESSAGE(STATUS "THIS IS BINARY DIR " ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "THIS IS SOURCE DIR " ${PROJECT_SOURCE_DIR})
ADD_EXECUTABLE(youzi ${SRC_LIST})
# Install the executable to bin Catalog
# Default /usr/local/
# Specify custom Directory , such as cmake -DCMAKE_INSTALL_PREFIX=/tmp/usr ..
INSTALL(TARGETS youzi RUNTIME DESTINATION bin)
(2) Catalog presentation
1 .
2 ├── build
3 ├── CMakeLists.txt
4 ├── doc
5 │ ├── darren.txt
6 │ └── README.MD
7 └── src
8 ├── CMakeLists.txt
9 └── main.c
(3) Code
main.c
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("0voice CMake!!!\n");
return 0;
}
(4) Instructions
cmake -DCMAKE_INSTALL_PREFIX=/tmp/usr ..
make install
cd xxx
./ Executable files
3) Practice compiling multiple directories ( Library file generation call usage )
(1) Basic engineering
(2) Catalog presentation
- Subdirectories are compiled into library files
1 ├── CMakeLists.txt
2 ├── doc
3 │ ├── darren.txt
4 │ └── README.MD
5 └── src
6 ├── CMakeLists.txt
7 ├── dir1
8 │ ├── CMakeLists.txt
9 │ ├── dir1.c
10 │ └── dir1.h
11 ├── dir2
12 │ ├── CMakeLists.txt
13 │ ├── dir2.c
14 │ └── dir2.h
15 └── main.c
(3) Code
- Outermost cmake
# CMake Minimum version number requirement
cmake_minimum_required (VERSION 2.8)
PROJECT(0VOICE)
# Add subdirectories
ADD_SUBDIRECTORY(src bin)
#INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/0voice)
INSTALL(DIRECTORY doc/ DESTINATION share/doc/cmake/0voice)
- src Of cmake
# A single directory implements
# CMake Minimum version number requirement
cmake_minimum_required (VERSION 2.8)
# engineering
PROJECT(0VOICE)
# Add files manually
SET(SRC_LIST main.c)
MESSAGE(STATUS "THIS IS BINARY DIR " ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "THIS IS SOURCE DIR " ${PROJECT_SOURCE_DIR})
# Add the header file path
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/dir1")
INCLUDE_DIRECTORIES(dir1) # Both of them can be written
MESSAGE(STATUS "CMAKE_CURRENT_SOURCE_DIR -> " ${CMAKE_CURRENT_SOURCE_DIR})
# add to dir1 subdirectories
ADD_SUBDIRECTORY("${CMAKE_CURRENT_SOURCE_DIR}/dir1")
# Add the header file path
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/dir2")
# add to dir2 subdirectories
ADD_SUBDIRECTORY("${CMAKE_CURRENT_SOURCE_DIR}/dir2")
# This project will generate a file named [BINARY] Executable file , The relevant source file is SOURCE_LIST in
List of source files defined
ADD_EXECUTABLE(darren ${SRC_LIST} )
# Link library to executable file
TARGET_LINK_LIBRARIES(darren dir1 dir2)
# Install the executable to bin Catalog
INSTALL(TARGETS darren RUNTIME DESTINATION bin)
- Of subdirectories cmake
# Load all the source code , and makefile wildcard similar , Want to be SET (DIR_SRCS dir1.c)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
# SET(DIR_SRCS dir1.c dir12.c)
# The default is static library ,SHARED Is to generate dynamic library
ADD_LIBRARY (dir1 SHARED ${DIR_SRCS})
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_LIBRARY (dir2 ${DIR_SRCS})
(4) Instructions
mkdir build
cd build
cmake ..
make
4) Practice compiling multiple directories ( Traverse the source file and load )
(1) Basic engineering
- Home directory cmake
# CMake Minimum version number requirement
cmake_minimum_required (VERSION 2.8)
PROJECT(0VOICE)
ADD_SUBDIRECTORY(src bin)
MESSAGE(STATUS "TOP CMAKE_CURRENT_SOURCE_DIR -> " ${CMAKE_CURRENT_SOURCE_DIR})
#INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/0voice)
INSTALL(DIRECTORY doc/ DESTINATION share/doc/cmake/0voice)
- src Of cmake
# A single directory implements
# CMake Minimum version number requirement
cmake_minimum_required (VERSION 2.8)
# engineering
# PROJECT(0VOICE)
# Add files manually
SET(SRC_LIST main.c)
MESSAGE(STATUS "THIS IS BINARY DIR " ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "THIS IS SOURCE DIR " ${PROJECT_SOURCE_DIR})
# Set subdirectories
set(SUB_DIR_LIST "${CMAKE_CURRENT_SOURCE_DIR}/dir1" "${CMAKE_CURRENT_SOURCE_DIR}/dir2")
foreach(SUB_DIR ${SUB_DIR_LIST})
# Traverse the source file
aux_source_directory(${SUB_DIR} SRC_LIST)
MESSAGE(STATUS "SUB_DIR-> " ${SUB_DIR})
MESSAGE(STATUS "SRC_LIST-> " ${SRC_LIST})
endforeach()
# Add the header file path
INCLUDE_DIRECTORIES("dir1")
INCLUDE_DIRECTORIES("dir2")
(2) Catalog presentation
1 .
2 ├── CMakeLists.txt
3 ├── doc
4 │ ├── darren.txt
5 │ └── README.MD
6 └── src
7 ├── CMakeLists.txt
8 ├── dir1
9 │ ├── dir1.c
10 │ └── dir1.h
11 ├── dir2
12 │ ├── dir2.c
13 │ └── dir2.h
14 └── main.c
(3) Code
(4) Instructions
mkdir build
cd build
cmake ..
make
5) Generate dynamic library
(1) Basic engineering
# Set up release Version or debug edition
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
MESSAGE(STATUS "Release edition ")
SET(BuildType "Release")
else()
SET(BuildType "Debug")
MESSAGE(STATUS "Debug edition ")
endif()
# Set up lib The library catalog
#PROJECT_SOURCE_DIR Follow the list of recent projects
SET(RELEASE_DIR ${PROJECT_SOURCE_DIR}/release)
# debug and release The version directory is different
# Set the generated so The path of the last output of the dynamic library
SET(LIBRARY_OUTPUT_PATH ${RELEASE_DIR}/linux/${BuildType})
ADD_COMPILE_OPTIONS(-fPIC)
# Find all source files in the current directory
# And save the name to DIR_LIB_SRCS Variable
AUX_SOURCE_DIRECTORY(. DIR_LIB_SRCS)
# Generate static library link library Dir1
#ADD_LIBRARY (Dir1 ${DIR_LIB_SRCS})
# Generate dynamic library
ADD_LIBRARY (Dir1 SHARED ${DIR_LIB_SRCS})
(2) Catalog presentation




(3) Code
(4) Instructions
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug .. ( compile debug edition , compile release Version on =Release)
make
- explain :
debug Version will use parameters -g;
release Version will use -O3 -DNDEBUG
6) Generate static libraries + Install to specified directory
(1) Basic engineering
# Set up release Version or debug edition
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
MESSAGE(STATUS "Release edition ")
SET(BuildType "Release")
else()
SET(BuildType "Debug")
MESSAGE(STATUS "Debug edition ")
endif()
# Set up lib The library catalog
SET(RELEASE_DIR ${PROJECT_SOURCE_DIR}/release)
# debug and release The version directory is different
# Set the generated so The path of the last output of the dynamic library
SET(LIBRARY_OUTPUT_PATH ${RELEASE_DIR}/linux/${BuildType})
ADD_COMPILE_OPTIONS(-fPIC)
# Find all source files in the current directory
# And save the name to DIR_LIB_SRCS Variable
AUX_SOURCE_DIRECTORY(. DIR_LIB_SRCS)
# Generate static library link library Dir1
ADD_LIBRARY (Dir1 ${DIR_LIB_SRCS})
# Install the library files to lib Catalog
INSTALL(TARGETS Dir1 DESTINATION lib)
# Add header file to include
INSTALL(FILES dir1.h DESTINATION include)
#INSTALL(FILES *.h DESTINATION include)
(2) Catalog presentation

(3) Code
(4) Instructions
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/tmp/usr ..
make
make install
make clean

7) Call the static library
(1) Basic engineering
# A single directory implements
# CMake Minimum version number requirement
cmake_minimum_required (VERSION 2.8)
# engineering
PROJECT(0VOICE)
# Add files manually
SET(SRC_LIST main.c)
MESSAGE(STATUS "THIS IS BINARY DIR " ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "THIS IS SOURCE DIR " ${PROJECT_SOURCE_DIR})
# Header file
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/lib")
# Specify the path to the library
LINK_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/lib")
# Reference dynamic library
ADD_EXECUTABLE(darren ${SRC_LIST})
# Link library files
TARGET_LINK_LIBRARIES(darren Dir1)
(2) Catalog presentation

(3) Code
(4) Instructions
8) Call dynamic library
(1) Basic engineering
# A single directory implements
# CMake Minimum version number requirement
cmake_minimum_required (VERSION 2.8)
# engineering
PROJECT(0VOICE)
# Add files manually
SET(SRC_LIST main.c)
MESSAGE(STATUS "THIS IS BINARY DIR " ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "THIS IS SOURCE DIR " ${PROJECT_SOURCE_DIR})
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/lib")
LINK_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/lib")
# Reference dynamic library
ADD_EXECUTABLE(darren ${SRC_LIST})
# Connect dynamic libraries first
#TARGET_LINK_LIBRARIES(darren Dir1)
# Force the use of static libraries Full library file name libDir1.a
TARGET_LINK_LIBRARIES(darren libDir1.a)
(2) Catalog presentation ( Default priority link dynamic library ,ldd daren Go and see )

(3) Code
(4) Instructions
9) compile debug edition release edition
(1) Basic engineering
(2) Catalog presentation
1 .
2 ├── CMakeLists.txt
3 ├── doc
4 │ ├── darren.txt
5 │ └── README.md
6 ├── release
7 │ └── linux
8 │ ├── Debug
9 │ └── Release
10 └── src
11 ├── CMakeLists.txt
12 ├── dir1
13 │ ├── CMakeLists.txt
14 │ ├── dir1.c
15 │ └── dir1.h
16 ├── dir2
17 │ ├── CMakeLists.txt
18 │ ├── dir2.c
19 │ └── dir2.h
20 ├── main.c
21 ├── Makefile
22 ├── README.md
23 └── release
24 └── linux
(3) Code
(4) Instructions
compile debug edition
cmake -DCMAKE_INSTALL_PREFIX=/tmp/usr ..
make install
//
compile release edition
cmake -DCMAKE_BUILD_TYPE=Release ..
10)
(1) Basic engineering
(2) Catalog presentation
(3) Code
(4) Instructions
01 55 36
版权声明
本文为[Xie Baiyu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230431314604.html
边栏推荐
- Phishing for NFT
- [BIM introduction practice] Revit building wall: detailed picture and text explanation of structure, envelope and lamination
- The perfect combination of collaborative process and multi process
- MATLAB lit plusieurs diagrammes fig et les combine en un seul diagramme (sous forme de sous - Diagramme)
- C语言: 指针的进阶
- 华为机试--高精度整数加法
- [echart] démarrer avec echart
- 优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
- [echart] Introduction to echart
- 【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解
猜你喜欢

補:注解(Annotation)

QtSpim手册-中文翻译
![[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022](/img/eb/916a3fc1a89356fd8e74b943172ac3.png)
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022

洛谷P1858 【多人背包】 (背包求前k优解)

STM32F4单片机ADC采样及ARM-DSP库的FFT

指纹Key全国产化电子元件推荐方案

STM32单片机ADC规则组多通道转换-DMA模式

Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
![[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022](/img/40/72fdf9c89ed7d063cc368e6e052d0f.png)
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022

Bridge between ischemic stroke and intestinal flora: short chain fatty acids
随机推荐
SQL statement for adding columns in MySQL table
229. 求众数 II
Mysql---数据读写分离、多实例
Basic use of shell WC (counting the number of characters)
C语言 字符常量
阿里云IoT流转到postgresql数据库方案
thymeleaf th:value 为null时报错问题
Phishing for NFT
【BIM入门实战】Revit中的墙体层次以及常见问题解答
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
STM32上μC/Shell移植与应用
Differences among electric drill, electric hammer and electric pick
Understand the gut organ axis, good gut and good health
Supplément: annotation
Installation du compilateur croisé de la plateforme zynq
减治思想——二分查找详细总结
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
mysql table 中增加列的SQL语句
Set classic topics
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022