943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4909
  • C++ RSS
Jun 17th, 2009
0

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

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anirbanjoy is offline Offline
4 posts
since Jun 2009
Jun 17th, 2009
0

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

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 17th, 2009
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anirbanjoy is offline Offline
4 posts
since Jun 2009
Jun 17th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by anirbanjoy ...
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.
Reputation Points: 58
Solved Threads: 17
Junior Poster
Stinomus is offline Offline
109 posts
since May 2009
Jun 17th, 2009
0

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

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.
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jun 18th, 2009
0

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

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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anirbanjoy is offline Offline
4 posts
since Jun 2009
Jun 18th, 2009
0

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

Could you try doing a
'ldd libPixyCANopen.so'
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jun 18th, 2009
0

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

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.

Could you try doing a
'ldd libPixyCANopen.so'
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anirbanjoy is offline Offline
4 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Inheritance in STL
Next Thread in C++ Forum Timeline: Memory leaks in a dll based VC++ project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC