Compiling a shared lib

Marcus Clyne
Fri Feb 12 12:36:00 GMT 2010

Andrew Haley wrote:
> On 02/12/2010 10:20 AM, Marcus Clyne wrote:
>> Andrew Haley wrote:
>>> On 02/12/2010 05:53 AM, Marcus Clyne wrote:
>>>> I'm trying to compile a shared library, but am having problems.
>>>> I have one main binary, which will load one or several shared libraries
>>>> that all use a common set of functions.  The functions common to all the
>>>> shared libraries I would like to store either in another shared library,
>>>> which the shared libraries themselves load, or (preferably) in the main
>>>> binary itself.
>>>> I'm using the following commands for compilation (simplified):
>>>> [1] gcc -Wall -fPIC -o shared.o -c file.c
>>>> [2] gcc -shared -o shared.o
>>>> The error message I'm getting is:
>>>> shared.o: relocation R_X86_64_PC32 against undefined symbol `my_func'
>>>> can not be used when making a shared object; recompile with -fPIC
>>>> /usr/bin/ld: final link failed: Bad value
>>>> collect2: ld returned 1 exit status
>>>> Where my_func is one of the common functions that I want to define
>>>> outside the shared library.  I get this both when my_func is defined in
>>>> the main binary, and when it is defined in another shared object
>>>> (libcommon), and I add the following to [2]
>>>> -L/path/to/libcommon/dir -lcommon
>>> That looks right.  Maybe your doesn't have the right name?
>>> Hard to say, you haven't provided enough details.
>> I've changed the name of '' for the example, but it's
>> correct on the system.  In any case, AFAIK, it shouldn't matter - the
>> error is appearing at compile-time (i.e. not load-time), so stage [2] is
>> failing (I forgot to make this clear).
>> I've successfully used the same procedure with other non-defined
>> functions which are in the main binary, and just included a header file
>> (as I did here).
> Well, like I said, you haven't provided enough details for us to be able
> to answer.  What you've posted here looks correct, but we need a real test
> case that we can try.
Found the problem - I'd defined some functions that needed to be 
non-static as static.

Thanks for taking the time to respond.


More information about the Gcc-help mailing list