使用-export-symbols
或-export-symbols-regex
限制库的公共接口时,如何对未导出的符号进行单元测试?
例如,这是一个带有导出和未导出功能的小库,以及一个测试程序:
/* hello.h */
int hello__unexported(int a, int b);
int hello_exported(int a, int b);
/* hello.c */
int hello__unexported(int a, int b) {
return a * b;
}
int hello_exported(int a, int b) {
return a + b + hello__unexported(a, b);
}
/* tests/hello_test.c */
#include <assert.h>
#include "hello.h"
int main(int argc, char **argv) {
assert(6 == hello__unexported(2, 3));
assert(11 == hello_exported(2, 3));
return 0;
}
这是用于构建库和
的自动制作文件# Makefile.am
lib_LTLIBRARIES = libhello.la
libhello_la_SOURCES = hello.c
libhello_la_LDFLAGS = -export-symbols-regex '^hello_[^_]'
TESTS = $(check_PROGRAMS)
check_PROGRAMS = tests/hello_test
tests_hello_test_SOURCES = tests/hello_test.c
tests_hello_test_LDADD = libhello.la
但是,未导出的符号不能与测试程序链接。这是make check
:
libtool: link: gcc -g -O2 -o tests/.libs/hello_test tests/hello_test-hello_test.o ./.libs/libhello.dylib
Undefined symbols for architecture x86_64:
"_hello__unexported", referenced from:
_main in hello_test-hello_test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [tests/hello_test] Error 1
make: *** [check-am] Error 2
除了添加与第一个相同的第二个lib_LTLIBRARIES
目标,但从-export-symbols-regex
中删除_la_LDFLAGS
之外,还有没有办法只限制分发期间的导出符号,而不进行测试?