使用cmake和命令行构建MSVC项目 [英] Building MSVC project with cmake and command-line

查看:1668
本文介绍了使用cmake和命令行构建MSVC项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

让我们有一个源文件main.cpp和一个包含下一个文本的CMakeLists.txt文件:

Let us have a source file main.cpp and a CMakeLists.txt file containing the next text:

cmake_minimum_required(VERSION 2.6)
project(tmp)

set(CMAKE_CXX_FLAGS "-Wall")
add_executable(tmp.elf main.cpp)

比方说,main.cpp文件包含一个简单的"Hello,World!".程序:

Let's say the main.cpp file contains a simple "Hello, World!" program:

#include <stdio.h>

int main()
{
  printf("Hello, World!\n");
  return 0;
}

我们可以使用cmake CMakeLists.txt && make构建项目.然后,我们将获得可以运行的tmp.elf文件.或者我们无法获得tmp.elf文件,并假定main.cpp源文件有问题(假设编译器和cmake已正确安装在建筑系统中).

We can build the project with cmake CMakeLists.txt && make. Then we'll just get the tmp.elf file which we can just run. Or we can get no tmp.elf file and assume that something is wrong with the main.cpp source file (assuming the compiler and cmake are installed properly on the building system).

因此,问题是:我们如何在Windows计算机上执行相同的操作?例如.我们将在运行cmake CMakeLists.txt之后获得tmp.vcproj文件,然后我们需要以某种方式进行构建.如何使用命令行执行构建过程? (Java的Process.start(),实际上是:-P)

So, the question is: how can we do the same on the Windows machine? E.g. we will get the tmp.vcproj file after running cmake CMakeLists.txt and then we need to build it somehow. How the build process can be performed using command-line? (Java's Process.start(), actually :-P )

推荐答案

您可以通过使用--build选项调用cmake,以独立于平台和CMake生成器的方式启动构建:

You can start the build in a platform and CMake generator independent fashion by invoking cmake with the --build option:

cmake --build .

对于多配置生成器,您可以通过以下方式指定配置:

For multi-configuration generators, you can specify the configuration in the following way:

cmake --build . --config Release

另请参见文档

这篇关于使用cmake和命令行构建MSVC项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆