1

I have a C library that includes the following dependency in CMake:

if (WIN32)
    target_link_libraries(my_library legacy_stdio_definitions.lib)
    target_link_libraries(my_library wsock32)
else()
    ...
endif()

This works mostly fine for my application, but I'm experimenting with a different approach that requires the libraries to have absolute paths. So switched to the following:

if (WIN32)
    find_library(LEGACY_LIB legacy_stdio_definitions.lib)
    find_library(WSOCK_LIB wsock32)
    # Do something with ${LEGACY_LIB} and ${WSOCK_LIB}
else()
    ...
endif()

To my surprise, this doesn't work. Both find_librarys fail which sets the variables to NOTFOUND.

How do I fix this? My first guess would be that find_library is not looking in the same places as target_link_libraries, but I did some research and I wasn't able to find where it was looking. What am I missing?

TechnoSam
  • 578
  • 1
  • 8
  • 23
  • 1
    don't know what version of cmake you're using, but for about halfway down the page here it has a numbered list 1-6 of where `find_library` is searching for libraries if no search paths are specified. This is for v3.4: https://cmake.org/cmake/help/v3.4/command/find_library.html – yano Nov 01 '19 at 21:37
  • @yano the `find_library` docs isn't what I need, it's already not finding what I'm looking for. I need the paths that `target_link_libraries` searches to add those to the search for `find_library`. Make sense? – TechnoSam Nov 01 '19 at 21:40
  • 2
    "What is the difference in search paths between target_link_libraries and find_library?" - Search path in `target_link_libraries` is determined by the **linker**, CMake just "feed" the `target_link_libraries`'s arguments to the linker. But searching in `find_library` is performed by **CMake** only. It is true that CMake tends to search similar paths as the linker does, but still the paths are not the same. – Tsyvarev Nov 01 '19 at 21:59
  • Good idea on searching the system, but I really don't think it helps much. I found the lib in the install directory... `MSVS/2019/Community/VC/Tools/MSVC/14.23.28105/lib/onecore/x64`. I would normally expect I could get something more general than trying to guess the target's installation path. – TechnoSam Nov 01 '19 at 22:04
  • @Tsyvarev Ah, that makes a fair amount of sense. Is there any way for me to resolve that? Can I ask the linker to resolve the path and save it in a CMake variable? – TechnoSam Nov 01 '19 at 22:06
  • I know that `gcc` has a `--print-file-name` option with which it prints the absolute path to the library. (see e.g. [there](https://stackoverflow.com/questions/1614769/how-to-i-find-the-filename-of-a-library-via-the-library-name)). But I don't know corresponding option for `cl` compiler used in Visual Studio. – Tsyvarev Nov 01 '19 at 22:11
  • 1
    I agree,, that doesn't look like a path you should have to specify. Looks like [MS linker](https://learn.microsoft.com/en-us/cpp/build/reference/libpath-additional-libpath?view=vs-2019) searches the paths in the LIB env var. If you're using Visual Studio, open a developer prompt (Start --> Visual Studio --> Visual Studio Tools, Native Tools Command Prompt) and enter "set", it will tell you what LIB is set to. Although I don't think that helps your problem here. – yano Nov 01 '19 at 22:19

0 Answers0