Advertisement Jump to content
Sign in to follow this  

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

This topic is 3960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

all: RDS

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

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

        rm -rf main.o

Share this post

Link to post
Share on other sites
It's just a function

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

Share this post

Link to post
Share on other sites
Is the library header file wrapped in something like this:


#ifdef __cplusplus
extern "C" {

// declarations

#ifndef __cplusplus


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

Share this post

Link to post
Share on other sites
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!

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!