1

I'm working on a main project, having several third party libraries. My goal is to import all of these as imported target for clarity sake, but I can't get one to work. It's basically a set of static libraries (.lib / lib.a) and their headers.

For the sake of discussion, Main is the main target, Third is the imported third party.

I manage to generate a proper Visual Studio solution on Windows, but things are not working as intended on Linux. When building the solution, I end up with this error:

"No rule to make target 'Third-NOTFOUND', needed by '../Bin/Main'"

Here are some code snippets from my CMakeLists.txt's.

Main's CMakeLists.txt:

project(Main)
[...]
add_subdirectory(ThirdParty/Third)
set_property(TARGET Third PROPERTY FOLDER "thirdparty")
[...]
add_executable(Main ${SOURCES})
target_link_libraries(Main PRIVATE Third)

Third's CMakeLists.txt:

project(Third)
[...]
# Helper function
function(append_lib target lib_debug_dir lib_release_dir name)
    set_property(TARGET ${target} APPEND PROPERTY IMPORTED_LOCATION_DEBUG "${lib_debug_dir}/${LIB_NAME}")
    set_property(TARGET ${target} APPEND PROPERTY IMPORTED_LOCATION_RELEASE "${lib_release_dir}/${LIB_NAME}")
endfunction()
[...]
add_library(Third STATIC IMPORTED GLOBAL)
set_property(TARGET Third APPEND PROPERTY PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${INCLUDE_DIR}")

append_lib(Third ${LIB_DIR_DEBUG} ${LIB_DIR_RELEASE} libMyLib1.a)

NB: The code for Linux and Windows are the same, except the name of the static library it self (MyLib1.lib replaces libMyLib1.a)

What am I doing wrong? Thanks :)

  • Is it a path issue? Does this answer apply to you: http://stackoverflow.com/questions/14077611/how-do-i-tell-cmake-to-link-in-a-static-library-in-the-source-directory – Nikos C. Apr 21 '15 at 13:14
  • I don't think so. I've just checked that again, these are all correct absolute paths. – Alexandre Z. Apr 21 '15 at 15:38
  • If you inspect the generated Makefile, do you see the absolute paths in there? – Nikos C. Apr 22 '15 at 15:37
  • Actually you put me in the right direction :) The libs are indeed missing from the generated Makefile. I've tried to set IMPORTED_LOCATION rather than IMPORTED_LOCATION_ and it solves the problem. I'm now looking for a way to specify different libs for debug and release configuration (like on Windows). – Alexandre Z. Apr 22 '15 at 17:25

2 Answers2

2

I've finally managed to get what I wanted, even though I could not find a way to get a method that works on both systems.

On Windows, I stick to what I described in my question.

On Linux however, I've found some workaround. I'm importing one libraries as IMPORTED_LOCATION and all the others as INTERFACE_LINK_LIBRARIES. That's not great, but if I don't specify an imported location, I get the Third-NOTFOUND error.

As Antonio suggested, I use CMAKE_BUILD_TYPE to link different libraries on different configuration.

Thank you both for you help!

0

If it works in Windows and not in Unix, that's very likely because of path casing: namely, you have inconsistent use of uppercase and lowercase characters in your folder names. This is tolerated in Windows, but ruthlessly punished in Unix :)

Solution: Check how you have spelled thirdparty all around your project (Here I see thirdparty and ThirdParty).

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Thank you for you answer! I've double checked each path and I'm positive, they are correct, even considering the casing trap (already fallen into that :P) However, the problem seems to come from the property I'm using. IMPORTED_LOCATION_DEBUG/RELEASE leads to the Third-NOTFOUND error. Using IMPORTED_LOCATION kind of solves the issue (cannot specify different libraries for different configs though), but appending all of them adds the following option in /CMakeFiles/Main.dir/link.txt -o "/libLib1.a;/libLib2.a;[...]" which lead to a lib not found error... – Alexandre Z. Apr 23 '15 at 08:48
  • Removing the double-quotes and replacing commas by spaces solves the problem. I'm looking for a way to add several IMPORTED_LOCATION now (and still looking for a way to differenciate Debug and Release configs :)) – Alexandre Z. Apr 23 '15 at 08:52
  • @AlexandreZ. Are you specifying the build type? Adding something like -DCMAKE_BUILD_TYPE=Debug or -DCMAKE_BUILD_TYPE=Release to the cmake command line (or setting it from cmake-gui, which is easier in fact). – Antonio Apr 23 '15 at 20:30