how to compile these code(simple share library)
h3w4 2007-01-12 02:23:19 /////////////sharelib.h
#ifndef _SHARE_LIB_
#define _SHARE_LIB_
void fun();
#endif
/////////////sharelib.cpp
#include "sharelib.h"
#include <iostream>
void fun()
{
std::cout << "int fun" << std::endl;
}
//////////////////////////////
/////////////////main.cpp
extern void (*fun)();
int main()
{
fun();
return 0;
}
//////////////////////////compile code
#g++ -c -fPIC sharelib.cpp -o libshare.0 // pass
#ld -shared -soname sharelib.1 libshare.0 -o libshare.so // pass
#ls -l
-rw-r--r-- 1 root root 2584 Jan 12 14:24 libshare.0
-rwxr-xr-x 1 root root 3794 Jan 12 14:24 libshare.so
-rw-r--r-- 1 hh users 63 Jan 12 13:55 main.cpp
-rw-r--r-- 1 root root 213 Jan 12 14:01 makefile
-rw-r--r-- 1 root root 121 Jan 12 14:05 sharelib.cpp
-rw-r--r-- 1 root root 63 Jan 12 14:05 sharelib.h
#g++ -o sharetest main.cpp -L./ -lshare // error
//////////////////////////error message:
/tmp/ccy9frxb.o: In function `main':
main.cpp:(.text+0x12): undefined reference to `fun'
/usr/lib/gcc/i586-suse-linux/4.1.0/../../../../i586-suse-linux/bin/ld: sharetest: hidden symbol `__dso_handle' in /usr/lib/gcc/i586-suse-linux/4.1.0/crtbegin.o is referenced by DSO
/usr/lib/gcc/i586-suse-linux/4.1.0/../../../../i586-suse-linux/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status