Working with multiple projects in CMake

I am currently working on a transition to a Visual C ++ project (which has several subprojects inside it) for CMake.

There is one thing I'm not sure about - basically, to include subprojects from the top-level CMakeLists.txt file, I simply use the add_subdirectory command and refer to the directories in which these various subprojects are stored.

However, I have one project that is in the same directory as my top-level CMakeLists.txt file, and so I wonder if it is possible to include this file anyway? CMake does not allow me to call add_subdirectory on an existing PROJECT_BINARY_DIR (see below):

 add_subdirectory(${PROJECT_BINARY_DIR}) #not allowed in CMake 

I cannot think of another way to include this subproject in my CMake assembly. Any ideas?

+7
source share
1 answer

Everything add_subdirectory does, adds a subdirectory to it with the CMakeLists.txt file, and therefore it makes no sense to let you add the current directory. You can simply add CMake logic to create this part of your project in the CMakeLists.txt file. If you want to separate the logic, you can put it in build_project.cmake and then use include,

 include(build_project.cmake) 

You can include as many CMake files as you want, and that the CMake code will be evaluated as if it were inserted inline. Thus, all normal add_executable and similar commands will work.

+12
source

All Articles