编辑:如果它的 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
and libclang_rt.ubsan_osx.a
(或libclang_rt.full-x86_64.a
在libclang_rt.ubsan-x86_64.a
Linux 上)。
为了提供库,我然后导出以下内容。注意:它是LIBS
,而不是LDLIBS
像大多数其他make
相关工具所期望的那样。
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'
并从结果中删除lib
前缀和.a
后缀:LIBS
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 项目以使用静态库?
奖励积分:为什么这么容易的事情变得如此困难?