Shared library compiled by Sun studio can't be used by G++

H

HelloLinux

The following is how I encouted my problems:

1) Compile print.cpp into libprint.so by Sun C++ 5.9.The command is:
CC -G -KPIC -o libprint.so print.cpp
It succeeds.

2)Compile debug.cpp into libdebug.so by g++ 3.4.6, debug.cpp calls a
function defined in libprint.so. The command is:
g++ -shared -fPIC -o libdebug.so debug.cpp -lprint -L.
It succeeds too.

3)Compile main.cpp into a.out. main.cpp use a function defined in
libdebug.so. The command is:
g++ -fPIC main.cpp -ldebug -L.
It failed.The error msg is like below.

Undefined first referenced
symbol in file
print(char const*) ./libdebug.so
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

What I can't understand is that why step 2 succeed but then step 3
failed.
Do I miss some option when invoking g++ in step 3?

Any help will be appreciated, Thank in advance!

The code are here.

//print.cpp
#include "stdio.h"
#include "print.h"
void print(const char *msg)
{
printf("print: %s\n",msg);
}


//debug.cpp
#include <iostream>
#include "print.h"
using namespace std;
void debug_oo(const char *msg)
{
cout << "debug_oo: " << msg << endl;
print("called from debug_oo()");
}


//main.cpp
#include "debug.h"
int main(char* agrc, char** argv)
{
debug_oo("wangding");
return 0;
}
 
I

Ian Collins

HelloLinux said:
The following is how I encouted my problems:

1) Compile print.cpp into libprint.so by Sun C++ 5.9.The command is:
CC -G -KPIC -o libprint.so print.cpp
It succeeds.

2)Compile debug.cpp into libdebug.so by g++ 3.4.6, debug.cpp calls a
function defined in libprint.so. The command is:
g++ -shared -fPIC -o libdebug.so debug.cpp -lprint -L.
It succeeds too.
You can't mix libraries compiled with one C++ compiler with code
compiled with another. Well OK you can, provided the functions exported
by the library have C linkage (declared as extern "C").
 
H

HelloLinux

You can't mix libraries compiled with one C++ compiler with code
compiled with another.  Well OK you can, provided the functions exported
by the library have C linkage (declared as extern "C").

Thanks a lots, I will try to find a way out to compile all my libs
using g++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top