使用cmake和命令行构建MSVC项目 [英] Building MSVC project with cmake and command-line
问题描述
美好的一天!
让我们有一个源文件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屋!