Expert
Expert

Reputation: 31

CMake : add_executable not generating an executable under windows OS

I am trying to generate an executable with CMake under windows by command line.

This is what I do: I enter testprog directory then do

 cd build
 cmake ..

I also tried this:

cd build
cmake -G"Visual Studio 16 2019" ..

same result as earlier attempt.

This builds bunch of files in build directory but no executable is ever created. It creates files to be opened in visual studio.

This is how my folder look like;

testprog
      -build
      -src
             -main.cpp
             -call.h
      -include
            -main.h
            -call.h
      -CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)

project(testprog)

include_directories(include)

file(GLOB SOURCES "src/*.cpp")

add_executable(testprog ${SOURCES})

Upvotes: 2

Views: 5774

Answers (2)

abhilb
abhilb

Reputation: 5757

In order to build you have to call cmake --build <dir> [<options>] [-- <build-tool-options>]

Documentation is here

Upvotes: 0

Dean Seo
Dean Seo

Reputation: 5693

This builds bunch of files in build directory but no executable is ever created. It creates files to be opened in visual studio.

That's it.

That's exactly what CMake does; CMake is a meta build system where it generates all necessary files that you can open with, say, Visual Studio on Windows since you've given:

-G "Visual Studio 16 2019"

and build the projects with Visual Studio.

add_executable does not magically build your source files. It only tells the given tool to do so.

It's all same in Unix-like platforms as well; CMake drops Makefile, and then you build with make. (Or other tools like Ninja can be a good candidate instead of make)

EDIT

I understand, but is there no way to build the executable from command line without the need of say Visual studio

Yes, you can! Microsoft provides Build Tools for Visual Studio for those who don't want the whole heavy IDE.

This is quite common when it comes to establishing a build server.

Upvotes: 4

Related Questions