当前位置:网站首页>CMake教程系列-02-使用cmake代碼生成二進制
CMake教程系列-02-使用cmake代碼生成二進制
2022-06-30 02:36:00 【喜歡打籃球的普通人】
1.Cmake的安裝
2.Win10下使用CMake GUI
Win10安裝文件:
創建的目錄以及代碼如下:
- CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(sample CXX)
add_library(sample sample.cpp)
add_executable(sample_exe sample_exe.cpp)
- sample.cpp
#include <iostream>
int print_hello_world()
{
std::cout << "hello world!" << std::endl;
return 0;
}
- 目錄結構如下:注意多創建一個build目錄存放中間文件和最終二進制文件

點擊“Configure
- 我的的vs是2019,在Configure中選擇了

點擊“Configure”右邊的“Generate”。
點擊“Generate”右邊的“Open Projrct”。
- 可以看到針對於Visual Studio的配置已經完全生成,此時只需要構建 ALL_BUILD 即可生成庫 sample 和可執行文件 sample_exe


2.win使用命令行生成項目
- 參考:cmake(1)
使用上面的代碼文件,打開命令行,並輸入以下命令:
E:\CmakeTest>cmake.exe -S E:\\CmakeTest -B E:\\CmakeTest\build -G "Visual Studio 16 2019" -A x64
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The CXX compiler identification is MSVC 19.29.30141.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: E:/CmakeTest/build
執行以下命令生成庫文件以及二進制程序:
E:\CmakeTest>cmake --build E:\\CmakeTest\build
用於 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642
版權所有(C) Microsoft Corporation。保留所有權利。
Checking Build System
sample_exe.cpp
sample.vcxproj -> E:\CmakeTest\build\Debug\sample.lib
sample_exe.cpp
sample_exe.vcxproj -> E:\CmakeTest\build\Debug\sample_exe.exe
Building Custom Rule E:/CmakeTest/CMakeLists.txt
3.幾個重要的命令行參數
-S
- 頂級CMakeLists.txt(包含project聲明)所在路徑。
-B
- 存放臨時編譯的二進制文件(.obj、.ilk等)和編譯器對應的配置文件路徑。
-G
- 編譯器名稱
-A
- 架構名稱
-D
- 使用該變量以向cmake傳入各種參數。包括選項及覆蓋cmake提供的各種默認變量值。
--toolchain
- cmake toolchain文件路徑。
--install-prefix
- 安裝的二進制存放路徑。
--trace / --trace-expand
- 調試時使用,用於打印已執行的cmake代碼及行號。否則僅輸出函數message中的內容。
--build
- 使用cmake直接調用編譯器編譯項目。
--config
- 選擇需要編譯的項目配置類型。
--install
安裝已編譯好的二進制文件至 CMAKE_INSTALL_PREFIX 中。
边栏推荐
- [论]【DSTG】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
- Precautions for purchasing wildcard SSL certificate
- JS advanced -es6 syntax
- 选择排序
- Créer des compétences exquises dans l'éducation des créateurs
- Bubble sort
- Global and Chinese market of subscription revenue management software 2022-2028: Research Report on technology, participants, trends, market size and share
- 【干货分享】最新WHQL徽标认证申请流程
- Four, forty, fourhundred swatches
- Bucket sort
猜你喜欢

Unity TimeLine 数据绑定

Bucket sort

What files does a CA digital certificate contain? How to view SSL certificate information?

Select sort

2022年7月深圳地区CPDA数据分析师认证

What should academic presentation /ppt do?

IBM websphere通道联通搭建和测试

Bubble sort

Seven common errors of SSL certificate and their solutions

CA数字证书包含哪些文件?如何查看SSL证书信息?
随机推荐
有流量,但没有销售?增加网站销量的 6 个步骤
AutoJS代码能加密吗?YES,AutoJS加密技巧展示
Enlightenment from the revocation of Russian digital certificate by mainstream CA: upgrade the SSL certificate of state secret algorithm to help China's network security to be autonomous and controlla
选购通配符SSL证书注意事项
Seven common errors of SSL certificate and their solutions
归并排序
Ffmpeg source code
Four, forty, fourhundred swatches
What are the requirements for NPDP product manager international certification examination?
C language pivot_ Invalid argument error resolution for root
What is an X.509 certificate? 10. 509 certificate working principle and application?
PEM_ read_ bio_ Privatekey() returns null only in ECB mode - PEM_ read_ bio_ PrivateKey() returns NULL in ECB mode only
Bubble sort
Shenzhen CPDA Data Analyst Certification in July 2022
dhu编程练习
迅为恩智浦iTOP-IMX6开发平台
What is a dangling pointer- What is a dangling pointer?
Dynamic SQL
Global and Chinese markets for wireless security in LTE networks 2022-2028: Research Report on technology, participants, trends, market size and share
DHU programming exercise