cmake在Windows上的使用
最编程
2024-08-12 20:24:26
...
cmake介绍
cmake是一个构建C++代码的跨平台工具,他能够干什么呢?他能够搜索你的平台环境,然后生成平台上用于Build的文件。在Windows上安装了Visual Studio, 他能够帮你生成.sln, .vcxproj文件。如果在Linux上,能够帮你生成makefile。在MAC OS上,帮你生成xcode的项目文件。利用这些文件你就可以在本地编译,链接文件。生成这些项目,solution文件的输入信息,就是一套自己编写的和平台无关的配置文件。一般使用CMakeLists.txt文件。
cmake在Windows上的安装
https://cmake.org/download/ 上下载Windows安装包,安装就可以了。
cmake的简单实用
编写C++文件
首先需要一个简单的Hello World程序。
HelloWorld.cpp
#include <iostream>
using namespace std;
int main() {
cout<<"Hello World"<<endl;
return 0;
}
然后需要一个CMakeLists.txt
在cpp文件相同的目录下(项目cpp源文件根目录)建立一个CMakeLists.txt文件。
cmake_minimum_required (VERSION 2.6)
project (HelloWorld)
add_executable (HelloWorld HelloWorld.cpp)
运行cmake
到CMakeLists.txt文件所在目录,运行cmake。
cmake .
输出
D:\HelloWorld>cmake .
-- Building for: Visual Studio 14 2015
-- The C compiler identification is MSVC 19.0.23506.0
-- The CXX compiler identification is MSVC 19.0.23506.0
-- Check for working C compiler using: Visual Studio 14 2015
-- Check for working C compiler using: Visual Studio 14 2015 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 14 2015
-- Check for working CXX compiler using: Visual Studio 14 2015 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/HelloWorld
生成的.sln, .vcxproj文件
ID | 文件名 |
---|---|
1 | HelloWorld.sln |
2 | ALL_BUILD.vcxproj |
3 | HelloWorld.vcxproj |
4 | ZERO_CHECK.vcproj |
你可以用visual studio打开.sln文件来编译,运行,调试程序。
构建程序
msbuild HelloWorld.sln
然后就可以在Debug目录下面找到HelloWorld.exe程序。
总结
cmake给跨平台的C++程序提供了很好的构建解决方案。适合需要跨平台项目的使用。功能不多,简单,但还是比较实用的。建议阅读https://cmake.org/cmake-tutorial/ 。基本功能在tutorial里面都有介绍。
推荐阅读
-
Ant Design Pro]在 Mac 上配置和运行 Ant Design Pro,并配置解决运行时遇到的一些错误。
-
在安卓设备上使用 Kivy 和 OpenCV 来调用摄像头并显示实时画面。
-
Solon-Boot 和 SpringBoot 在概念上的区别
-
Mac 上的 Safari 有一个非常奇怪的问题:在输入框中输入所有小写字母时,第一个字母会自动转换为大写字母。
-
在使用 pycharm 的 sftp 功能远程控制服务器时,我遇到了一些问题:本地路径''不在项目中
-
[C++] CMake 使用 OpenMP 并行加速 - II.在 CMake 项目中使用 OpenMP
-
在 Linux 上安装软件包时安装 apt 和 pip 的位置
-
使用内置的 mysql 在 docker 中部署 zendo - 启动 zendo
-
在 Windows 上安装 Anaconda 并将其适配到 PowerShell 的 Nanny 教程。
-
如何使用工具移除 iPhone 上的图片背景