For my CMake project i have an external library, consisting of a header file and several .lib/.dll pairs.
The header file selectively links to one of the .lib/.dll pair, take this example:
#ifdef DEBUG
#pragma comment(lib "exampled.lib")
#elif
#pragma comment(lib "example.lib")
#endif
In full, theres a .lib and matching .dll for 32/64 bit and Debug/Release, so 4 pairs in total. Inside the header file is the proper #ifdef
-logic to link the right library. As i said it's an external library, so i don't want to change that header.
What is the right way to teach this to CMake?
For compile time (i.e. include directories) i can use target_include_directories()
which works fine. I can also create an imported target with an interface include directory, this also works fine.
The problem starts at link-time:
target_link_libraries()
forces me to specify one of the .lib/.dll files. I could duplicate the#ifdef
logic, but this feels wrong. Adding all of the files is incorrect as well, as always only one is needed.- Imported targets allow me to add .dll and .lib files as well, but again, i would either have to duplicate the logic or link all libraries.
link_directories()
works on a global scale. Which feels unnecessary if only a few targets actually need it.- adding the given directory to the
PATH
also seems "to global", i.e. since the library is currently distributed along with the code.
So what i am looking for is something like target_link_directories()
or similar solutions.
Obviously to actually load the .dll at runtime i would have to take further steps, so if your solution includes this, it would be very welcome.