g++链接器无法识别-Bstatic

发布于 2024-12-27 10:38:16 字数 501 浏览 3 评论 0原文

我的问题是这个问题的扩展

我想要链接 2 个库 - foobar 更喜欢静态的 foo 和动态的 bar。如果我使用

g++ -static -lfoo -lbar

它尝试查找 foo 和 bar 的静态档案。当我将命令更改

g++ -Wl,-Bstatic -lfoo -Wl,-Bdynamic -lbar -Wl,--as-needed

为上述问题时,这是我收到的错误:

ld: unknown option: -Bstatic

更新: 我正在使用 OSX,如果这有什么区别的话

My question is an extension of this question

I want to link against 2 libraries - foo and bar preferring static for foo and dynamic for bar. If I use

g++ -static -lfoo -lbar

it tries to find static archives for both foo and bar. When I change the command to

g++ -Wl,-Bstatic -lfoo -Wl,-Bdynamic -lbar -Wl,--as-needed

as per the above SO question, this is the error I get:

ld: unknown option: -Bstatic

Update:
I am using OSX, if that makes any difference

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

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

发布评论

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

评论(2

海的爱人是光 2025-01-03 10:38:16

我们在这里处理 GNU 链接器吗?你能向我们展示“ld -v”的输出吗?

编辑: 这看起来不像 GNU 的 ld,所以这就是 -Bstatic 选项不被识别的原因。而且Apple的ld似乎不太支持混合静态和动态库;请参阅:Mac OS 上的混合静态和动态链接

Are we dealing with the GNU linker here? Can you show us the output of "ld -v"?

EDIT: that doesn't look like GNU's ld, so that's why the -Bstatic option is not recognized. And it seems that Apple's ld doesn't support mixing static and dynamic libraries very well; see this: Mixed static and dynamic link on Mac OS.

执笔绘流年 2025-01-03 10:38:16

是的,不幸的是,使用 OS X 会带来不同。 -static 要求编译器为您提供完全静态链接的可执行文件(OS X 上不支持),正如 Adiel 指出的那样,不支持用于混合静态和动态链接的 -Wl,-Bstatic通过 Apple 的 clang 链接器。

要在 Mac 上解决此问题,请尝试:

g++ myapp.cpp libfoo.a libbar.a

作为编译行(其中库名称在命令行上遵循您的源代码)。这将使您的 myapp 与 foo 和 bar 库静态链接,而其他所需的库将动态链接。

Yes, unfortunately, using OS X is making the difference. -static is asking the compiler to give you a fully statically linked executable (not supported on OS X), and as Adiel pointed out, -Wl,-Bstatic for mixing static and dynamic linking isn't supported by Apple's clang linker.

To get around this problem on the Mac try:

g++ myapp.cpp libfoo.a libbar.a

as your compile line (where library names follow your source on the command line). This will give you myapp statically linked with the foo and bar libraries, while other required libraries will be linked in dynamically.

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