当前位置: 技术问答>linux和unix
静态链接与动态链接高手进
来源: 互联网 发布时间:2015-12-13
本文导语: 我用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...
我用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
#include
#include
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!
我的环境是:
/home/yrq/test/log4/test$ ls
hello.cpp liblog4cplus.so
/home/yrq/test/log4/test$ ls ..
liblog4cplus.a t2 test
我的代码是:hello.cpp(这个是log4cplus网站上的源代码)
#include
#include
#include
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!
|
用调试器调试,看代码运行到什么地方出错,这样好找一些,先要定位错误
然后再用strace这样的工具看一下系统调用
最后才去怀疑是否是编程器的BUG,换一种比如将G++换编译器试一下,成GCC,将低版本的GCC换成高版本的GCC,或者相反,或者换一台另外的LINUX发行版的机子试一下
然后再用strace这样的工具看一下系统调用
最后才去怀疑是否是编程器的BUG,换一种比如将G++换编译器试一下,成GCC,将低版本的GCC换成高版本的GCC,或者相反,或者换一台另外的LINUX发行版的机子试一下