Keyikedalube Ndang
Keyikedalube Ndang

Reputation: 361

Can values be set for CMake SOURCE(S) other than using set?

Suppose I have a simple C++ hello-world project with the following CMake script:

cmake_minimum_required(VERSION 3.15)

project(hello)

set(SOURCE main.cpp)

add_executable(${PROJECT_NAME} ${SOURCE})

Now I noticed that PROJECT_NAME is built-in and its value is set from project(*value*) but also SOURCE (and SOURCES) seems to be provided by CMake too.

Are there other ways where SOURCE can be assigned with project source files? Just like the same behavior with PROJECT_NAME. Or is set(SOURCE ...) the intended method.

I'm new to CMake. The SOURCE and SOURCES variables were colored out on my text editor. I'm confused.

Upvotes: 0

Views: 44

Answers (1)

Etienne Laurin
Etienne Laurin

Reputation: 7184

Using a SOURCE variable is a common patter in CMake files, but it is not required.

The code above can be written without any variables, it would look something like this:

add_executable(hello main.cpp)

When there are a lot of source files, passing them all to add_executable can be inconvenient. Another alternative is target_sources:

add_executable(hello)

target_sources(hello PRIVATE main.cpp)

Upvotes: 1

Related Questions