g++ - linking error - undefined reference to <func_name>

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 4
Reputation: anirbanjoy is an unknown quantity at this point 
Solved Threads: 0
anirbanjoy anirbanjoy is offline Offline
Newbie Poster

g++ - linking error - undefined reference to <func_name>

 
0
  #1
Jun 17th, 2009
Hi,

I have 3 seperate make file where,
1st one generates a static library libCanOpen.a,
(using ""ar -r ")
2nd one creates a dynamically linkable library libPixyCANopen.so
(using "g++ -Wall -fPIC -shared")
and the 3rd one creates the executable by linking it to libPixyCANopen.so.

The 1st two makefiles build without a single warning (-Wall is used)
The last Makefile is generating linking error like the following:

  1.  
  2. ../lib/libPixyCANopen.so: undefined reference to `coTimerPulse'
  3. ../lib/libPixyCANopen.so: undefined reference to `releaseTimer'

nm command shows that the corresponding object names are mangled (e.g. _Z12releaseTimerv), Using extern "C" in the header file did not remove it.

But, I am using g++ for every compilation, so I think the mangling should not matter.

Any suggestion is appreciated.

Thanks,
Anirban
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: g++ - linking error - undefined reference to <func_name>

 
0
  #2
Jun 17th, 2009
> nm command shows that the corresponding object names are mangled (e.g. _Z12releaseTimerv),
Try adding the extern "C" to the implementation of those functions as well.

> Using extern "C" in the header file did not remove it.
But it did make it so that the users of that function did get the unmangled name.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 4
Reputation: anirbanjoy is an unknown quantity at this point 
Solved Threads: 0
anirbanjoy anirbanjoy is offline Offline
Newbie Poster

Re: g++ - linking error - undefined reference to <func_name>

 
0
  #3
Jun 17th, 2009
Thanks .. that did help ..

There is only one linking error now, it's an extern variable .. the problem is same .. "undefined reference to <var_name>" ..

Need to share this variable .. that means I have to keep that extern stuff .. but, how?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 109
Reputation: Stinomus is on a distinguished road 
Solved Threads: 16
Stinomus Stinomus is offline Offline
Junior Poster

Re: g++ - linking error - undefined reference to <func_name>

 
0
  #4
Jun 17th, 2009
Originally Posted by anirbanjoy View Post
Thanks .. that did help ..

There is only one linking error now, it's an extern variable .. the problem is same .. "undefined reference to <var_name>" ..

Need to share this variable .. that means I have to keep that extern stuff .. but, how?
The short answer to that one is that you should not have public access to the variable, access should be wrapped within get/set methods.

And you shouldn't have to worry about name-mangling, declaring functions as extern C isn't necessary. What do your makefiles look like?
Last edited by Stinomus; Jun 17th, 2009 at 7:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: g++ - linking error - undefined reference to <func_name>

 
0
  #5
Jun 17th, 2009
It's shouldn't be necessary to use extern,
generally you have access to all the public variables and function in the symboltable, but only the externed are required to have the same signature, from version to version.
This is atleast how it should be.

http://people.redhat.com/drepper/dsohowto.pdf

try doing a ldd on your compiled files to see if anything is missing
Last edited by monkey_king; Jun 17th, 2009 at 10:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 4
Reputation: anirbanjoy is an unknown quantity at this point 
Solved Threads: 0
anirbanjoy anirbanjoy is offline Offline
Newbie Poster

Re: g++ - linking error - undefined reference to <func_name>

 
0
  #6
Jun 18th, 2009
This was my assumption too. As I am using g++ for all the cmpilation, extern "C" stuff does not seem logical. however, here are the outputs of 3 Makefiles in order:

Makefile #1
  1. root:/tmp/trunk/CANopenStack/source# make CC=g++
  2. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o access_o.o access_o.c
  3. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o cmsmain.o cmsmain.c
  4. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o nmt.o nmt.c
  5. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o cmscodec.o cmscodec.c
  6. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o sdoserv.o sdoserv.c
  7. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o sdoclien.o sdoclien.c
  8. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o sdomain.o sdomain.c
  9. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o sdo.o sdo.c
  10. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o emerg.o emerg.c
  11. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o lme.o lme.c
  12. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o pdo.o pdo.c
  13. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o set_com.o set_com.c
  14. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o sync.o sync.c
  15. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o utility.o utility.c
  16. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o time.o time.c
  17. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o timer.o timer.c
  18. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o nmterr.o nmterr.c
  19. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o nmt_s.o nmt_s.c
  20. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o led.o led.c
  21. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o co_debug.o co_debug.c
  22. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o heartbt.o heartbt.c
  23. g++ -Wall -I/tmp/trunk/PIXYapi/include -I/tmp/trunk/PIXYapi -I../include -I. -I/tmp/trunk/CANdriver/include -c -o slave_ut.o slave_ut.c
  24. ar -r ./lib/libCanOpen.a access_o.o cmsmain.o nmt.o cmscodec.o sdoserv.o sdoclien.o sdomain.o sdo.o emerg.o lme.o pdo.o set_com.o sync.o utility.o time.o timer.o nmterr.o nmt_s.o led.o co_debug.o heartbt.o slave_ut.o
  25. ar: creating ./lib/libCanOpen.a

Makefile #2
  1. root:/tmp/trunk/PIXYapi# make CC=g++
  2. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o ../CANdriver/src/can_p_l.o ../CANdriver/src/can_p_l.c
  3. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o ../CANdriver/src/cpu_generic.o ../CANdriver/src/cpu_generic.c
  4. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o ../CANdriver/src/cdriver.o ../CANdriver/src/cdriver.c
  5. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o ../CANdriver/src/cpu.o ../CANdriver/src/cpu.c
  6. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o co_init.o co_init.c
  7. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o nmtslave.o nmtslave.c
  8. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o objects.o objects.c
  9. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o usr_301.o usr_301.c
  10. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o PixyCANopen.o PixyCANopen.c
  11. g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -shared -o lib/libPixyCANopen.so ../CANdriver/src/can_p_l.o ../CANdriver/src/cpu_generic.o ../CANdriver/src/cdriver.o ../CANdriver/src/cpu.o co_init.o nmtslave.o objects.o usr_301.o PixyCANopen.o -L../CANopenStack/source/lib/ -L./lib -lpcan -lCanOpen -lh8com

Makefile #3

  1. root:/tmp/trunk/PIXYapi/test# make
  2. g++ -g -Wall -DNO_RT -I../include/ -I../ -I../../CANopenStack/include/ -I../../CANdriver/include/ -I../CANopenStack/source -c -o src/main.o src/main.cpp
  3. g++ -g -Wall -DNO_RT -I../include/ -I../ -I../../CANopenStack/include/ -I../../CANdriver/include/ -I../CANopenStack/source -L../lib -lPixyCANopen -o ApiTest src/main.o
  4. ../lib/libPixyCANopen.so: undefined reference to `coTimerPulse'
  5.  

Existence of coTimerPulse in code is:
  1. root:/tmp/trunk# grep -nR "coTimerPulse" *
  2. CANdriver/src/.svn/text-base/cpu_generic.c.svn-base:164:VOLATILE UNSIGNED16 CO_CONST coTimerPulse = CONFIG_TIMER_INC; /**< length of 1 tick / unit 100us */
  3. CANdriver/src/cpu_generic.c:169:UNSIGNED16 CO_CONST coTimerPulse = CONFIG_TIMER_INC; /**< length of 1 tick / unit 100us */
  4. CANopenStack/include/co_drv.h:149:extern UNSIGNED16 coTimerPulse;
  5. CANopenStack/include/co_drv.h:151:extern UNSIGNED16 CO_CONST coTimerPulse;
  6. Binary file CANopenStack/source/timer.o matches
  7. CANopenStack/source/lme.c:533: GL_VAR(coTimerPulse) = CONFIG_TIMER_INC;
  8. Binary file CANopenStack/source/lib/libCanOpen.a matches
  9. CANopenStack/source/globvars.c:261://UNSIGNED16 coTimerPulse;
  10. CANopenStack/source/timer.c:565: if (timerVal > ((UNSIGNED32)GL_VAR(coTimerPulse) << 15)) {
  11. CANopenStack/source/timer.c:573: if ((timerVal + pTimer->restTime) <= GL_VAR(coTimerPulse)) {
  12. CANopenStack/source/timer.c:580: pTimer->endTime = tVal / GL_VAR(coTimerPulse);
  13. CANopenStack/source/timer.c:582: pTimer->endTime = (UNSIGNED16)(tVal / GL_VAR(coTimerPulse));
  14. CANopenStack/source/timer.c:585: pTimer->restTime = (UNSIGNED16)(tVal % GL_VAR(coTimerPulse));
  15. CANopenStack/source/timer.c:744: retVal = addTimerEvent(pTimer, pTimer->endTime * GL_VAR(coTimerPulse),
  16. CANopenStack/source/timer.c:829: ticks = timerVal / GL_VAR(coTimerPulse);
  17. CANopenStack/source/timer.c:831: if ((ticks * GL_VAR(coTimerPulse)) < timerVal) {
  18. GL_VAR(coTimerPulse) = CONFIG_TIMER_INC;
  19. Binary file PIXYapi/test/lib/libPixyCANopen.so matches
  20. Binary file PIXYapi/lib/libPixyCANopen.so matches

Originally Posted by monkey_king View Post
It's shouldn't be necessary to use extern,
generally you have access to all the public variables and function in the symboltable, but only the externed are required to have the same signature, from version to version.
This is atleast how it should be.

http://people.redhat.com/drepper/dsohowto.pdf

try doing a ldd on your compiled files to see if anything is missing
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: g++ - linking error - undefined reference to <func_name>

 
0
  #7
Jun 18th, 2009
Could you try doing a
'ldd libPixyCANopen.so'
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 4
Reputation: anirbanjoy is an unknown quantity at this point 
Solved Threads: 0
anirbanjoy anirbanjoy is offline Offline
Newbie Poster

Re: g++ - linking error - undefined reference to <func_name>

 
0
  #8
Jun 18th, 2009
root:/tmp/trunk/PIXYapi/lib# ldd libPixyCANopen.so
libpcan.so.0 => /usr/lib/libpcan.so.0 (0x00138000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x0013b000)
libm.so.6 => /lib/libm.so.6 (0x0021c000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00241000)
libc.so.6 => /lib/libc.so.6 (0x0024c000)
/lib/ld-linux.so.2 (0xb7f07000)$

I removed the extern variable and now the linking problem is gone .. but still I will keep trying to remove the extern "C" completely from my code.

Originally Posted by monkey_king View Post
Could you try doing a
'ldd libPixyCANopen.so'
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC