xcode 为什么我在这个简单的代码中得到“ld:警告:在 _main 中直接访问全局弱符号”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36567072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:43:09  来源:igfitidea点击:

Why do I get "ld: warning: direct access in _main to global weak symbol" in this simple code?

c++xcodewarningsld

提问by JavaRunner

It's very strange behavior in my Clang compiler. I use Xcode (OS X), all is up-to-date. Why am I getting this warning in that simple code? If I remove those two lines the warning hides.

在我的 Clang 编译器中这是非常奇怪的行为。我使用 Xcode (OS X),一切都是最新的。为什么我在那个简单的代码中收到这个警告?如果我删除这两行,警告就会隐藏。

ld: warning: direct access in _main to global weak symbol std::__1::char_traits::eq(char, char) means the weak symbol cannot be overridden at runtime. This was likely caused by different translation units being compiled with different visibility settings.

ld:警告:在 _main 中直接访问全局弱符号 std::__1::char_traits::eq(char, char) 意味着在运行时不能覆盖弱符号。这可能是由于使用不同的可见性设置编译不同的翻译单元造成的。

int main( int argc, char* argv[] ) {
    std::string file = "test";
    size_t pos = file.find( "a" );
    return 0;
}

回答by sergej

See Controlling Symbol Visibility @ developer.apple.comfor details.

有关详细信息,请参阅控制符号可见性 @ developer.apple.com

It looks like your libs (eg. the C++ standard library) and your code have been compiled with different visibility settings, at least, that is what the linker error message is saying.

看起来您的库(例如 C++ 标准库)和您的代码已使用不同的可见性设置编译,至少,链接器错误消息是这样说的。

To fix the warning, you should use the same visibility settings when compiling your code, eg -fvisibility=hidden.

要修复警告,您应该在编译代码时使用相同的可见性设置,例如-fvisibility=hidden.