1

I have multiple projects that have identical/duplicated code in their respective CMakeLists.txt. At the very least I need a place to store string definitions that I can pull into the CMakeLists.txt files. What's the best way to do this? My directory structure

common_defs/
        src_files
        hdr_files
        CMakeLists.Txt  - file that has common defs
independent_dir1/
        src_files
        hdr_files
        CMakeLists.Txt  --> Imports/Includes from ../common_defs/CMakeLists.txt
independent_dir2/
        src_files
        hdr_files
        CMakeLists.Txt  --> Imports/Includes from ../common_defs/CMakeLists.txt

independent_dir1 and independent_dir2 will be built independently from each other. Building in common_defs should not trigger builds in the independent_dirs

4
  • 1
    Explore add_subdirectory and related functions. Commented Jun 21, 2018 at 13:19
  • add_subdirectory doesn't quite get me there, because I need independent builds. Commented Jun 21, 2018 at 13:20
  • use include to include common cmake code. But honestly, i think we dont entirely understand your problem. What do you mean with pull? Commented Jun 21, 2018 at 13:24
  • include will do it. WRT "pull" I'm not sure what to call it, I can see why it's confusing. I mean that the independent_dir*/CMakeLists files should include/import the common code from the common_defs/CMakeLists.txt file Commented Jun 21, 2018 at 13:28

1 Answer 1

1

Create a file in common_defs/common_defs.cmake with common strings and definitions, than add to your CMakeLists.txt:

// add path common_defs to include search path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../common_defs")
// include module from file common.cmake searched in search path
include(common_defs)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I'll give this a shot now, and mark the answer appropriately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.