免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 882 | 回复: 2
打印 上一主题 下一主题

静态链接与动态链接高手进 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-06 15:16 |只看该作者 |倒序浏览
我用log4cplus的库,有静态库(liblog4cplus.a)也有动态库(liblog4cplus.so)
我的环境是:
/home/yrq/test/log4/test$ ls
hello.cpp  liblog4cplus.so
/home/yrq/test/log4/test$ ls ..
liblog4cplus.a t2  test

我的代码是:hello.cpp(这个是log4cplus网站上的源代码)

#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <iomanip>

using namespace log4cplus;

int main( int argc, char** argv)
{
    BasicConfigurator config;
    config.configure();

    Logger logger = Logger::getInstance("main");
    LOG4CPLUS_WARN(logger, "Hello, World!");
    return 0;
}


当我用静态库编译的时候不报错:
/home/yrq/test/log4/test$ g++ -g -c hello.cpp
/home/yrq/test/log4/test$ g++ -g  -o hello hello.o -L.. -llog4cplus

当运行的时候出现段错误。加上-Bstatic结果也是一样。
/home/yrq/test/log4/test$ hello
段错误
/home/yrq/test/log4/test$ gdb hello core.3505
GNU gdb Red Hat Linux (6.1post-1.20040607.62rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

Core was generated by `hello'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /usr/lib/libstdc++.so.6...done.
Loaded symbols for /usr/lib/libstdc++.so.6
Reading symbols from /lib/tls/libm.so.6...done.
Loaded symbols for /lib/tls/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /lib/tls/libc.so.6...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
#0  0x00000000 in ?? ()
(gdb) where
#0  0x00000000 in ?? ()
#1  0x08074b84 in log4cplus::thread::createPthreadKey () at threads.cxx:115
#2  0x08061608 in NDC (this=0x808f830) at ndc.cxx:89
#3  0x08061665 in log4cplus::getNDC () at ndc.cxx:56
#4  0x0805c1af in log4cplus::initializeLog4cplus () at global-init.cxx:52
#5  0x0807f015 in __do_global_ctors_aux ()
#6  0x0804a3d9 in _init ()
#7  0x0807ef7e in __libc_csu_init ()
#8  0x0070ede3 in __libc_start_main () from /lib/tls/libc.so.6
#9  0x0804ab7d in _start ()
(gdb) q


当用动态库编译也没错,运行也没错。
/home/yrq/test/log4/test$ hello
WARN - Hello, World!

论坛徽章:
0
2 [报告]
发表于 2006-08-08 21:53 |只看该作者
晕啊,没有一个人回答

论坛徽章:
0
3 [报告]
发表于 2006-08-08 22:04 |只看该作者
原帖由 mukey 于 2006-8-6 15:16 发表
我用log4cplus的库,有静态库(liblog4cplus.a)也有动态库(liblog4cplus.so)
我的环境是:
/home/yrq/test/log4/test$ ls
hello.cpp  liblog4cplus.so
/home/yrq/test/log4/test$ ls ..
liblog4cplus.a ...

>>
>>
>>没用过log4cplus,不知道你那几个函数怎么用的,
>>不过建议编译的时候加上类似-Wall的选项看看
>>
>>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP