English 中文(简体)
在我汇编C/C++关于MacBook Air M2的法典时,我为什么必须每刻写——lstdc++? [复制]
原标题:Why do I have to write -lstdc++ every time at the end when I compile C/C++ code on a MacBook Air M2? [duplicate]

I am using a MacBook Air M2, and I am new this machine. I found out that there is Clang on MacBook which is directly taken in use by default for C and C++ code in MacBook. But my system is having one issue or kind of issue, when I compile code with a simple line like:

gcc 1931D.cpp -o 1931D

它给我留下了一个错误:

Undefined symbols for architecture arm64:
  "std::logic_error::logic_error(char const*)", referenced from:
      std::length_error::length_error[abi:ue170006](char const*) in 1931D-f28fab.o
  "std::length_error::~length_error()", referenced from:
      std::__1::__throw_length_error[abi:ue170006](char const*) in 1931D-f28fab.o
  "std::bad_array_new_length::bad_array_new_length()", referenced from:
      std::__throw_bad_array_new_length[abi:ue170006]() in 1931D-f28fab.o
  "std::bad_array_new_length::~bad_array_new_length()", referenced from:
      std::__throw_bad_array_new_length[abi:ue170006]() in 1931D-f28fab.o
  "std::__1::basic_istream<char, std::__1::char_traits<char>>::operator>>(long long&)", referenced from:
      _main in 1931D-f28fab.o
      solve() in 1931D-f28fab.o
      solve() in 1931D-f28fab.o
      solve() in 1931D-f28fab.o
      solve() in 1931D-f28fab.o
  "std::__1::cin", referenced from:
      _main in 1931D-f28fab.o
      solve() in 1931D-f28fab.o
      solve() in 1931D-f28fab.o
  "std::terminate()", referenced from:
      ___clang_call_terminate in 1931D-f28fab.o
  "typeinfo for std::length_error", referenced from:
      std::__1::__throw_length_error[abi:ue170006](char const*) in 1931D-f28fab.o
  "typeinfo for std::bad_array_new_length", referenced from:
      std::__throw_bad_array_new_length[abi:ue170006]() in 1931D-f28fab.o
  "vtable for std::length_error", referenced from:
      std::length_error::length_error[abi:ue170006](char const*) in 1931D-f28fab.o
   NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "operator delete(void*)", referenced from:
      std::__1::allocator<long long>::deallocate[abi:ue170006](long long*, unsigned long) in 1931D-f28fab.o
      void std::__1::__libcpp_operator_delete[abi:ue170006]<void*>(void*) in 1931D-f28fab.o
  "operator new(unsigned long)", referenced from:
      std::__1::allocator<long long>::allocate[abi:ue170006](unsigned long) in 1931D-f28fab.o
      void* std::__1::__libcpp_operator_new[abi:ue170006]<unsigned long>(unsigned long) in 1931D-f28fab.o
  "___cxa_allocate_exception", referenced from:
      std::__1::__throw_length_error[abi:ue170006](char const*) in 1931D-f28fab.o
      std::__throw_bad_array_new_length[abi:ue170006]() in 1931D-f28fab.o
  "___cxa_begin_catch", referenced from:
      ___clang_call_terminate in 1931D-f28fab.o
  "___cxa_call_unexpected", referenced from:
      std::__1::vector<long long, std::__1::allocator<long long>>::max_size() const in 1931D-f28fab.o
      std::__1::vector<long long, std::__1::allocator<long long>>::__base_destruct_at_end[abi:ue170006](long long*) in 1931D-f28fab.o
      std::__1::allocator<long long>::deallocate[abi:ue170006](long long*, unsigned long) in 1931D-f28fab.o
  "___cxa_free_exception", referenced from:
      std::__1::__throw_length_error[abi:ue170006](char const*) in 1931D-f28fab.o
  "___cxa_throw", referenced from:
      std::__1::__throw_length_error[abi:ue170006](char const*) in 1931D-f28fab.o
      std::__throw_bad_array_new_length[abi:ue170006]() in 1931D-f28fab.o
  "___gxx_personality_v0", referenced from:
      /private/var/folders/pt/csddh2cd0fq4krbz3vjkgf7m0000gn/T/1931D-f28fab.o
**ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)**

但是,当我使用以下代码时,它会进行罚款,因为我指的是C++文档,我是猜测。

gcc 1931D.cpp -o 1931D -lstdc++

我的PC为什么会发生这种情况? 是否有办法避免这种情况?

另外,我如何在MacBook Air M2中重新确定整个C++或汇编者?

I Trial gcc 1931D.cpp -o 1931D -lstdc++, and it s working penalty, but, why do I must written “-lstdc++”? 它还在使用视觉演播室法和使用C++推广法方面造成了问题。

最佳回答

When you compile C++ code on a MacBook, especially on one with the M1 or M2 chip which uses the ARM64 architecture, using gcc directly sometimes doesn t automatically link against the C++ standard library, leading to the undefined symbols errors you ve encountered. The reason your code compiles successfully with the -lstdc++ flag is that this explicitly tells the compiler to link against the GNU Standard C++ Library, resolving the undefined symbols related to C++ features.

<代码>clang汇编器是关于macOS的缺漏,其次为gccclang,以备兼容性。 但是,clang 采用LLVERV实施标准图书馆(libc++),而不是libstdc++,即GSNU版本。 因此,当你重新汇编C++代码时,更宜使用<编码>clang++或g++/code>(如果安装)作为你的汇编者指挥而不是gcc。 这与正确的C++标准图书馆自动连接,从而不再需要<代码>。 - 国旗。

为了编纂关于你的MacBook Air M2的C++代码,而无需明确连接标准图书馆,你应使用:

clang++ 1931D.cpp -o 1931D

或如果您有<代码>g++ 安装:

g++ 1931D.cpp -o 1931D

这种做法应当解决联系问题,是为什么增加<代码>的原因。 -lstdc++ 在使用<代码>gcc时解决问题 (实际上是clang on macOS)。

关于重新确定你的C++汇编者或围绕你的MacBook Air M2进行的整个开发,MacOS公司没有内在地将汇编者“重建”到其违约状态,因为它是指挥系统一揽子工具的一部分。 如果你再次遇到问题,你可以重新使用指挥系统工具或将其更新到最新版本。 为了拆除并重新安装指挥线工具,你可以在终端使用以下指挥器:

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

这将消除目前安装的指挥线路工具,促使你再次安装。

关于VS代码及其C++延伸的问题,确保确定你的VS代码组合使用正确的汇编(例如,clang++/code>或g++,而不是gcc/code>(C++文档)。 您可在<代码>tasks.json 文档中,在<代码>.vscode上对您的项目或全球在《瑞士刑法》中的项目进行校正。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签