1

I am using mac osx yosemite, xcode 6.1.1. I wrote this simple piece of code. When I compile and link it with gcc, I kept getting error: symbol(s) not found for architecture x86_64

But the same code compiles successfully in xcode, or using g++, clang++. I wonder what is the difference? the project is a single file contain:

#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <stdio.h>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

int main(int argc, char** argv){
    vector<ListNode *> vec;
    vec.push_back(new ListNode(0));
    vec.push_back(new ListNode(1));
    cout<<vec[0]->val<<vec[1]->val<<endl;
    return 0;
}
mmmmm
  • 143
  • 1
  • 5
  • Simply because you need c++ compiler to successfully compile c++ code? – dbrank0 Dec 10 '14 at 07:50
  • gcc is a c++ compiler, too. it is said to evoke g++ for c++ code. – mmmmm Dec 10 '14 at 07:54
  • Yes, but it does not link with c++ library (unless explicitly told so). See http://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc. – dbrank0 Dec 10 '14 at 09:15

1 Answers1

2

If you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this). That is why xcode, g++ and clang++ will compile your code but gcc will throw errors.

GCC: GNU Compiler Collection
- Refers to all the different languages that are supported by the GNU compiler.

gcc: GNU C Compiler
g++: GNU C++ Compiler


The main differences between gcc and g++:

  1. gcc will compile: *.c/*.cpp files as C and C++ respectively.
  2. g++ will compile: *.c/*.cpp files but they will all be treated as C++ files. Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
  3. gcc compiling C files has less predefined macros.
  4. gcc compiling *.cpp and g++ compiling *.c/*.cpp files has a few extra macros.
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43