Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building MSVC project with cmake and command-line

Good day!

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)

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

#include <stdio.h>

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

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).

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 )

like image 377
shybovycha Avatar asked Dec 19 '11 08:12

shybovycha


People also ask

How do I create a project using CMake command line?

For a list of available generators, run cmake --help . Create the binary folder, cd to that folder, then run cmake , specifying the path to the source folder on the command line. Specify the desired generator using the -G option. If you omit the -G option, cmake will choose one for you.

How do I build and run with CMake?

To build with just cmake change directory into where you want the binaries to be placed. For an in-place build you then run cmake and it will produce a CMakeCache. txt file that contains build options that you can adjust using any text editor.


1 Answers

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

Also see the documentation.

like image 73
sakra Avatar answered Oct 11 '22 10:10

sakra