当前位置:网站首页>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
边栏推荐
- 博弈论 AcWing 893. 集合-Nim游戏
- Longest ascending subsequence model acwing 1012. Sister Cities
- Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!
- 12 is at least twice the maximum number of other numbers
- What is the mystery of the gate of the meta universe?
- 最长上升子序列模型 AcWing 1016. 最大上升子序列和
- 博弈论 AcWing 892. 台阶-Nim游戏
- Ansible
- Find the combination number acwing 887. find the combination number III
- 高斯消元 AcWing 883. 高斯消元解线性方程组
猜你喜欢

Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?

博弈论 AcWing 893. 集合-Nim游戏

Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?

数字三角形模型 AcWing 1015. 摘花生

FAQs of "relay chain" and "dot" in Poka ecosystem

最长上升子序列模型 AcWing 1010. 拦截导弹

最长上升子序列模型 AcWing 1014. 登山

Game theory acwing 892. Step Nim game

Game theory acwing 891. Nim game

What is the mystery of the gate of the meta universe?
随机推荐
Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
Digital triangle model acwing 275. pass note
Students, don't copy all my code, remember to change it, or we both want G
Play with the cluster configuration center and learn about the Taier console
Miscellaneous records of Finance
求组合数 AcWing 889. 满足条件的01序列
最长上升子序列模型 AcWing 1014. 登山
博弈论 AcWing 894. 拆分-Nim游戏
博弈论 AcWing 892. 台阶-Nim游戏
Description and feelings
Longest ascending subsequence model acwing 482. Chorus formation
力扣——10. 正则表达式匹配
Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
ACM warm-up Exercise 2 in 2022 summer vacation (summary)
求组合数 AcWing 885. 求组合数 I
Derive the detailed expansion of STO double center kinetic energy integral
Digital triangle model acwing 1018. Minimum toll
tensorflow运行报错解决方法
15 design movie rental system
14 check whether integers and their multiples exist