Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

在linux下静态库的问题 #10

Closed
qengli opened this issue Oct 31, 2012 · 6 comments
Closed

在linux下静态库的问题 #10

qengli opened this issue Oct 31, 2012 · 6 comments

Comments

@qengli
Copy link

qengli commented Oct 31, 2012

cc -o zlog-chk-conf -O2 -fPIC -Wall -Werror -Wstrict-prototypes -g -ggdb -fPIC -lpthread zlog-chk-conf.o libzlog.a 时出错,
出现类似于zlog.c:52: undefined reference to `pthread_key_delete' 之类的错误信息, 用-pthread可以解决

但如果写一个最简单的程序链接静态库,用valgrind跑一下会产生几千个内存错误:

但链接静态库却没有这个问题,请释疑
==15205== Use of uninitialised value of size 8
==15205== at 0x464701: __strftime_internal (in ...)
==15205== by 0x463D3B: __strftime_internal (in ..)
==15205== by 0x465695: strftime_l (in ..)
==15205== by 0x421F21: zlog_spec_write_time_D (spec.c:128)
==15205== by 0x41D862: zlog_format_gen_msg (format.c:164)
==15205== by 0x41FEDF: zlog_rule_output_stderr (rule.c:421)
==15205== by 0x41B7FD: zlog_category_output (category.c:240)
==15205== by 0x41A84F: zlog (zlog.c:904)
==15205== by 0x402938: main (main.c:15)
==15205==
==15205== Use of uninitialised value of size 8
==15205== at 0x4646DD: __strftime_internal (in ..)
==15205== by 0x463D3B: __strftime_internal (in ..)
==15205== by 0x465695: strftime_l (in ..)
==15205== by 0x421F21: zlog_spec_write_time_D (spec.c:128)
==15205== by 0x41D862: zlog_format_gen_msg (format.c:164)
==15205== by 0x41FEDF: zlog_rule_output_stderr (rule.c:421)
==15205== by 0x41B7FD: zlog_category_output (category.c:240)
==15205== by 0x41A84F: zlog (zlog.c:904)
==15205== by 0x402938: main (main.c:15)
==15205==
==15205== Use of uninitialised value of size 8
==15205== at 0x425046: zlog_buf_append (buf.c:532)
==15205== by 0x41D862: zlog_format_gen_msg (format.c:164)

@HardySimpson
Copy link
Owner

第一个问题是makefile的写法有误,库要放在.o后面,已经修改,1.2.5就应该都对了。

zlog-chk-conf: zlog-chk-conf.o $(STLIBNAME)
$(CC) -o $@ $(REAL_CFLAGS) zlog-chk-conf.o -L. -lzlog $(REAL_LDFLAGS)

@HardySimpson
Copy link
Owner

第二个问题目前还不知道为什么……有人知道吼一声!

@HardySimpson HardySimpson reopened this Nov 15, 2012
@HardySimpson
Copy link
Owner

使用目前的版本,第二个问题貌似解决了?

$ gcc -c -o test_hello.o -I../src test_hello.c
$ gcc -o test_hello test_hello.o -L../src -lzlog -lpthread
$ ./test_hello
2012-11-22 19:53:22 INFO [5014:test_hello.c:41] hello, zlog
2012-11-22 19:53:22.129 hello, zlog
2012-11-22 19:53:22.129059 hello, zlog
$ valgrind ./test_hello
==5055== Memcheck, a memory error detector
==5055== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==5055== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==5055== Command: ./test_hello
==5055==
2012-11-22 19:53:31 INFO [5055:test_hello.c:41] hello, zlog
2012-11-22 19:53:31.134 hello, zlog
2012-11-22 19:53:31.134990 hello, zlog
==5055==
==5055== HEAP SUMMARY:
==5055== in use at exit: 0 bytes in 0 blocks
==5055== total heap usage: 87 allocs, 87 frees, 334,428 bytes allocated
==5055==
==5055== All heap blocks were freed -- no leaks are possible
==5055==
==5055== For counts of detected and suppressed errors, rerun with: -v
==5055== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

@qengli
Copy link
Author

qengli commented Nov 23, 2012

第二个问题还是存在,你的编绎方法还是使用的动态库,用静态库就可以重现了,
我测的是最新版本1.2.6 你用下面的命令编一下试试

$gcc test_hello.c -static -I../src -L ../src/ -lzlog -pthread

$valgrind a.out

.............
==9230== by 0x404DEA: zlog (zlog.c:852) [13/1841]
==9230== by 0x40291D: main (in /home/li/workspace/mv.git/tmp/build/zlog-1.2.6/test/a.out)
==9230==
==9230== Use of uninitialised value of size 8
==9230== at 0x407F05: zlog_format_gen_msg (format.c:165)
==9230== by 0x40A5FF: zlog_rule_output_stdout (rule.c:403)
==9230== by 0x405E3D: zlog_category_output (category.c:240)
==9230== by 0x404DEA: zlog (zlog.c:852)
==9230== by 0x40291D: main (in /home/li/workspace/mv.git/tmp/build/zlog-1.2.6/test/a.out)
==9230==
==9230==
==9230== More than 1000 different errors detected. I'm not reporting any more.
==9230== Final error counts will be inaccurate. Go fix your program!
==9230== Rerun with --error-limit=no to disable this cutoff. Note
==9230== that errors may occur in your program without prior warning from
==9230== Valgrind, because errors are no longer being displayed.
==9230==
2012-11-23 09:08:45.924 hello, zlog
2012-11-23 09:08:45.924051 hello, zlog
==9230==
==9230== HEAP SUMMARY:
==9230== in use at exit: 0 bytes in 0 blocks
==9230== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==9230==
==9230== All heap blocks were freed -- no leaks are possible
==9230==
==9230== For counts of detected and suppressed errors, rerun with: -v
==9230== Use --track-origins=yes to see where uninitialised values come from
==9230== ERROR SUMMARY: 7618 errors from 1000 contexts (suppressed: 0 from 0)

@HardySimpson
Copy link
Owner

http://pl.digipedia.org/usenet/thread/11632/12006/

看起来是valgrind的问题,在静态链接的时候会报一堆pthread和malloc calloc的错,可能valgrind升级到新版会好

@HardySimpson
Copy link
Owner

可以写一个有pthread的静态链接的小程序,然后用valgrind'跑,也能观察那些报错

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants