🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Link error in C++, but not in C ?

Started by
4 comments, last by rip-off 16 years, 3 months ago
Hi guys, I'm very much a new comer to unix programming - I'm working in Sol 10. I've got a shared library file which I need to link into my program, and I call one of the functions very simply in the main.c file. I'm using the gcc compiler. This works fine. However, I want to use C++ - so I renamed my file main.cpp, changed the compiler to g++ and suddenly it can't find the symbol in the library. I am a real beginner here so I'd very much appreciate any insight. My makefile code is quite simple;

LIBDIR = /opt/devpak
LIBS=$(LIBDIR)/libgctlib.so
CC=g++

all: RDS

RDS: main.o
        $(CC) main.o -o RDS $(LIBS)

main.o: main.cpp
        $(CC) -c main.cpp

clean:
        rm -rf main.o
Thanks.
- Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
Advertisement
So what symbol is the linker not finding?
It's just a function

Undefined                       first referenced symbol                             in fileGCT_receive()                       main.old: fatal: Symbol referencing errors. No output written to RDScollect2: ld returned 1 exit status*** Error code 1make: Fatal error: Command failed for target `RDS'

- Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
Is the library header file wrapped in something like this:
#ifndef SOME_LIBRARY_H#define SOME_LIBRARY_H#ifdef __cplusplusextern "C" {#endif// declarations#ifndef __cplusplus}#endif#endif


If not, you will have to wrap the includes in an extern "C" block, to disable C++ name mangling.
Thanks rip-off!

That did the trick, so it wasn't a unix specific thing after all... So when C++ attempts to access an external symbol there's some crazy name mangling going on right? Mental note made :)

I noticed in the header file there's a #ifdef LINT_ARGS which only appears to come into force when using a C++ compiler. Is this also something native that I should know about?

Many many thanks again!
- Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
C++ allows function overloading, symbols in namespaces and member functions, templates etc to all share the same name. However, (from what I understand) most linkers don't understand C++ symbols. What compilers do is Name Mangling, allowing all these symbols to share the same name in your C++ source while the linker can differentiate them.

This is one of the reasons why C interfaces are often preferred for libraries (even if the library itself is written in C++). Different compilers (or even different versions of the same compiler) can have different name mangling schemes.

This topic is closed to new replies.

Advertisement