AnswerBun.com

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:

include(ExternalProject)
ExternalProject_Add(gainput_project
  PREFIX gainput
  GIT_REPOSITORY https://github.com/jkuhlmann/gainput.git
  GIT_TAG e21b15f0bc3dd3f1a745fe89a966a2457e940142
  INSTALL_COMMAND ""
)
ExternalProject_Get_Property(gainput_project SOURCE_DIR)
ExternalProject_Get_Property(gainput_project BINARY_DIR)
set(GAINPUT_SOURCE_DIR "${SOURCE_DIR}")
set(GAINPUT_BINARY_DIR "${BINARY_DIR}")

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

target_include_directories(demo PUBLIC
  SYSTEM ${GAINPUT_SOURCE_DIR}/lib/include
)

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

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!

Related Questions

Find the best matching combination of numbers

0  Asked on December 18, 2020 by stkvtflw

     

toUpperCaseFunction does not work with await keyword

2  Asked on December 18, 2020 by goxarad784

   

How to split given linked list

1  Asked on December 18, 2020 by evgeni-nabokov

   

delete bigquery table from spark by using scala

1  Asked on December 17, 2020 by counter10000

     

Python : find_all() return an empty list

2  Asked on December 17, 2020 by bastien

     

expected false to deeply equal true on postman

2  Asked on December 17, 2020 by cristian-hernandez

   

Split the data in the specific column in the DataFrame

2  Asked on December 17, 2020 by jaehyeon-robert-han

   

preventDefault not working when form submit react js

2  Asked on December 17, 2020 by gihankumara

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP