CMake - two source files into one executable

Hi to the Community,

i have a question about the CLION IDE regarding CMake:

I read (in a tutorial) that I can add a second file to the same executable like this (and I tried it):

cmake_minimum_required(VERSION 3.30)
project(CMakeTest)

set(CMAKE_CXX_STANDARD 26)

add_executable(CMakeTest main.cpp 
				general.cpp)

I don't understand in which way they are gonna get compiled. I understood that only one can have a main() function. When I tried to use general as a “library” ('#include “general.cpp”' in main.cpp) with a sample function xy only defined in ‘general.cpp’, I got an Error saying 'xy is defined twice'. So how are they put and compiled together and what i the advantage of doing that? (Sorry if I have some fundamental misconceptions)

Thanks in advance

1
1 comment

Hello!

When I tried to use general as a “library” ('#include “general.cpp”' in main.cpp) with a sample function xy only defined in ‘general.cpp’, I got an Error saying 'xy is defined twice'.

If you use #include “general.cpp” in main.cpp, you don't need to add general.cpp to the target in CMakeLists.txt. It should be either this or that, not both at the same time.

How do you want to use general.cpp? What code do you have there?

0

Please sign in to leave a comment.