Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include an additional CMakeLists.txt

Say we have a command call foo in CMakeLists.txt which is in folder /A.

foo is defined in antother CMakeLists.txt which is in folder /B.

How can one reference to /B/CMakeLists.txt from within /A/CMakeLists.txt in order to call foo?

I tried to set search paths to /B/CMakeLists.txt via:

  • CMAKE_INCLUDE_PATH
  • CMAKE_MODULE_PATH
  • CMAKE_SOURCE_DIR

but none of them worked.

CMake still complaines Unknown CMake command "foo".

like image 877
Juergen Avatar asked Jun 21 '13 06:06

Juergen


People also ask

How do I update CMakeLists text?

You can edit CMakeLists. txt files right in the Editor. Make sure to reload the project after editing. Automatic reload feature is disabled by default, you can turn it on by selecting the Automatically reload CMake project on editing checkbox in Settings / Preferences | Build, Execution, Deployment | CMake.

Where do I put CMakeLists txt?

CMakeLists. txt is placed at the root of the source tree of any application, library it will work for. If there are multiple modules, and each module can be compiled and built separately, CMakeLists. txt can be inserted into the sub folder.

How do I add to CMakeLists?

The first step is to add an option to the top-level CMakeLists. txt file. This option will be displayed in the cmake-gui and ccmake with a default value of ON that can be changed by the user.


1 Answers

That can be done with include, here's some simple example:

Content of A/CMakeLists.txt

function(foo)     message(STATUS "heya") endfunction() 

Content of B/CMakeLists.txt

cmake_minimum_required(VERSION 2.8) include(${CMAKE_CURRENT_SOURCE_DIR}/../A/CMakeLists.txt) foo() 

Now, including another CMakeLists.txt will also run everything in that file, so you may want to avoid doing that if there are targets in B/CMakeLists.txt

If you can modify your code, it would be better to define your functions in a "top level" CMakeLists.txt before calling add_subdirectory.

like image 114
Guillaume Avatar answered Oct 15 '22 23:10

Guillaume