【发布时间】:2013-11-22 18:52:27
【问题描述】:
编辑:如果是 TLDR,就跳到底部。我要问的是:如何配置 autotools 项目以使用静态库?
我正在使用几个开源库,并尝试在 Clang 的消毒剂下运行他们的测试套件。要在 Clang sanitizers 下运行,我们需要 (1) 指定一些选项,以及 (2) 根据需要从 Clang 的 Compiler-RT 链接静态库。 注意:没有动态库或共享对象。
设置选项很简单:
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/lib/clang/3.3/lib/darwin/
export CC=/usr/local/bin/clang
export CXX=/usr/local/bin/clang++
export CFLAGS="-g3 -fsanitize=address -fsanitize=undefined"
export CXXFLAGS="-g3 -fsanitize=address -fsanitize=undefined -fno-sanitize=vptr"
./configure
但是,这会产生一些带有未定义符号的存档警告(当运行AR 时)和链接错误(当运行LD 时)。该消息将类似于:
libjpeg.a(jmemmgr.o): In function `do_sarray_io':
/home/jwalton/jpeg-6b/jmemmgr.c:695: undefined reference to
`__ubsan_handle_type_mismatch'
/home/jwalton/jpeg-6b/jmemmgr.c:695: undefined reference to
`__ubsan_handle_type_mismatch'
/home/jwalton/jpeg-6b/jmemmgr.c:696: undefined reference to
`__ubsan_handle_type_mismatch'
我知道需要链接的库。对于我使用的消毒剂,它们是 libclang_rt.asan_osx.a 和 libclang_rt.ubsan_osx.a(或在 Linux 上为 libclang_rt.full-x86_64.a 和 libclang_rt.ubsan-x86_64.a)。
为了提供库,然后我导出以下内容。 注意:它是LIBS,而不是大多数其他make 相关工具所期望的LDLIBS。
export LIBS="/usr/local/lib/clang/3.3/lib/darwin/libclang_rt.asan_osx.a \
/usr/local/lib/clang/3.3/lib/darwin/libclang_rt.ubsan_osx.a"
这会导致configure 问题:
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
...
查看config.log,似乎出现了两个问题。首先,通过将/usr/local/... 更改为/Users/jwalton/...,路径正在被扼杀。其次,文件名被从静态库更改为动态库:
configure:3346: ./conftest
dyld: Library not loaded: /Users/jwalton/clang-llvm/llvm-3.3.src/Release+Asserts/lib/clang/3.3/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
Referenced from: /Users/jwalton/libpng-1.6.7/./conftest
Reason: image not found
在另一次尝试中,我尝试使用LDFLAGS:
export LDFLAGS="-L/usr/local/lib/clang/3.3/lib/darwin/"
export LIBS="libclang_rt.asan_osx.a libclang_rt.ubsan_osx.a"
这会导致类似的错误:
configure: error: in `/Users/jwalton/libpng-1.6.7':
configure: error: C compiler cannot create executables
还有config.log:
configure:3209: /usr/local/bin/clang -g3 -fsanitize=address -fsanitize=undefined -L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c libclang_rt.asan_osx.a libclang_rt.ubsan_osx.a >&5
clang: error: no such file or directory: 'libclang_rt.asan_osx.a'
clang: error: no such file or directory: 'libclang_rt.ubsan_osx.a'
从LIBS 中删除lib 前缀和.a 后缀会导致:
configure:3209: /usr/local/bin/clang -g3 -fsanitize=address -fsanitize=undefined -L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c clang_rt.asan_osx clang_rt.ubsan_osx >&5
clang: error: no such file or directory: 'clang_rt.asan_osx'
clang: error: no such file or directory: 'clang_rt.ubsan_osx'
将-l 添加到LIBS 会导致:
configure:3335: /usr/local/bin/clang -o conftest -g3 -fsanitize=address -fsanitize=undefined
-L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c -lclang_rt.asan_osx -lclang_rt.ubsan_osx >&5
configure:3339: $? = 0
configure:3346: ./conftest
dyld: could not load inserted library: /Users/jwalton/libpng-1.6.7/./conftest
./configure: line 3348: 38224 Trace/BPT trap: 5 ./conftest$ac_cv_exeext
最后,-L 参数是有效的:
$ ls /usr/local/lib/clang/3.3/lib/darwin/
libclang_rt.10.4.a libclang_rt.ios.a
libclang_rt.asan_osx.a libclang_rt.osx.a
libclang_rt.asan_osx_dynamic.dylib libclang_rt.profile_ios.a
libclang_rt.cc_kext.a libclang_rt.profile_osx.a
libclang_rt.cc_kext_ios5.a libclang_rt.ubsan_osx.a
libclang_rt.eprintf.a
了解所有背景:如何配置 autotools 项目以使用静态库?
奖励积分:为什么这么简单的事情变得这么困难?
【问题讨论】:
标签: static-libraries clang configure autotools sanitizer