本文介绍了clang ++ macos x c ++ 11链接器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用xcode 4.6.2中的clang ++在mac os x 10.8.3下使用 -std = c ++ 11 -stdlib = libc ++编译程序时遇到问题。

I have a problem compiling a program with "-std=c++11 -stdlib=libc++" under mac os x 10.8.3 using clang++ from xcode 4.6.2.

当我尝试使用std :: mem_fn()或(不推荐使用)std :: mem_fun_ref()时,出现链接器错误找不到符号。相同的代码(使用std :: mem_fun_ref代替std :: mem_fn)在c ++ 03标准下编译和链接没有任何问题。

When I try to use std::mem_fn() or (deprecated) std::mem_fun_ref(), I get linker error "symbol(s) not found". The same code (with std::mem_fun_ref instead of std::mem_fn) compiles and links without any issues under the c++03 standard.

如果我调用相同的代码无需通过mem_fn或mem_fun_ref引用该对象上的成员函数,该程序即可编译并运行,不会出现任何问题。是clang ++问题,mac OS问题还是我做错了事?

If I call the same member function on an object without referring to it via mem_fn or mem_fun_ref, the program compiles and runs without any problems. Is it a clang++ problem, a mac os problem, or am I doing something wrong?

代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char **arvc)
{

  /* The beginning of the function compiles without any errors */

  string str = "It's a test)";
  if (str.empty())
  {
    cout << "String is empty!!!" << endl;
  }
  vector<string> v1;
  v1.push_back("str1");
  v1.push_back("str2");
  v1.push_back("");
  v1.push_back("str4");
  v1.push_back("");
  v1.push_back("str6");

  /* The code after this point leads to linker error  */

  vector<string>::iterator it = remove_if(v1.begin(), v1.end(), mem_fn(&string::empty));
  v1.erase(it, v1.end());
  for (it = v1.begin(); it < v1.end(); ++it)
  {
    cout << "   '" << *it << "'" << endl;
  }

  return 0;
}

编译日志:

$ xcrun clang++ -v -std=c++11 -stdlib=libc++ ../src/test_cxx.cpp
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test_cxx.cpp -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 136 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/4.2 -isysroot / -fmodule-cache-path /var/folders/nz/0x05d6p14gddzxxn8ymnn74c0000gn/T/clang-module-cache -stdlib=libc++ -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Volumes/Work/projects/web/sp3/build -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.8.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/nz/0x05d6p14gddzxxn8ymnn74c0000gn/T/test_cxx-kAQ3wk.o -x c++ ../src/test_cxx.cpp
clang -cc1 version 4.2 based upon LLVM 3.2svn default target x86_64-apple-darwin12.3.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/4.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -syslibroot / -o a.out /var/folders/nz/0x05d6p14gddzxxn8ymnn74c0000gn/T/test_cxx-kAQ3wk.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/4.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::empty() const", referenced from:
      _main in test_cxx-kAQ3wk.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


推荐答案

这是clang中的错误。我找不到关于它的错误报告。一个错误报告将不胜感激。

This is a bug in clang. I can't find a bug report on it though. A bug report would be much appreciated.

问题是 string :: empty 被标记为 always_inline,但 string 模板也标记为extern,这意味着它已经为您实例化。

The problem is that string::empty is marked "always_inline", but also the string template is marked extern, meaning that it has been instantiated already for you.

由于某种原因当我们有了这种组合,并且您形成了指向always_inline成员的成员函数指针时,clang拒绝对其进行轮廓描述,以便它可以指向它。

For some reason when we have this combination, and you form a member function pointer to the always_inline member, clang refuses to outline it so that it can point to it. It only refuses if the instantiation is marked as extern.

如果禁用extern或always_inline,则代码将起作用。您可以通过在 C ++其他标志中添加前者来禁用前者:

If you disable either the extern, or the always_inline, then the code will work. You can disable the former by including this in "C++ Other Flags":

-D'_LIBCPP_EXTERN_TEMPLATE(...)='

这篇关于clang ++ macos x c ++ 11链接器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 16:07