如何提供多个链接功能的选项

发布于 2025-02-05 04:41:30 字数 162 浏览 2 评论 0原文

我有3个对象文件

main.o

general.o

specific.o

。 enstry.o对handle(),实现通用功能, extile。 我想在我的cmake中指定“搜索要链接到特定的链接

I have 3 object files :

main.o

general.o

specific.o

in main.o there is a call to function : void handle(void),
general.o implement a generic functionality to handle() ,
specific.o might or might not have an implementation to handle() ,
I want to specify in my cmake that "search to link handle with specific.o if fail then try with general.o"

Is that possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私野 2025-02-12 04:41:30

标准C ++肯定没有什么可以允许的。

对于GCC/CLANG工具链,您可以将定义作为ender>常规带有__属性__(((弱))的弱符号, in <代码>特定。

但是请非常小心,请确保handle本身不会在常规中调用。因为如果是这样,则可以在链接器看到任何内容之前将呼叫夹住,从而导致某些代码调用常规方法,其余特定于特定于此。链接时间优化实际上可能更安全。

There certainly is nothing in standard C++ which allows this.

For Gcc/clang toolchain you can define handle as a weak symbol in general TU with __attribute__((weak)) and as ordinary symbol in specific.

Be very careful with this though, ensure that handle itself is not called in general. Because if it is, the call might be inlined before the linker sees anything, resulting in some code calling the general method, the rest specific. Link-time optimization might be actually more safe here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文