Are CMAKE_SOURCE_DIR and PROJECT_SOURCE_DIR the same in CMake?

前端 未结 2 1783
悲&欢浪女
悲&欢浪女 2021-01-30 00:18

This page contains a good summary of variables CMake already defines for us. I feel that some variables are the same. Take the example of CMAKE_SOURCE_DIR and

2条回答
  •  青春惊慌失措
    2021-01-30 00:27

    There is a difference between these variables. CMAKE_SOURCE_DIR does indeed refer to the folder where the top-level CMakeLists.txt is defined. However, PROJECT_SOURCE_DIR refers to the folder of the CMakeLists.txt containing the most recent project() command.

    For example, say you have a top-level project called Outer and this contains a subdirectory with its own project called Inner. Outer's CMakeLists.txt has:

    project(Outer)
    add_subdirectory(Inner)
    

    and Inner's:

    project(Inner)
    

    Then in both of these CMakeLists files, CMAKE_SOURCE_DIR will refer to Outer's source dir. But while PROJECT_SOURCE_DIR for Outer is also this same dir, this is not the case for Inner. Inner's PROJECT_SOURCE_DIR is the subdirectory containing its CMakeLists.txt.

    This difference applies to all PROJECT_ vs CMAKE_ variables.

提交回复
热议问题