Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

search - 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".

question from:https://stackoverflow.com/questions/17228677/how-to-include-an-additional-cmakelists-txt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...