CMake learning (3) - Use add_subdirectory() to add an external project folder

        Under normal circumstances, each sub-project of our project is in a general project root directory, but sometimes, we need to use external folders. What should we do? For example, cxx/utility/examplewithin a directory CMakeLists.txtyou want to refer to the upper directory cxx/utility: CMakeLists.txtusually
Insert image description hereInsert image description here
        , if cxx/utility/examplewithin a directory CMakeLists.txtyou use it directly.

add_subdirectory(${
    
    CMAKE_CURRENT_SOURCE_DIR}/..)

        When executed cmake -B build, an error will be reported:

CMake Error at CMakeLists.txt:14 (add_subdirectory):
  add_subdirectory not given a binary directory but the given source
  directory "D:/workspace/sdk/base/cxx/utility" is not a subdirectory of
  "D:/workspace/sdk/base/cxx/utility/example".  When specifying an
  out-of-tree source a binary directory must be explicitly specified.

        Reason for error : Because the folder to be added is not a subdirectory of the current project. The last sentence points the way: to specify an external folder you must specify it explicitly. But how to specify it explicitly? Check out the official documentation.
Insert image description here        It turns out add_subdirectorythere is another binary_dirparameter, which is used to specify source_dirthe location in the output folder. If not specified, source_dirthe value is used. binary_dirThis must be specified if you want to add an external folder . cxx/utility/exampleThe changes inside CMakeLists.txtare:

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/.. 3rdparty/base/utility)

        After execution, cmake -B buildyou can find additional subdirectories in the output directory , which is specified by 3rdparty/base/utilitythe second parameter :binary_dir
Insert image description here

Guess you like

Origin blog.csdn.net/hezhanran/article/details/123896355