GCC undefined object reference

Pandurangan R S pandurangan.r.s@gmail.com
Fri Aug 12 05:13:00 GMT 2011


There is a common mistake that I am aware of. Illustrated (problem and
solution) below for easy understanding. Hope this helps.

user@host:~/tmp/test1$ cat master.c
int main()
{
    slave();
      return 1;
}
user@host:~/tmp/test1$ cat slave.c
void slave()
{
}
user@host:~/tmp/test1$ gcc -o slave.o -c slave.c
user@host:~/tmp/test1$ ar rcs libslave.a slave.o
user@host:~/tmp/test1$ gcc -o master -L./ -lslave master.c
/tmp/ccag46Wr.o: In function `main':
master.c:(.text+0xa): undefined reference to `slave'
                      collect2: ld returned 1 exit status
user@host:~/tmp/test1$ echo $?
1
user@host:~/tmp/test1$ gcc -o master master.c -L./ -lslave
user@host:~/tmp/test1$ echo $?
0


On Fri, Aug 12, 2011 at 9:30 AM, manaankit2 <ankit.anand1@wipro.com> wrote:
>
> I have tried using the library path in the LD_LIBRARY_PATH and also using the
> -L option in the Makefile.
> Can you suggest me something for this.
>
> Jonathan Wakely-4 wrote:
>>
>> On 11 August 2011 12:36, manaankit2 wrote:
>>>
>>> Hello,
>>>
>>> I have a c file which uses the function defined in a library. In my
>>> makefile
>>> i am providing the -lCOM option with gcc. Where COM is the library. The
>>> Library Path is there in the LD_LIBRARY_PATH.
>>>
>>> But when i run the gmake, i get the error which says that undefined
>>> Object
>>> reference.
>>> That function is there in the COM lib(libCOM.a).
>>>
>>> Can someone help me with the same?
>>
>> This is not really a gcc question, it's about your linker.
>>
>> On some platforms (e.g. Solaris) the linker will look in
>> LD_LIBRARY_PATH, on others (e.g. GNU/Linux) it won't and you need to
>> use -L to specify the directory containing libCOM.a
>>
>>
>
> --
> View this message in context: http://old.nabble.com/GCC-undefined-object-reference-tp32241304p32247042.html
> Sent from the gcc - Help mailing list archive at Nabble.com.
>
>



More information about the Gcc-help mailing list