EN VI

Why have to write '-lstdc++' every time at the end when I compile C/C++ code in my MacBook Air M2?

2024-03-12 22:00:07
Why have to write '-lstdc++' every time at the end when I compile C/C++ code in my MacBook Air M2?

I am using MacBook Air M2, and I am new this machine. I found out that there is clang in 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 simple line like

gcc 1931D.cpp -o 1931D

it gives me error like:

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)**

But Instead when I use this below code it works fins, because I am giving reference link to C++ files, I guess so.

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

Can you please tell me why it's happening in my PC? Is there any way to avoid this

Also, if you can tell me how can I reset whole C++ or compiler(gcc) in MacBook Air m2 then it will be really helpful?

I tried this gcc 1931D.cpp -o 1931D -lstdc++ and it's working fine but, why I have to write "-lstdc++". And its also create problem in using vsCode and using its cpp extension.

Solution:

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.

The clang compiler is the default on macOS, and it aliases gcc to clang for compatibility. However, clang uses the LLVM implementation of the standard library (libc++), not libstdc++ which is the GNU version. So, when you're compiling C++ code, it's more appropriate to use clang++ or g++ (if installed) as your compiler command instead of gcc. This automatically links against the correct C++ standard library, eliminating the need for the -lstdc++ flag.

To compile C++ code on your MacBook Air M2 without needing to explicitly link the standard library, you should use:

clang++ 1931D.cpp -o 1931D

or if you have g++ installed:

g++ 1931D.cpp -o 1931D

This approach should resolve the linkage issue and is the reason why adding -lstdc++ solves the problem when using gcc (which is actually clang on macOS).

Regarding resetting your C++ compiler or the entire development setup on your MacBook Air M2, macOS doesn't have a built-in way to "reset" the compiler to its default state since it comes as part of the Command Line Tools package. If you're experiencing issues, you can reinstall the Command Line Tools or update them to the latest version. To remove and then reinstall the Command Line Tools, you can use the following commands in the terminal:

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

This will remove the current installation of Command Line Tools and prompt you to install them again.

As for the issue with VS Code and its C++ extension, ensuring that your VS Code configuration is set to use the correct compiler (e.g., clang++ or g++ instead of gcc for C++ files) should help. You can adjust this in your tasks.json file within the .vscode folder of your project or globally in your VS Code settings.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login