1

I am trying a simple test to see if CMake is working on my windows system correctly. I keep getting a error. Here is the command with the error.

cmake .
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- Configuring done
CMake Error at CMakeLists.txt:5 (add_executable):
No SOURCES given to target: main.cpp


CMake Generate step failed.  Build files cannot be regenerated correctly.

code for file named main.cpp

#include <iostream>

int main()
{

std::cout << "hello world\n" << "this is a test" <<std::endl;

}

and my CMake file

cmake_minimum_required(VERSION 3.10)

project(test)

add_executable(main.cpp)

I have used CMake in Linux before so not sure why this is failed. I used Microsoft package manager to install it. I am using the command line for this, i tried the GUI it also failed. I have also deleted the CMake files and cache multiple times. I have not been able to find anything online.

SuperCell
  • 27
  • 1
  • 6
  • The first argument to `add_executable` is the **target name**, which should be followed by sources list. It seems you forgot to specify that argument. – Tsyvarev Apr 15 '22 at 18:49

1 Answers1

5

Can't add a comment since my reputation is too low, so I will write an answer instead. In the last line of your CMakeLists.txt file

add_executable(main.cpp)

you are missing the name of the executable

add_executable(name_exe main.cpp)

CMake is telling you that in the error message. CMake tries to create a target main.cpp without source files, since CMake suggests the name of the executable at the first place in the command add_executable().

  • that worked, now how can i get it to spit out a makefile instead of .sln. the -g flag did not work for me – SuperCell Apr 15 '22 at 19:43
  • never mind i got it spit out a .exe file. Just had to run cmake --build . – SuperCell Apr 15 '22 at 19:49
  • To get a makefile you need to specify a Generator which creates makefiles. This should work with the -G flag, I don't know why this didn't work in your case. *.sln files are files for the msvc Compiler which you are using. You can see this in the output of CMake in your question. With cmake --build cmake will call the Compiler, msvc in your case, directly. – CodingWithMagga Apr 15 '22 at 19:55