cmakelibtiffcray

Why is CMake find_package ignoring linker flags?


I'm developing on a Cray system that requires '-dynamic' be passed before dynamic libraries can be found. In my case I'm trying to link with libtiff. The command cc main.cpp -ltiff fails with a file not found but cc main.cpp -dynamic -ltiff works. I believe this file not found behavior is tripping up CMake's find_package.

When I use CMake (version 3.5.2) and pass -DCMAKE_EXE_LINKER_FLAGS:STRING="-dynamic" from the command line find_package(TIFF) works. But when I set it in CMakeLists.txt like this:

set(CMAKE_EXE_LINKER_FLAGS "-dynamic")
find_package(TIFF REQUIRED)

it can't find libtiff. But if I try:

set(CMAKE_EXE_LINKER_FLAGS "-dynamic" CACHE STRING "" FORCE)
find_package(TIFF REQUIRED)

it fails the first time, but puts "-dynamic" in the cache and then works on the second run. If I manually set TIFF_LIBRARY_RELEASE to point to the .so then find_package will also work (by filling out the rest of the variables).

What is the proper way of doing this without passing it as a parameter?


Solution

  • Per @Tsyvarev's comment above:

    Compilers are probed when you call project(). So in this case, where a compiler flag will change the library and include paths, you have to set CMAKE_EXE_LINKER_FLAGS before calling project.