首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

how to compile these code(simple share library)解决办法

2012-03-17 
how to compile these code(simple share library)/////////////sharelib.h#ifndef_SHARE_LIB_#define_SHA

how to compile these code(simple share library)
/////////////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




[解决办法]
//extern void (*fun)();这是声明一个函数指针
extern void fun();//修改为这样
int main()
{
fun();
return 0;
}
[解决办法]
$g++ -fPIC -shared -o libshare.so sharelib.cpp
$g++ main.cpp -L. -lshare

热点排行