MantisBT - CMake
View Issue Details
0015630CMakeCMakepublic2015-06-24 04:322016-06-10 14:21
Nicolas Deherly 
 
normalminoralways
closedfixed 
LinuxDebian8.0
CMake 3.0.2 
CMake 3.4CMake 3.4 
0015630: ENABLE_EXPORTS value is not used for gcc makefile -rdynamic option
Whatever is the value of ENABLE_EXPORTS, executable are linked with the -rdynamic option.

I have to reset the __linux_compiler_gnu macro to link without -rdynamic parameter.
This bug has already been opened (9985).
No tags attached.
related to 0009985closed Brad King Linux.cmake should not hard-code -rdynamic 
Issue History
2015-06-24 04:32Nicolas DeherlyNew Issue
2015-06-24 08:42Brad KingRelationship addedrelated to 0009985
2015-06-24 08:45Brad KingNote Added: 0038964
2015-06-24 08:50Nicolas DeherlyNote Added: 0038967
2015-06-24 09:07Brad KingNote Added: 0038969
2016-03-14 12:08Bernd LörwaldNote Added: 0040680
2016-03-14 13:04Brad KingNote Added: 0040681
2016-03-14 13:04Brad KingStatusnew => resolved
2016-03-14 13:04Brad KingResolutionopen => fixed
2016-03-14 13:04Brad KingFixed in Version => CMake 3.4
2016-03-14 13:04Brad KingTarget Version => CMake 3.4
2016-06-10 14:21Kitware RobotNote Added: 0041228
2016-06-10 14:21Kitware RobotStatusresolved => closed

Notes
(0038964)
Brad King   
2015-06-24 08:45   
Yes. The current behavior is historical, left from before we had an explicit ENABLE_EXPORTS option, and has been preserved for compatibility.

If anyone wants to work on this, it will require a policy to change such behavior while preserving compatibility for existing projects.
(0038967)
Nicolas Deherly   
2015-06-24 08:50   
If ENABLE_EXPORTS default value is ON, the historical behaviour is kept and it is possible to remove -rdynamic option by setting ENABLE_EXPORTS to OFF (as I was expecting).
(0038969)
Brad King   
2015-06-24 09:07   
Re 0015630:0038967: The ENABLE_EXPORTS option default is OFF. It is meant for use when the executable intends to support plugins and enables allowing MODULE libraries to reference the executable as the intended loader when they are linked. It has effects other than just an equivalent to -rdymamic.

The -rdynamic option has been used since CMake's earliest days, along with similar options on other platforms. Fixing this will require auditing the effects of all options named in CMAKE_SHARED_LIBRARY_LINK_<LANG>_FLAGS settings in Modules/Platform/*.cmake to see which ones need to be converted to be based on ENABLE_EXPORTS.
(0040680)
Bernd Lörwald   
2016-03-14 12:08   
As noted in (closed) issue 9985, this can be worked around with unsetting CMAKE_SHARED_LIBRARY_LINK_<LANG>_FLAGS.

This does not allow to only override this for specific targets only though, as the value is not used at declaration of the target but afterwards:

$ touch foo.cpp

$ cat > CMakeLists.txt
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
add_executable (foo "foo.cpp")

$ cmake . -G "Unix Makefiles" >/dev/null 2>&1 && cat CMakeFiles/foo.dir/link.txt
/soft/clang/3.7.1/bin/clang++ -O3 -DNDEBUG CMakeFiles/foo.dir/foo.cpp.o -o foo

$ cat > CMakeLists.txt
set (safe ${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS})
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
add_executable (foo "foo.cpp")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS ${safe})

$ cmake . -G "Unix Makefiles" >/dev/null 2>&1 && cat CMakeFiles/foo.dir/link.txt
/soft/clang/3.7.1/bin/clang++ -O3 -DNDEBUG CMakeFiles/foo.dir/foo.cpp.o -o foo -rdynamic

Overwriting the LINK_FLAGS property does nothing there either, as that's additional only:

$ cat > CMakeLists.txt
set (safe ${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS})
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
add_executable (foo "foo.cpp")
set_property (TARGET foo PROPERTY LINK_FLAGS "-static")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS ${safe})

$ cmake . -G "Unix Makefiles" >/dev/null 2>&1 && cat CMakeFiles/foo.dir/link.txt
/soft/clang/3.7.1/bin/clang++ -O3 -DNDEBUG -static CMakeFiles/foo.dir/foo.o -o foo -rdynamic


As we too do not want to audit the effects of -rdynamic on all of our targets, this is quite a bummer. The only thing that worked would be adding -Wl,--no-export-dynamic (which is the inverse of -rdynamic) to LINK_FLAGS, but that was added to ld in 2009 only, so distros like centos5 do not have that flag.

Am I missing something here or does this really mean that having a single target without -rdynamic is not possible, unless doing it the other way around and specifying -rdynamic for all but one target?
(0040681)
Brad King   
2016-03-14 13:04   
The audit mentioned in 0015630:0038969 has been done.

Since CMake 3.4 one can set policy CMP0065:

  https://cmake.org/cmake/help/v3.5/policy/CMP0065.html [^]

to disable use of -rdynamic outright, except for executables where it is explicitly requested via the ENABLE_EXPORTS property.

Using

  cmake_minimum_required(VERSION 3.4)

at the top of a project is sufficient to set CMP0065 to NEW. Otherwise an explicit

  if(POLICY CMP0065)
    cmake_policy(SET CMP0065 NEW)
  endif()

will do it.
(0041228)
Kitware Robot   
2016-06-10 14:21   
This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.