Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined symbols for architecture x86_64: Which architecture should I use?

Tags:

c++

macos

I'm trying to do some really simple stuff in C++, but I can't find any information on how to tackle this. Even the book I have just says "Just compile and run the program".

test.cpp

#include <iostream> 
using namespace std;

int main() 
{ 
    cout << "Never fear, C++ is here!"; 
    return 0;
}

The compiler says:

Undefined symbols for architecture x86_64:
  "std::cout", referenced from:
      _main in ccVfJHGs.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in ccVfJHGs.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccVfJHGs.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in ccVfJHGs.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I tried compiling with flags like -arch i386 and -m32 but it always says it's the wrong architecture. Which one should I use?

I'm doing this on a Mac but not using XCode, just gcc.

like image 769
oskob Avatar asked Nov 07 '11 09:11

oskob


People also ask

What is undefined symbols for architecture x86_64?

Why Is the Undefined Symbols for Architecture x86_64: Error Happening? This error is happening due to the lack of included values inside the declared statements in your code. The browser is going to render the information incorrectly and show this error, especially if you are working with Main and Similarity tools.

How do I fix undefined symbol in XCode?

The error Undefined symbols for architecture arm64: "_OBJC_CLASS_$_SKAdImpression" during the iOS build usually happens if XCode or CocoaPods version is lower than required. To fix it update XCode to 12.5 or higher and CocoaPods to 1.10. 0 or higher.

What does undefined symbol mean in XCode?

Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_xxx", referenced from: objc-class-ref in yyy.o. This generally means, you are calling "xxx" (it may be a framework or class) from the class "yyy". The compiler can not locate the "xxx" so this error occurs.

Why is there an undefined symbol error?

A symbol remains undefined when a symbol reference in a relocatable object is never matched to a symbol definition. Similarly, if a shared object is used to create a dynamic executable and leaves an unresolved symbol definition, an undefined symbol error results.


3 Answers

The error isn't that it's the wrong architecture, it's that std::cout (and other symbols) isn't defined.

You should compile and link with g++ not gcc, to automatically link with correct C++ libraries.

like image 107
Some programmer dude Avatar answered Oct 16 '22 23:10

Some programmer dude


The error is caused because you're compiling with gcc, which only default-links libc. You need to compile with g++ so that libstdc++ is auto-linked in too.

like image 5
moshbear Avatar answered Oct 16 '22 22:10

moshbear


Use g++ instead of gcc to link with exact c++ libraries

like image 2
Muhammed Shibin Avatar answered Oct 16 '22 23:10

Muhammed Shibin