当前位置:网站首页>Get started quickly cmake
Get started quickly cmake
2022-06-13 01:46:00 【No change of name】
Use Cmake It is convenient for us to configure C++ file , The efficiency of compilation is greatly improved , but Cmakelists Some of the key words in the are written and used in a similar way , There may be a bit of confusion for novices who haven't been in touch . This article will introduce the simple CmakeLists File configuration method , I hope you can compile your own C++ Engineering documents .
1. The file path is as follows :

2. Header files and cpp The contents of the document are as follows :
hello.h
#include <iostream>
void say_hello();
hello.cpp
#include "hello.h"
void say_hello(){
std::cout<<"hello_world"<<std::endl;
}
test.cpp
#include <opencv2/opencv.hpp>
#include "hello.h"
using namespace cv;
int main(){
cv::Mat frame = cv::imread("./dog.jpg");
cv::imshow("test", frame);
cv::waitKey(0);
say_hello();
say_hello();
getchar();
return 0;
}
In this case test.cpp call opencv library , And my own hello library , We can write according to our own needs Cmakelists The documents are as follows :
2.1 Manually add :
Use include_directories and target_link_libraries Add header and library files manually
Do not select build hello The library files , need hello.cpp Added to the add_executable
To be sure :project Inside opencvTest Project name ,add_executable and target_link_libraries Inside main Is the executable file name , You can have the same name
cmake_minimum_required(VERSION 3.1)
project(opencvTest)
# Add header file path manually
include_directories("/usr/include/opencv4")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_executable(main hello.cpp test.cpp)# Add library manually
target_link_libraries(main opencv_calib3d;opencv_core;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_video;opencv_videoio)
2.2 Manually add , And generate hellolib:
At this time in add_executable No need to add hello.cpp, But need to be in target_link_libraries Add generated hellolib library , Libraries can be separated by spaces or semicolons
cmake_minimum_required(VERSION 3.1)project(opencvTest)
# Add header file path manually
include_directories("/usr/include/opencv4")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
# Generate static or dynamic libraries
add_librarY(hellolib SHARED hello.cpp)add_executable(main test.cpp)
# Add library manually , Contains the libraries generated above
target_link_libraries(main hellolib opencv_calib3d;opencv_core;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_video;opencv_videoio)
2.3 Use find method :
${OpenCV_INCLUDE_DIRS} Is the header file path ,${OpenCV_LIBS} Found for opencv library
message For output find_package Information about .
To sum up , The search mode is divided into find_package->include_directories->target_link_libraries
cmake_minimum_required(VERSION 3.1)
project(opencvTest)
# Look for libraries in the system directory
find_package(OpenCV REQUIRED)message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# Add the header file path
include_directories(${OpenCV_INCLUDE_DIRS})include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(hellolib SHARED hello.cpp)
add_executable(main test.cpp)
# Link library
target_link_libraries(main hellolib ${OpenCV_LIBS})
2.4 about hpp File status :
2.4.1. Can be hpp File put in include Folder , Use as header file
cmake_minimum_required(VERSION 3.1)
project(opencvTest)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_executable(main test.cpp)
2.4.2. Can also be hpp The file is placed in the file to be compiled cpp Files are compiled under the same level directory
cmake_minimum_required(VERSION 3.1)
project(opencvTest)
add_executable(main hello.hpp test.cpp)
That's right cmakelists A brief summary of grammar , For small projects, the above configuration can be used cmakelists file , Welcome to discuss .
边栏推荐
- How do you use your own data to achieve your marketing goals?
- [WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx
- PyFlink实现自定义SourceFunction
- Magics 23.0如何激活和使用视图工具页的切片预览功能
- How does Google's audience work?
- 谷歌的智能出价有几种?
- Project training (XVII) -- personal work summary
- Implementation and design of JMeter interface test database assertion for CSDN salary increase technology
- 4、 Improvement of warehousing management function
- How to solve practical problems through audience positioning?
猜你喜欢

How to solve practical problems through audience positioning?

General IP address, account and password of mobile IPv6 optical cat login, and mobile optical cat is in bridging mode

Workspace for ROS

Large end storage and small end storage

Tweets movement description and chart display

Network communication tcp/ip

Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)

路径字段是什么? ——竞价广告

How does Google's audience work?

Matplotlib drawing Chinese garbled code
随机推荐
30: Kakfa simulates JSON data generation and transmission
Temporary objects and compilation optimization
Matplotlib drawing Chinese garbled code
[wsl2] restrict wsl2 accessible hardware resources (cpu/ memory)
Spit bubbles (stack)
如何利用您的自有数据来实现营销目标?
Delphi 10.4.2 release instructions and installation methods of three patches
开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧
Numpy multidimensional array transpose transpose
What is the path field—— Competitive advertising
Camera model_
五、库存查询功能的完善
谷歌加大型文字广告是什么?怎么用?
移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
Devaxpress Chinese description --tdximageslider (picture rotation control)
Topic creation and running example of ROS
Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2
How to solve the problems when using TV focusable to package APK in uni app
csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
URI, URL and urn difference, relation and syntax diagram