Problem Accessing C++ Code From Within C Source

Hi fellows...

Today I'd like to steal you a little of your valuable time to see if you can help me solve a puzzle in which I introduced myself.

I'm developing a library, writen in C++, that must be callable from a program written in C-code.
I paste the code below:

The C++ Library (cpp_funcs.cpp):
====================================================================
#include <iostream>

extern "C"
int function1(int x)
{
std::cout << "Returning (" << x << " + 1) = [" << (x + 1) << "]" << std::endl;
return x + 1;
}
====================================================================

The C Program (main.c):
====================================================================
#include <stdio.h>
#include <stdlib.h>

extern int function1(int x);

int main(int argc, char** argv)
{
printf("function1: %d\n", function1(10));
return (EXIT_SUCCESS);
}
====================================================================

1) I compile the C++ Library with the following command:
CC -G -o ./libCPP.so ./cpp_funcs.cpp

2) and then compile the program with the following command:
cc -o ./binary ./main.c ./libCPP.so

the library compilation runs smoothly, but the program compilation gives the following output:

==============================================================================
Undefined first referenced
symbol in file
__1cDstd2l6Frn0ANbasic_ostream4Ccn0ALchar_traits4Cc____pkc_2_ ./libCPP.so
__1cDstdEcout_ ./libCPP.so
ld: fatal: Symbol referencing errors. No output written to ./binary
==============================================================================

Apparently (to my humble comprehension) the linker can't bind those two symbols

...well, I interrupt the description of this problem, (this may sound crazy) but in the interim that I was describing this problem, I have come to a solution, and as I have come this far, I'll describe where was my mistake.

The problem ocurred because when I issued the second command, the linker couldn't determine where those two "undefined symbols" listed above resided, and they were "undefined" because when I issued the first command I forgot to specify where they were. So, instead I modified the first command for the following:

CC -G -o ./libCPP.so ./cpp_funcs.cpp -lCstd

(note that I added the "Cstd" library, and that's where those two symbols reside)

So, what apparently ocurred was that, as I didn't specify the location of those symbols, the resolution (or binding) of them both were delayed to a later time, and that "later time" never came, so -obviously- the linker wasn't able to produce a complete binary with all of the symbols resolved.

When I finally added the "-lCstd" parameter to the first command, that resolution wasn't necessary in a posterior time, so I was able to put all of the C++ code that I wanted in the C++ library, and then call a <extern "C">'ed function from the entrails of a C program.

Final Conclusion: Everytime that you got an "Undefined Symbol" error, there is some reference to an external function/variable/something that hasn't been resolved.

Well, hope that helps in a future :).

Cheers!!

Juanjo
AsunciĆ³n - Paraguay
Topic archived. No new replies allowed.