Linux下Shared Object编译问题

leonchenjian 2008-04-16 09:31:28
小弟在编译一个Linux下的so文件的时候碰到如下问题:
编译 A.so时, 引用了 libB.a 这个静态库, 然后在使用A.so时发现还要有B.so(静态库libB.a对应的so)这个文件才行

有没有办法让编译出来的A.so不需要B.so也能正常使用
...全文
184 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinshuow 2008-04-17
  • 打赏
  • 举报
回复
[xinshuow@localhost shared2]$ ll
总用量 72
-rw-r--r-- 1 xinshuow davinci 101 4月 17 11:37 bar.c
-rw-r--r-- 1 xinshuow davinci 984 4月 17 12:06 bar.o
-rwxr-xr-x 1 xinshuow davinci 415 4月 17 11:38 build.sh
-rwxr-xr-x 1 xinshuow davinci 5117 4月 17 12:06 foobar
-rwxr-xr-x 1 xinshuow davinci 5125 4月 17 12:06 foobarstatic
-rw-r--r-- 1 xinshuow davinci 119 4月 17 11:37 foo.c
-rw-r--r-- 1 xinshuow davinci 1020 4月 17 12:06 foo.o
-rw-r--r-- 1 xinshuow davinci 1124 4月 17 12:06 libbar.a // 大小为1124
-rwxr-xr-x 1 xinshuow davinci 4447 4月 17 12:06 libbar.so // 大小为4447,怎么比静态库libbar.a大这么多啊?
-rwxr-xr-x 1 xinshuow davinci 4559 4月 17 12:06 libfoo.so
-rwxr-xr-x 1 xinshuow davinci 4701 4月 17 12:06 libfoostatic.so
-rw-r--r-- 1 xinshuow davinci 149 4月 17 11:37 main.c
-rw-r--r-- 1 xinshuow davinci 984 4月 17 12:06 main.o
[xinshuow@localhost shared2]$
xinshuow 2008-04-17
  • 打赏
  • 举报
回复

mymtom的这个小例子很全面..学习了..
leonchenjian 2008-04-17
  • 打赏
  • 举报
回复
问题解决了, 非常感谢 mymtom
leonchenjian 2008-04-16
  • 打赏
  • 举报
回复
我贴一部分Makefile吧
${DEST} : ${OBJECTS}
$(CC) -o ${DEST} ${OBJECTS} ${LIBS} -shared -pthread

${OBJECTS} : ${SOURCES}
${CC} -c ${INCLUDES} ${DEFS} ${SOURCES} -pthread

其中
${DEST}就是A.so, ${LIBS}就是libB.a,
A.so调用B库中的函数是直接调用, 没有通过dlopen

使用A.so如果不附带B.so的话就提示缺少B.so
xinshuow 2008-04-16
  • 打赏
  • 举报
回复
这个完全可以实现的.
"然后在使用A.so时发现还要有B.so(静态库libB.a对应的so)这个文件才行"
你程序里面是怎么调用库函数的,是直接引用还是dlopen(...,)最好能把那几句贴出来,
还有Makefile贴出来看看..
把错误信息贴出来也看看 .
hzcpig 2008-04-16
  • 打赏
  • 举报
回复
应该是可以的吧,把Makefile贴出来看看
mymtom 2008-04-16
  • 打赏
  • 举报
回复

/*
* main.c
*/

#include <stdio.h>

void foo(void);

int
main(void)
{
(void)printf("%s: %s\n", __FILE__, __FUNCTION__);
foo();

return (0);
}



/*
* foo.c
*/

#include <stdio.h>

void bar(void);

void
foo(void)
{
(void)printf("%s: %s\n", __FILE__, __FUNCTION__);
bar();
}



/*
* bar.c
*/

#include <stdio.h>

void
bar(void)
{
(void)printf("%s: %s\n", __FILE__, __FUNCTION__);
}



#! /bin/sh -x
#
# build.sh
#
cc -c -fPIC -DPIC bar.c
cc -shared -o libbar.so bar.o
ar cr libbar.a bar.o
ranlib libbar.a

cc -c -fPIC -DPIC foo.c
cc -shared -o libfoo.so foo.o -Wl,-rpath=./ -L./ -lbar
cc -shared -o libfoostatic.so foo.o -Wl,-rpath=./ libbar.a

cc -c main.c
cc -o foobar -Wl,-rpath=./ main.o -L./ -lfoo
cc -o foobarstatic -Wl,-rpath=./ main.o -L./ -lfoostatic

ldd foobar foobarstatic
uname -msvrp

[code=BatchFile]
$ ./build.sh
+ cc -c -fPIC -DPIC bar.c
+ cc -shared -o libbar.so bar.o
+ ar cr libbar.a bar.o
+ ranlib libbar.a
+ cc -c -fPIC -DPIC foo.c
+ cc -shared -o libfoo.so foo.o -Wl,-rpath=./ -L./ -lbar
+ cc -shared -o libfoostatic.so foo.o -Wl,-rpath=./ libbar.a
+ cc -c main.c
+ cc -o foobar -Wl,-rpath=./ main.o -L./ -lfoo
+ cc -o foobarstatic -Wl,-rpath=./ main.o -L./ -lfoostatic
+ ldd foobar foobarstatic
foobar:
libfoo.so => ./libfoo.so (0x00002ab43bade000)
libc.so.6 => /lib64/libc.so.6 (0x00000039aec00000)
libbar.so => ./libbar.so (0x00002ab43bcfa000)
/lib64/ld-linux-x86-64.so.2 (0x00000039ae800000)
foobarstatic:
libfoostatic.so => ./libfoostatic.so (0x00002ac2c5b9b000)
libc.so.6 => /lib64/libc.so.6 (0x00000039aec00000)
/lib64/ld-linux-x86-64.so.2 (0x00000039ae800000)
+ uname -msvrp
Linux 2.6.20 #1 SMP Thu Feb 14 18:08:17 CST 2008 x86_64 x86_64
[/code]
leonchenjian 2008-04-16
  • 打赏
  • 举报
回复
补充一下, 是C++写的程序用的编译器是 g++
leonchenjian 2008-04-16
  • 打赏
  • 举报
回复
用ldd A.so会看到对B.so的依赖
用g++编译so文件时使用静态连接需要加什么参数呢?
xinshuow 2008-04-16
  • 打赏
  • 举报
回复
这里涉及到程序的编译,连接,装载运行..
你的应该是哪个环节出现错误..
我不知道你的A.so编译连接成功了没有..
如果成功了, $ldd A.so看看它依赖哪些库.
"有没有办法让编译出来的A.so不需要B.so也能正常使用" 通过静态连接可以的

23,216

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧