C++ Apple LLVM 4.1 的 STD 链接器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12920891/
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-08-27 16:45:22  来源:igfitidea点击:

STD linker error with Apple LLVM 4.1

c++xcodemacoslinker-errorsstd

提问by antho

I've got a large static library in C++ with bits of Objective-C originally built for iOS (armv7).

我在 C++ 中有一个大型静态库,其中包含一些最初为 iOS (armv7) 构建的 Objective-C。

I built a OS X (64-bit Intel x86_64) version of it, but as soon as I tried to use it in a OS X app project (targeted to Lion 10.7), tens of linker errors appeared, most of them about standard library symbols.

我构建了它的 OS X(64 位 Intel x86_64)版本,但是当我尝试在 OS X 应用程序项目(针对 Lion 10.7)中使用它时,出现了数十个链接器错误,其中大部分与标准库有关符号。

I know how to solve "my" linker problems, but the STD ones copied below are bugging me.

我知道如何解决“我的”链接器问题,但下面复制的 STD 问题困扰着我。

"std::basic_filebuf<char, std::char_traits<char> >::is_open() const"
"std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const"
"std::basic_ios<char, std::char_traits<char> >::widen(char) const"
"std::istream& std::istream::_M_extract<double>(double&)"
"std::ostream::put(char)"
"std::ostream::flush()"
"std::ostream& std::ostream::_M_insert<void const*>(void const*)"
"std::ostream& std::ostream::_M_insert<bool>(bool)"
"std::ostream& std::ostream::_M_insert<double>(double)"
"std::ostream& std::ostream::_M_insert<unsigned long>(unsigned long)"
"std::ostream::operator<<(int)"
"std::ostream::operator<<(short)"
"std::string::_Rep::_M_destroy(std::allocator<char> const&)"
"std::string::_Rep::_S_terminal"
"std::string::_Rep::_S_empty_rep_storage"
"std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&)"
"std::string::append(char const*, unsigned long)"
"std::string::append(std::string const&)"
"std::string::assign(std::string const&)"
"std::string::reserve(unsigned long)"
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)"
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&)"
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()"
"std::basic_ofstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)"
"std::basic_ofstream<char, std::char_traits<char> >::close()"
"std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream()"
"std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream()"
"std::_List_node_base::hook(std::_List_node_base*)"
"std::_List_node_base::unhook()"
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::string const&, std::_Ios_Openmode)"
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)"
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream()"
"std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::basic_ostringstream(std::_Ios_Openmode)"
"std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_ostringstream()"
"std::ios_base::Init::Init()"
"std::ios_base::Init::~Init()"
"std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)"
"std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)"
"std::_Rb_tree_decrement(std::_Rb_tree_node_base*)"
"std::_Rb_tree_increment(std::_Rb_tree_node_base const*)"
"std::_Rb_tree_increment(std::_Rb_tree_node_base*)"
"std::__throw_logic_error(char const*)"
"std::__throw_length_error(char const*)"
"std::__throw_out_of_range(char const*)"
"std::_Rb_tree_rebalance_for_erase(std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)"
"std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)"
"std::cerr"
"std::cout"

I checked my build settings, my project links to the standard library (-stdlib=libc++) and I can use std::cout without any problem in my main.cpp.

我检查了我的构建设置,我的项目链接到标准库 ( -stdlib=libc++) 并且我可以在 main.cpp 中使用 std::cout 没有任何问题。

I changed the compiler in the build settings from Apple LLVM 4.1 to LLVM GCC 4.2 and the problem disappeared. I want to keep using Apple LLVM 4.1. How can I fix that?

我将构建设置中的编译器从 Apple LLVM 4.1 更改为 LLVM GCC 4.2,问题就消失了。我想继续使用 Apple LLVM 4.1。我该如何解决?

Thanks!

谢谢!

回答by gzfrancisco

In iOS 7 I use a library for charts and have the same issue. In this case lib stdc++ does not resolve the issue.

在 iOS 7 中,我使用了一个图表库并且有同样的问题。在这种情况下,lib stdc++ 不能解决问题。

I add the stdc++.6.dylib to my build phase and the symbols are found.

我将 stdc++.6.dylib 添加到我的构建阶段并找到了符号。

回答by Petesh

Change the standard library that is linked to use libstdc++instead of libc++- the problem is that the other library was compiled using the g++mode which uses the libstdc++library.

将链接的标准库更改为 uselibstdc++而不是libc++- 问题是其他库是使用g++使用该libstdc++库的模式编译的。

Consider the following sample code:

考虑以下示例代码:

dhcp-191:~/Development/testy/fred% cat fred.cpp
#include <iostream>
#include <string>
#include "fred.h"

using namespace std;

bool dofred(string &x)
{
    cout << x << endl;
    return true;
}
dhcp-191:~/Development/testy/fred% cat fred.h

#include <iostream>
#include <string>

bool dofred(std::string &x);

dhcp-191:~/Development/testy/fred% clang++ -stdlib=libc++ -shared -o fred.dylib fred.cpp
dhcp-191:~/Development/testy/fred% nm fred.dylib | c++filt | grep dofred
0000000000000fa0 T dofred(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
dhcp-191:~/Development/testy/fred% clang++ -stdlib=libstdc++ -shared -o fred.dylib fred.cpp
dhcp-191:~/Development/testy/fred% nm fred.dylib | c++filt | grep dofred                     
0000000000000e30 T dofred(std::string&)

You get two completely different exported symbols. When trying to use the symbol, the app that uses the same -stdlibflag will be able to link, while the app that doesn't will display a link error.

您将获得两个完全不同的导出符号。尝试使用该符号时,使用相同-stdlib标志的应用程序将能够链接,而不使用该标志的应用程序将显示链接错误。

回答by jlukanta

I had this problem after putting all C++ files into a separate library. I did set the settings of all projects to use libc++, but the linker does not link with libc++. If I add a C++ file to the main project, the problem would disappear. To fix this, you can add '-lc++' on the "Other Linker Flags" section of the main project. This would force XCode to link to libc++.

将所有 C++ 文件放入单独的库后,我遇到了这个问题。我确实将所有项目的设置设置为使用 libc++,但链接器不与 libc++ 链接。如果我在主项目中添加一个 C++ 文件,问题就会消失。要解决此问题,您可以在主项目的“其他链接器标志”部分添加“-lc++”。这将强制 XCode 链接到 libc++。

EDIT: As the other poster said, XCode may be behaving correctly. I had expected it to know to add C++ linkage because the C++ lib source code is on the same workspace.

编辑:正如另一位海报所说,XCode 可能表现正确。我原以为它知道添加 C++ 链接,因为 C++ lib 源代码位于同一个工作区。

回答by djacobs7

I just had a similar problem, and I had to go to "Build Settings" and then "Apple LLVM 5.1 - Language - C++" and then change the "C++ Standard Library" to libstdc++.

我刚刚遇到了类似的问题,我必须转到“构建设置”,然后转到“Apple LLVM 5.1 - 语言 - C++”,然后将“C++ 标准库”更改为 libstdc++。

回答by Lee Irvine

you may also try adding an empty .cpp file to your project. This will trick xcode into loading C++ std libraries

您也可以尝试将一个空的 .cpp 文件添加到您的项目中。这将欺骗 xcode 加载 C++ std 库

回答by user2073196

In response to jlukanta: I had the same problem. I have been careful to choose the right STD but I still got those errors. But that's not a bug, it actually makes sense: Why should Xcode link with the c++ stdlib if you don't have any C++ code in your project?

回应 jlukanta:我遇到了同样的问题。我一直很小心地选择了正确的 STD,但我仍然遇到了这些错误。但这不是错误,它实际上是有道理的:如果项目中没有任何 C++ 代码,为什么 Xcode 应该与 c++ stdlib 链接?

Of course, this is a problem when you don't have C++ code in your project but still C++ libraries.

当然,当您的项目中没有 C++ 代码但仍有 C++ 库时,这是一个问题。