我有很多函数和相应的单元测试。我希望将测试嵌入到代码库本身中。我提出了以下解决方案:
void a() {
// this code should be tested
}
__attribute__((section(".tests")))
void a_test() {
// test if a() works
}
.tests
我可以为发布版本剥离它。我尝试了以下方法:
$ gcc -c a.c -o a.orig.o # a.c is shown above
$ objcopy -R.tests a.orig.o a.o
objcopy: a.o: symbol `.tests' required but not present
objcopy:a.o: no symbols
我发现
this question
这描述了一个类似的问题,回答说这是因为该部分是从某个地方交叉引用的。
我想
.eh_frame
那是有罪的部分,因为删除它会
工作:
$ objcopy -R.tests -R.eh_frame a.orig.o a.o