Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix g++ Undefined symbols for architecture x86_64 error?

First of all, I've read the related content on Stackoverflow on this problem but I still can't solve it. I've simplified my code as much as possible.

I've only a custom class with .h and .cpp files but I get error while trying to create an instance of this class from main.cpp.

main.cpp

#include "Customer.h"
using namespace std;

int main() {
    Customer a("string");

    return 0;
}

Customer.h

using namespace std;

class Customer {
public:
    Customer(string input);
};

Customer.cpp

#include "Customer.h"
using namespace std;

Customer::Customer(string input) {
}

The error message I get is the following?

  gcc *.cpp -o k


  Undefined symbols for architecture x86_64:
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
        _main in main-40340f.o
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
        _main in main-40340f.o
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
        _main in main-40340f.o
    "std::terminate()", referenced from:
        ___clang_call_terminate in main-40340f.o
    "___cxa_begin_catch", referenced from:
        ___clang_call_terminate in main-40340f.o
    "___gxx_personality_v0", referenced from:
        _main in main-40340f.o
        Dwarf Exception Unwind Info (__eh_frame) in main-40340f.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: error: linker command failed with exit code 1 (use -v to see invocation)

I run Mac OS X 10.9 and Sublime Text 3. gcc -v gives the following:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

It compiles without problem when I write empty constructors instead of these.

What could cause this problem?

like image 891
Han Tuzun Avatar asked May 06 '14 13:05

Han Tuzun


3 Answers

Two problems:

  1. You should #include <string>.
  2. To compile C++ code on OS X, use clang++ or g++.
like image 61
Bill Lynch Avatar answered Oct 17 '22 16:10

Bill Lynch


Use g++ instead of gcc:

$ g++ *.cpp -o k

and then the compiler driver knows to link-in the C++ runtime library. Given it's actually clang you are using, and not gcc, then this is better:

$ clang++ *.cpp -o k
like image 45
trojanfoe Avatar answered Oct 17 '22 16:10

trojanfoe


Use "g++ "instead of "gcc" to compile and link your application. It automatically knows about the C++ libraries that need to be included.

like image 3
Ernest Friedman-Hill Avatar answered Oct 17 '22 15:10

Ernest Friedman-Hill