当前位置:网站首页>Basic use of cmake
Basic use of cmake
2022-07-27 11:21:00 【lqw198421】
Preface
The company currently uses cmake, I haven't used it before , Take advantage of the intermission of the project to study , Take this as a record ;
This is a targeted study , So it is realized through various examples ;
cmake Specific use
One cpp File compiles an executable
File directory

among build Is a directory , It's to put cmake The file generated in the compilation process is isolated from the source file , This is it. cmake Recommended external build patterns ;
The contents of the document
main.cpp
#include <stdlib.h>
#include <stdio.h>
int main() {
printf("cmake test\n");
return 1;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5) # Set up CMake Minimum version
project (cmake_test) # Set the project name
add_executable(${
PROJECT_NAME} main.cpp) # Generate executable files add_executable(cmake_test main.cpp)
compile
stay build Execute first under directory cmake3 …( From build Go to the upper directory of CMakeLists.txt) To generate MakeFile file , The file directory after executing the command is as follows :
And then build Execute under directory make command , For real compilation :
make After execution , Can be in build I see it under the directory cmake_test Generation of executable files , This completes the goal ;
In the following example , By default, they are all created under the project directory build Directory and generate MakeFile File and generate executable —— Unless there are complications later , For example, when it is generated under the specified directory ;
Multi level directory + Multiple cpp File generation executable file
File directory

You can see , Yes 2 individual cpp file (main.cpp + src/my_class_1.cpp) and 1 individual h file (include/my_class_1.h)
The contents of the document
include/my_class_1.h
#ifndef _MY_CLASS_1_H_
#define _MY_CLASS_1_H_
class My_Class_1 {
public:
My_Class_1();
~My_Class_1();
void class1_func();
};
#endif
src/my_class_1.cpp
#include <iostream>
#include "my_class_1.h"
My_Class_1::My_Class_1() {
std::cout << "My_Class_1::My_Class_1" << std::endl;
}
My_Class_1::~My_Class_1() {
std::cout << "My_Class_1::~My_Class_1" << std::endl;
}
void My_Class_1::class1_func() {
std::cout << "My_Class_1::class1_func==> " << std::endl;
}
main.cpp
#include <stdlib.h>
#include <stdio.h>
#include "my_class_1.h"
int main() {
printf("cmake test\n");
My_Class_1 my_class_1_obj;
my_class_1_obj.class1_func();
return 1;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5) # Set up CMake Minimum version
project (cmake_test) # Set the project name
set(SOURCES main.cpp src/my_class_1.cpp) # Create a variable , Name is SOURCE. It contains all of cpp file .
# If you want to put all of the current directory cpp File unified compilation , have access to : aux_source_directory(. SOURCES)
add_executable(${
PROJECT_NAME} ${
SOURCES}) # Generate an executable file with all the source files , Because it defines SOURCE Variable , So there is no need to list cpp The file
target_include_directories(${
PROJECT_NAME} PRIVATE ${
PROJECT_SOURCE_DIR}/include) # Set the header file directory that needs to be included to generate this executable
Multi level directory + Multiple cpp File generates dynamic library file and executable file respectively
File directory
Compared with the previous example , The file directory remains unchanged ;
The contents of the document
Compared with the previous example , have only CMakeLists.txt Has changed , Other unchanged items will not be explained ;
CMakeLists.txt
cmake_minimum_required(VERSION 3.5) # Set up CMake Minimum version
project (cmake_test) # Set the project name
#Create a library hold my_class_1.cpp Compile into a dynamic library ( The name is :libmyclass.so)
add_library(myclass SHARED src/my_class_1.cpp)
# Add the header file directory required for the compilation of this dynamic library
target_include_directories(myclass PUBLIC ${
PROJECT_SOURCE_DIR}/include)
# Generate executable files
add_executable(${
PROJECT_NAME} main.cpp)
# Link libraries and executables
target_link_libraries(${
PROJECT_NAME} PRIVATE myclass)
compile
To regenerate the MakeFile And compile , You can see in the build Generated in directory libmyclass.so Dynamic library ;
Generate dynamic library files and executable files in the specified directory
File directory
Compared with the previous example , The file directory remains unchanged ;
The contents of the document
Compared with the previous example , have only CMakeLists.txt Has changed , Other unchanged items will not be explained ;
CMakeLists.txt
cmake_minimum_required(VERSION 3.5) # Set up CMake Minimum version
# Set the project name
project (cmake_test)
# Set the output directory of the executable
SET(EXECUTABLE_OUTPUT_PATH ${
PROJECT_SOURCE_DIR}/bin)
# Set the output directory of the library file
SET(LIBRARY_OUTPUT_PATH ${
PROJECT_SOURCE_DIR}/lib)
# Create a library hold my_class_1.cpp Compile into a dynamic library ( The name is :libmyclass.so)
add_library(myclass SHARED src/my_class_1.cpp)
# Add the header file directory required for the compilation of this dynamic library
target_include_directories(myclass PUBLIC ${
PROJECT_SOURCE_DIR}/include)
# Generate executable files
add_executable(${
PROJECT_NAME} main.cpp)
# Set dynamic link library Directory
link_libraries(${
PROJECT_SOURCE_DIR}/lib)
# Link libraries and executables
target_link_libraries(${
PROJECT_NAME} PRIVATE myclass)
Compared with the previous example , It only specifies the directory for generating dynamic libraries and executable files : The dynamic library is in the project directory lib Under the table of contents , The executable file is in bin Under the table of contents ; The setting is realized through the following two sentences :
# Set the output directory of the executable
SET(EXECUTABLE_OUTPUT_PATH ${
PROJECT_SOURCE_DIR}/bin)
# Set the output directory of the library file
SET(LIBRARY_OUTPUT_PATH ${
PROJECT_SOURCE_DIR}/lib)
Reference resources
边栏推荐
- Game theory acwing 893. Set Nim game
- Longest ascending subsequence model acwing 482. Chorus formation
- A verification test of the relationship between iteration number and entropy
- Game theory acwing 892. Step Nim game
- Maximized array sum after 13 K negations
- 数字三角形模型 AcWing 1015. 摘花生
- 数字三角形模型 AcWing 1027. 方格取数
- 12 is at least twice the maximum number of other numbers
- JVM judges that the object is dead, and practices verify GC recycling
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
猜你喜欢

Gaussian elimination acwing 883. solving linear equations with Gaussian elimination

MySQL installation (RPM package)

Local and overall differences between emergence and morphology

Knapsack problem acwing 9. grouping knapsack problem

An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft

最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼

15 design movie rental system

Memory search acwing 901. Skiing

Backpack model acwing 1022. Collection of pet elves

Remember an experience of using canvas to make the banner streamer effect of Tencent cloud homepage
随机推荐
Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
求组合数 AcWing 886. 求组合数 II
15th largest value of data flow
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
tensorflow运行报错解决方法
Knapsack model acwing 1024. Packing problem
I've compromised. Since everyone wants to call me Yelin, there's nothing I can do
6 find the smallest letter larger than the target letter
求组合数 AcWing 885. 求组合数 I
What is the mystery of the gate of the meta universe?
最长上升子序列模型 AcWing 272. 最长公共上升子序列
49 letter ectopic grouping and 242 effective letter ectopic words
中国剩余定理 AcWing 204. 表达整数的奇怪方式
Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
Longest ascending subsequence model acwing 1010. Interceptor missile
数字三角形模型 AcWing 1027. 方格取数
Knapsack problem acwing 9. grouping knapsack problem
First experience of three.js: simulating the growth of a small branch
Digital triangle model acwing 1018. Minimum toll