I have been trying to follow the vendor\'s tutorial here: CMake-Tutorial, look over the documentation here: Cmake-Documentation, and educate myself as best as possible with
In add_executable()
call relative paths are always interpreted relative to variables CMAKE_CURRENT_SOURCE_DIR
or CMAKE_CURRENT_BINARY_DIR
.
If you have source files in subdirectories of these dirs, you should explicitely specify these subdirectories:
add_executable(main src/main.cpp)
Also, variables like PROJECT_SOURCE_DIR
should be treated as readonly: while changing them isn't detected by CMake as an error, it breaks some things.
There are some points I'd like to mention.
include_directories
helps for finding header files. Source files must always have a complete relative path.
Assuming that your main.cpp is within src, the correct syntax is
add_executable(main ${PROJECT_SOURCE_DIR}/main.cpp)
Including ${PROJECT_BINARY_DIR}
does not make sense.
Overwriting CMake default variables like ${PROJECT_SOURCE_DIR}
is no good practice. In future you will always remember that you have done such a thing and for another programmer it is quite unexpected.
Hope it helps.