我已阅读有关使用 Boost 创建具有向后兼容 ABI 的库的主题,现在我正在尝试了解如何链接我的共享库以保持稳定的 ABI,并避免出现干扰符号的问题。
我创建了以下简单的测试项目:
cat <<EOF > a.c
#define ABI __attribute__((visibility("default")))
int common();
int ABI a() { return common() + 1; }
EOF
cat <<EOF > b.c
#define ABI __attribute__((visibility("default")))
int common();
int ABI b() { return common() + 2; }
EOF
cat <<EOF > common_v1.c
int common() { return 1; }
EOF
cat <<EOF > common_v2.c
int common() { return 2; }
EOF
cat <<EOF > test.c
#include <assert.h>
int a();
int b();
int main(int argc, const char *argv[])
{
assert( a() + b() == 6 );
return 0;
}
EOF
cat <<EOF > CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(TEST)
add_library(common_v1 STATIC common_v1.c)
add_library(common_v2 STATIC common_v2.c)
SET_SOURCE_FILES_PROPERTIES( a.c b.c COMPILE_FLAGS -fvisibility=hidden )
add_library(a SHARED a.c)
target_link_libraries(a common_v1)
add_library(b SHARED b.c)
target_link_libraries(b common_v2)
add_executable(test test.c)
target_link_libraries(test a b)
EOF
库 common_v1 和 common_v2 应该模拟库 a 和 b 的外部依赖(如 Boost)。因为 common_v1 和 common_v2 被视为外部库,所以我不希望更改它们的构建系统(并且不更改它们编译时使用的标志)。
上面的项目,编译正常,但它不起作用!执行测试应用程序时,它会跳转到断言语句。
这让我相信在 liba 和 libb 中使用了相同的 common 定义。为什么会这样,我做错了什么?