解决符号碰撞的cmake和部分链接

时间:2016-03-04 14:16:55

标签: c++ gcc cmake visibility static-linking

我有两个名称冲突来源(autogenerated1.cpp和autogenerated2.cpp)。

我不想修改它们,因为它们是由第三方工具生成的,所以我制作了包装来隐藏实现。

我想在CMAKE上做这样的事情:

g++ -fvisibility=hidden -c autogenerated1.cpp

g++ -fvisibility=hidden -c autogenerated2.cpp

g++ -c wrapper1.cpp

g++ -c wrapper2.cpp

ld -Ur autogenerated1.o wrapper1.o -o partial1.o

ld -Ur autogenerated2.o wrapper2.o -o partial2.o

objcopy --localize-hidden partial1.o

objcopy --localize-hidden partial2.o

我怎么能用CMAKE实现类似的东西?

TARGET_OBJECT 生成器无法处理自定义命令或自定义目标。

我无法通过应用" objcopy --localize-hidden"来生成一个有效的静态库。到没有可重定位中间对象的目标输出共享库。

我打开其他解决方案来修复符号冲突。

1 个答案:

答案 0 :(得分:1)

Nice to see that it works for your case. Now as an answer instead of a reply:

Maybe just an idea, but instead of compiling the generated files, could you instead wrap them in a namespace? e.g:

namespace autogen1 {
#include "autogenerated1.cpp"
}

You'd obviously need to exclude the autogenerated files from compiling (as those are now pulled in through the include), but you now have access to all members of both autogenerated files, simply by prefixing the calls with the appropriate namespaces.

It looks a bit hackish (but not as bad as your own solution ;-) )

Including one C source file in another?