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;
}
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;
}