关键词搜索

源码搜索 ×
×

使用cmake在Windows上编译c++

发布2019-03-27浏览16808次

详情内容

原文地址:https://blog.csdn.net/zhangyiant/article/details/51289404

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

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. cout<<"Hello World"<<endl;
  5. return 0;
  6. }


需要一个CMakeLists.txt


在cpp文件相同的目录下(项目cpp源文件根目录)建立一个CMakeLists.txt文件。

  1. cmake_minimum_required (VERSION 2.6)
  2. project (HelloWorld)
  3. add_executable (HelloWorld HelloWorld.cpp)


运行cmake

到CMakeLists.txt文件所在目录,运行cmake。

cmake .

输出

  1. D:\HelloWorld>cmake .
  2. -- Building for: Visual Studio 14 2015
  3. -- The C compiler identification is MSVC 19.0.23506.0
  4. -- The CXX compiler identification is MSVC 19.0.23506.0
  5. -- Check for working C compiler using: Visual Studio 14 2015
  6. -- Check for working C compiler using: Visual Studio 14 2015 -- works
  7. -- Detecting C compiler ABI info
  8. -- Detecting C compiler ABI info - done
  9. -- Check for working CXX compiler using: Visual Studio 14 2015
  10. -- Check for working CXX compiler using: Visual Studio 14 2015 -- works
  11. -- Detecting CXX compiler ABI info
  12. -- Detecting CXX compiler ABI info - done
  13. -- Detecting CXX compile features
  14. -- Detecting CXX compile features - done
  15. -- Configuring done
  16. -- Generating done
  17. -- 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里面都有介绍。
 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载