惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
G
Google Developers Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
P
Proofpoint News Feed
V
Visual Studio Blog
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 叶小钗
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
H
Help Net Security
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
量子位
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Build C++ Project with CMake -- A Simple Example
2020-12-12 · via jdhao's digital space

For small projects, people tend to directly invoke the compiler with the suitable options to build the program. While it is feasible, it is tedious to type those compilation options and flags. For projects with multiple source files and header files, it becomes inefficient to build by hand. On Linux, people may write Makefile and build with make. On Windows, people may use Visual Studio for writing and build their project.

CMake is a meta build tool. It can generate build files for various build tools such as make, Ninja, Visual Studio. So using CMake is way to make sure that our programs can build across platforms and build tools. Some famous open source projects choose CMake as their build tools. These projects include Neovim, OpenCV, MySQL.

In post, I will share how to build a simple C++ program using CMake.

The source file#

First, create a project directory, add the following source file write_text.cc. It uses the OpenCV library to read an image, write some text to image and save it on the disk.

#include "opencv2/opencv.hpp"

int main() {

    cv::Mat img = cv::imread("wind-turbine.jpg", cv::IMREAD_COLOR);
    cv::putText(img, "Test text", cv::Point(100, 100), cv::FONT_ITALIC, 2.0,
                cv::Scalar(0, 0, 255), 2);
    cv::imwrite("text_img.jpg", img);

    return 0;
}

Create Cmake file CMakeLists.txt#

Under project root, create a file named CMakeLists.txt. This file describes how we want to build our project in the language of CMake. The content of is:

cmake_minimum_required(VERSION 3.10)

project(opencv_demo)

add_executable(write_text write_text.cc)

set(OPENCV_INCLUDE_DIR /home/jdhao/local/include/opencv4/)
set(OPENCV_LIB_DIR /home/jdhao/local/lib/)

message(STATUS "OpenCV library path: ${OPENCV_LIB_DIR}")

# set include directory
target_include_directories(write_text PUBLIC "${OPENCV_INCLUDE_DIR}")

# set library directory
target_link_directories(write_text PUBLIC "${OPENCV_LIB_DIR}")

# link specific object files we need
target_link_libraries(write_text opencv_imgcodecs opencv_core opencv_imgproc)

It is best to create a build directory for building the project so that we do not pollute the source directory.

mkdir build
# use the following command to build if you are in project root
cmake -Bbuild -DCMAKE_BUILD_TYPE=Release

# or use the following command if you are in build directory
# cmake ../ -DCMAKE_BUILD_TYPE=Release

By default, cmake will generate a build file for make on Linux systems. If you want to generate build file for other tools such as ninja, you can use -G. For example, to generate build file for ninja, use the following command:

mkdir build_ninja
cmake -Bbuild_ninja -G "Ninja"

The option -DCMAKE_BUILD_TYPE=Release specify the build type. We can also use Debug or RelWithDebInfo etc.

To build the project, we can use cmake --build build if we are in project root or cmake --build . if we are in directory build. If you generate Makefile for make, you can also use make -C build to build the project directly, or use ninja -C build_ninja if you generate the build file for ninja. The advantage of using command cmake --build is consistency. You do not need to worry which specific build tool to invoke. CMake will figure it out for you.

Generate complilation databases#

If you happen to use ccls for code completion, you can also generate a compilation databases file named compile_commands.json for ccls to use, with the help of cmake. With the help of this file, ccls can provide code completion and code analysis for your project.

To produce this file, use the following comamnd:

cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=YES
ln -s build/compile_commands .

Ref#