CMake: Best practices for differing lib paths with ExternalProject_Add

Stack Overflow Asked by 0x5453 on August 20, 2020

I have a small project whose CMakeLists.txt looks something like this:

  PREFIX gainput
  GIT_TAG e21b15f0bc3dd3f1a745fe89a966a2457e940142
ExternalProject_Get_Property(gainput_project SOURCE_DIR)
ExternalProject_Get_Property(gainput_project BINARY_DIR)

add_executable(demo main.cpp)
add_dependencies(demo gainput_project)

target_include_directories(demo PUBLIC

# TODO: this is bad
  target_link_libraries(demo PUBLIC
    debug ${GAINPUT_BINARY_DIR}/lib/Debug/gainput-d.lib
    optimized ${GAINPUT_BINARY_DIR}/lib/Release/gainput.lib
  target_link_libraries(demo PUBLIC
    debug ${GAINPUT_BINARY_DIR}/lib/
    optimized ${GAINPUT_BINARY_DIR}/lib/

Obviously, those calls to target_link_libraries are not ideal; they are very repetitive and I am explicitly writing paths and filenames that the build system should already know. What is the best way to handle the different output directories and filenames from the external project?

Is there a way I can query the external project to get the actual output dir? (e.g. via ExternalProject_Get_Property?) Is there some magical generator expression I can use? (ExternalProject_Get_Property(gainput_project RUNTIME_OUTPUT_DIRECTORY) does not work.)

It seems like find_library would work, except that the output library does not exist at configure time, so that fails.

Maybe I should set the external project’s INSTALL_DIR to some hard-coded directory that I control and always use that? (Would it still get the Debug and Release subdirectories on Windows?)

Would the FetchContent module give me better access to the outputs of the included project? I don’t see anything obvious in the docs there.

Idk, I’ve already tried quite a few ideas and nothing has worked so far. I feel like I may be missing something fundemental – this is the first time I’ve used ExternalProject. Any tips?

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP