码迷,mamicode.com
首页 > Windows程序 > 详细

windows下CMake使用图文手册 Part 1

时间:2016-05-05 07:05:39      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

维基百科介绍“CMake是个开源跨平台自动化建构系统,它用配置文件控制建构过程(build process)的方式和UnixMake相似,只是CMake的配置文件取名为CMakeLists.txt。Cmake并不直接建构出最终的软件,而是产生标准的建构档(如Unix的Makefile或WindowsVisual C++的projects/workspaces),然后再依一般的建构方式使用。这使得熟悉某个集成开发环境(IDE)的开发者可以用标准的方式建构他的软件,这种可以使用各平台的原生建构系统的能力是CMake和SCons等其他类似系统的区别之处。”

1. CMake安装

https://cmake.org/download/, windows下载cmake-3.5.2-win32-x86.msi

安装后的界面

技术分享

2. Hello World Example

假设你建立了文件夹 E:\Playground\CMakeExamples\HelloWorld,下面有一个文件HelloWorld.cpp

#include <iostream>          
int main()
{
   std::cout<<"Hello World!"<<std::endl;
   return 0;
}

在同一文件夹下面创建CMakeLists.txt文件

cmake_minimum_required(VERSION 3.0)
project (hello)
add_executable(hello helloworld.cpp)

  • 第一行设置需要的CMake最低版本号,一般设置为你目前使用的CMake版本号,便于后续维护工作。
  • 第二行设置project名字
  • 第三行add_executable,第一个参量为生成可执行文件名字,取决于所在平台会生成hello.exe,或者hello.a或其他格式。第二个参量为所需要的源文件列表。

图形界面下运行:

 技术分享

在Where is the source code后面填写CMakeLists.txt所在文件夹,注意是CMakeLists.txt所在文件夹,不是HelloWorld.cpp所在文件夹。后面例子会讲到源文件与CMakeLists不在同一目录下的情形。

Where to build the binaries: 可以是任意一个文件夹,保持生成的项目文件,这里我选择生成在Build文件夹下。设置后点击 Configure后你需要选择项目生成器,这里我选择Visual Studio 14 2015 Win64

技术分享

点击Finish后

技术分享

CMake自动设置各种变量,不用理会红色部分,点击Generate,在Build文件夹下面会生成

技术分享

生成了hello.sln,project呢,除了hello.vcxproj还有ALL_BUILD.vcxproj和ZERO_CHECK.vcxproj。

stackoverflow上给的答案是

After some more searching, I found the answer at https://cmake.org/pipermail/cmake/2008-November/025448.html:

Armin Berres — 11/22/2008, 3:12:41 PM

ZERO_CHECK will rerun cmake. You can/should execute this after changing something on your CMake files.

ALL_BUILD is simply a target which builds all and everything project in the active solution, I guess one can compare it to "make all".

你可以用visual studio打开hello.sln,编译,运行。这里我们用msbuild编译。如果命令行提示msbuild无法找到,请先添加C:\Windows\Microsoft.NET\Framework64\v4.0.30319到系统PATH变量中。

在Build目录下,运行

msbuild hello.vcxproj

技术分享

运行Debug\hello.exe,查看输出。

如果要编译成Release,运行

msbuild /p:Configuration=Release hello.vcxproj

windows下CMake使用图文手册 Part 1

标签:

原文地址:http://www.cnblogs.com/cuiocean/p/5460419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!