Bug #527

must include -fPIC C flag when making static libs in x86_64 with gcc

Added by Eduardo over 3 years ago. Updated almost 3 years ago.

Status:ClosedStart date:2014-03-08
Priority:NormalDue date:
Assignee:Florian% Done:

100%

Category:-
Target version:Candidate for next bugfix release
Affected version:5.1.1 Platform:Linux

Description

ie:

/usr/bin/ld: error: libgmt.a(common_runpath.c.o): requires dynamic R_X86_64_32 reloc against 'GMT_runtime_libdir' which may overflow at runtime; recompile with -fPIC

Associated revisions

Revision 13634
Added by Paul almost 3 years ago

Apply fix for static libs, see issue #527

History

#1 Updated by Paul over 3 years ago

  • Status changed from New to In Progress
  • Assignee set to Florian

Does this mean that cmake, when asked to build static libs, fails to add that compiler flag? In the ConfigUser.cmake we have

# Enable building of shared libraries [TRUE] (disable to use static libraries;
# not recommended):
#set (BUILD_SHARED_LIBS FALSE)

so we don't recommend static builds. Florian, does it make any sense to add something like
add_definitions(-fPIC)
if BUILD_SHARED_LIBS is false? Or would that simply be opening a can of wiggly worms?

#2 Updated by Florian almost 3 years ago

I would avoid adding compiler specific flags. Eduardo, please try this:

set (BUILD_SHARED_LIBS FALSE)
set_property(TARGET gmtlib PROPERTY POSITION_INDEPENDENT_CODE TRUE)
See also: http://www.cmake.org/cmake/help/v2.8.9/cmake.html#prop_tgt:POSITION_INDEPENDENT_CODE

#3 Updated by Eduardo almost 3 years ago

I understand your point -fPIC is gcc centric
With

set_property(TARGET gmtlib PROPERTY POSITION_INDEPENDENT_CODE TRUE)

in ConfigUser.cmake I get this error:
-- The C compiler identification is GNU 4.9.1
-- The CXX compiler identification is GNU 4.9.1
-- Check for working C compiler: /opt/apps/gcc/gcc-4.9.1/bin/gcc
-- Check for working C compiler: /opt/apps/gcc/gcc-4.9.1/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /opt/apps/gcc/gcc-4.9.1/bin/c++
-- Check for working CXX compiler: /opt/apps/gcc/gcc-4.9.1/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at cmake/ConfigUser.cmake:200 (set_property):
  set_property could not find TARGET gmtlib.  Perhaps it has not yet been
  created.
Call Stack (most recent call first):
  cmake/modules/ConfigCMake.cmake:23 (include)
  CMakeLists.txt:57 (include)

#4 Updated by Florian almost 3 years ago

Right, the target does not exist yet. Maybe try this global variable:

set (CMAKE_POSITION_INDEPENDENT_CODE TURE)

#5 Updated by Eduardo almost 3 years ago

It works with

set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)

cmake adds -fPIC compile flag.

#6 Updated by Paul almost 3 years ago

In case Florian is off, in which file did you add the
set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
line? Presumably in the top-level CMakeList.txt so lines 127 onwards read

if (BUILD_SHARED_LIBS)
set (BUILD_MODE "shared")
else (BUILD_SHARED_LIBS)
set (BUILD_MODE "static")
set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
endif (BUILD_SHARED_LIBS)

Seems to be the right place for it, no?

#7 Updated by Eduardo almost 3 years ago

I've added it to ConfigUser.cmake because I avoid to modify your sources.

The place you suggested is right to me.

#8 Updated by Paul almost 3 years ago

  • Status changed from In Progress to Resolved
  • % Done changed from 0 to 100

I have applied this fix to CMakeList.txt in r13634. Please comment out your fix and see if this works with you just setting set (BUILD_SHARED_LIBS FALSE)
in your cmake/ConfiguUser.cmake file.

#9 Updated by Florian almost 3 years ago

Hi, I already modified ConfigUserTemplate.cmake but could not yet commit the change:

+++ cmake/ConfigUserTemplate.cmake    (working copy)
@@ -139,9 +139,13 @@
 #set (UNITS "US")

 # Enable building of shared libraries [TRUE] (disable to use static libraries;
-# not recommended):
+# not recommended; on non-x86 architectures uncomment the next option as well):
 #set (BUILD_SHARED_LIBS FALSE)

+# Create position independent code on all targets [auto] (needed for static
+# build on non-x86:
+#set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
+
 # Build GMT shared lib with supplemental modules [TRUE]:
 #set (BUILD_SUPPLEMENTS FALSE)

I recommend removing the smart code in CMakeList.txt because PIC is needed only on x86_64.

#10 Updated by Paul almost 3 years ago

OK, updated in r13636 per your instructions.

#11 Updated by Eduardo almost 3 years ago

It works for me.

#12 Updated by Paul almost 3 years ago

  • Status changed from Resolved to Closed

Closing this issue.

Also available in: Atom PDF