| | |
g++ - linking error - undefined reference to <func_name>
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 4
Reputation:
Solved Threads: 0
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:
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
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)
../lib/libPixyCANopen.so: undefined reference to `coTimerPulse' ../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
> 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.
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.
•
•
Join Date: May 2009
Posts: 109
Reputation:
Solved Threads: 16
•
•
•
•
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?
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.
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
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
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.
•
•
Join Date: Jun 2009
Posts: 4
Reputation:
Solved Threads: 0
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
Makefile #2
Makefile #3
Existence of coTimerPulse in code is:
Makefile #1
C++ Syntax (Toggle Plain Text)
root:/tmp/trunk/CANopenStack/source# make CC=g++ 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ar: creating ./lib/libCanOpen.a
Makefile #2
C++ Syntax (Toggle Plain Text)
root:/tmp/trunk/PIXYapi# make CC=g++ 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 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 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 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 g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o co_init.o co_init.c g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o nmtslave.o nmtslave.c g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o objects.o objects.c g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o usr_301.o usr_301.c g++ -Wall -fPIC -shared -DNO_RT -I. -I./include -I../CANopenStack/include -I../CANdriver/include -c -o PixyCANopen.o PixyCANopen.c 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)
root:/tmp/trunk/PIXYapi/test# make 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 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 ../lib/libPixyCANopen.so: undefined reference to `coTimerPulse'
Existence of coTimerPulse in code is:
C++ Syntax (Toggle Plain Text)
root:/tmp/trunk# grep -nR "coTimerPulse" * 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 */ CANdriver/src/cpu_generic.c:169:UNSIGNED16 CO_CONST coTimerPulse = CONFIG_TIMER_INC; /**< length of 1 tick / unit 100us */ CANopenStack/include/co_drv.h:149:extern UNSIGNED16 coTimerPulse; CANopenStack/include/co_drv.h:151:extern UNSIGNED16 CO_CONST coTimerPulse; Binary file CANopenStack/source/timer.o matches CANopenStack/source/lme.c:533: GL_VAR(coTimerPulse) = CONFIG_TIMER_INC; Binary file CANopenStack/source/lib/libCanOpen.a matches CANopenStack/source/globvars.c:261://UNSIGNED16 coTimerPulse; CANopenStack/source/timer.c:565: if (timerVal > ((UNSIGNED32)GL_VAR(coTimerPulse) << 15)) { CANopenStack/source/timer.c:573: if ((timerVal + pTimer->restTime) <= GL_VAR(coTimerPulse)) { CANopenStack/source/timer.c:580: pTimer->endTime = tVal / GL_VAR(coTimerPulse); CANopenStack/source/timer.c:582: pTimer->endTime = (UNSIGNED16)(tVal / GL_VAR(coTimerPulse)); CANopenStack/source/timer.c:585: pTimer->restTime = (UNSIGNED16)(tVal % GL_VAR(coTimerPulse)); CANopenStack/source/timer.c:744: retVal = addTimerEvent(pTimer, pTimer->endTime * GL_VAR(coTimerPulse), CANopenStack/source/timer.c:829: ticks = timerVal / GL_VAR(coTimerPulse); CANopenStack/source/timer.c:831: if ((ticks * GL_VAR(coTimerPulse)) < timerVal) { GL_VAR(coTimerPulse) = CONFIG_TIMER_INC; Binary file PIXYapi/test/lib/libPixyCANopen.so matches 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
•
•
Join Date: Jun 2009
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Another [Linker error] undefined reference to `WinMain@16' (C++)
- [Linker error] undefined reference to `SDL_Init' (C++)
- [Linker error] undefined reference to `D3DXCreateFontA@48' (C++)
- Linker Error (Undefined Reference) (C++)
- [linker error] undefined reference - why am I getting this error? (C++)
- [Linker error] undefined reference to `CQTMovieFile::CQTMovieFile()' (C++)
- What is it meaned [Linker error] undefined reference to `MAPILogon@24' (C++)
- Dev-C++, GLUI linker error, undefined reference (C++)
Other Threads in the C++ Forum
- Previous Thread: Inheritance in STL
- Next Thread: Memory leaks in a dll based VC++ project
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






