1

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?

Kevin
  • 16,549
  • 8
  • 60
  • 74
John K
  • 496
  • 3
  • 14
  • It is unlikely that `find_package` for TIFF uses `CMAKE_EXE_LINKER_FLAGS` variable. E.g. the [one shipped with CMake itself](https://github.com/Kitware/CMake/blob/master/Modules/FindTIFF.cmake) doesn't use this variable. So your problem in finding TIFF has, probably, some other reasons than setting the variable `CMAKE_EXE_LINKER_FLAGS`. – Tsyvarev Jun 16 '20 at 18:04
  • While FindTIFF.cmake in /usr/share/cmake/Modules doesn't reference the variable it calls other functions that might. Clearly the parameter makes a difference because that's the only change I need to make to get it to work in a minimum example. My CMake file doesn't even build anything it's just those two lines plus the project declaration (to which I only provide a name). – John K Jun 16 '20 at 18:11
  • 1
    "it fails the first time, but puts "-dynamic" in the cache and then works on the second run." - This smells like setting of `CMAKE_EXE_LINKER_FLAGS` affects on the **compiler** detection, which is performed on `project()` call. This detection in turn may set additional directories for search a library or add additional library extensions. Since I am not very familiar with compilers on Cray, above are only a guess. You may try to set `CMAKE_EXE_LINKER_FLAGS` variable **before** the `project()` call. This way your project would work identically both the first and following calls. – Tsyvarev Jun 16 '20 at 19:49
  • @Tsyvarev That's it. `find_package()` couldn't find it because the search paths came from the compiler and the search paths are different depending on the linker flags. When I moved `set(...)` before `project()` it didn't need the `CACHE` part either. Care to write up an answer so I can approve it? – John K Jun 16 '20 at 21:04

2 Answers2

0

For the Cray programming environment and dynamic linking, I usually find that it is best to set the environment variable CRAYPE_LINK_TYPE=dynamic before configuring and building. This usually plays nicer with any other modules that you may be using.

Rupert Nash
  • 1,360
  • 12
  • 20
  • That is correct, but it’s not the point of the question. If I needed a different linker flag I’d still have the problem. – John K Jun 18 '20 at 11:59
0

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.

John K
  • 496
  • 3
  • 14