[CMake] Override CMAKE_SHARED_LINKER_FLAGS for one particular library

Attila Krasznahorkay attila.krasznahorkay at gmail.com
Mon May 2 04:00:50 EDT 2016


Dear All,

I have a similar issue as Matt Keeler. In our project we have some global linker flags that we set for all targets using the CMAKE_EXE_LINKER_FLAGS, CMAKE_SHARED_LINKER_FLAGS and CMAKE_MODULE_LINKER_FLAGS cache variables.

Now it turns out that for some libraries in the project I need to modify these flags. I can't modify the flags on the directory level, I need to modify them at the target level.

I tried just setting extra flags in LINK_FLAGS for my specific targets. But the problem is that the placement of the flags set in this target specific property is not "well defined" wrt. the global properties.

In this particular case we use "-Wl,--as-needed" globally for all of our targets. But for some particular libraries I'd like to turn this off. When I add "-Wl,--no-as-needed" to the LINK_FLAGS property of this library, it is put before the other flag in my final build configuration. And as such, it has no effect.

I tried modifying CMAKE_SHARED_LINKER_FLAGS before the add_library call for this library, and then restoring it to its original value afterwards. (Since as I said, I can't modify the flag for the entire directory.) But this didn't work as expected. Since by the generation stage CMake doesn't remember that the variable was set differently in the different lines of the directory's CMakeLists.txt file.

So, any ideas? I'd really hate to give up on using these global variables, and starting to set the flags one by one for all of our targets. Since it's a lot less work to just remove the flag for the targets that don't need it.

Cheers,
            Attila


More information about the CMake mailing list