3

I'm trying to understeand how to use structs as lists in C++. I came up with a piece of code that to my understeanding should not result in any errors, but it did..

My code is this:

struct item {
int data;
struct item *next;
};

struct item *begin = NULL;

void add(int x) {
    struct item *a = new struct item();
    a->data = x;
    a->next = begin;
    begin = a;
}

int main() {

    add(2);
    printf("%d\n", begin->data);

    return 0;
}

and it gives me this:

Undefined symbols for architecture x86_64:
"operator new(unsigned long)", referenced from:
  add(int) in structtest-f49486.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I use GCC inside my mac terminal to run my code. I haven't seen this type of error before. I have found that the error is not present when I remove the line

struct item *a = new struct item();

Can anyone tell me what's wrong here?

Thanks,

Merijn

  • in c++ you don't need to use the struct keyword everytime you are using it's type – Krab Apr 26 '15 at 12:21
  • Apart of the point of declaration, `struct`, `union`, `enum` and `class` can be omitted in C++, and in a `new` expression it even has to be omitted. Please check some examples from a C++ tutorial. – Ulrich Eckhardt Apr 26 '15 at 12:22

1 Answers1

9

Use g++ not gcc. It looks like it's trying to link your C++ code as C code.

GCC is weird like this. When you use g++ to link it silently adds C++ support libraries, such as the one that defines default operator new.

And yes, it "forgets" that it just compiled the code as C++. Don't ask me why.


Regarding clang and gcc, here's what I see on my Mac:

$ gcc --version
gcc (MacPorts gcc48 4.8.4_0) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ type -a gcc
gcc is /opt/local/bin/gcc
gcc is /usr/bin/gcc
$ /usr/bin/gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
$ ls -lF /usr/bin/gcc
-rwxr-xr-x  1 root  wheel  14160 Sep 29  2014 /usr/bin/gcc*
$ ls -lF /usr/bin/g++
-rwxr-xr-x  1 root  wheel  14160 Sep 29  2014 /usr/bin/g++*
$ file /usr/bin/gcc
/usr/bin/gcc: Mach-O 64-bit executable x86_64
$ file /usr/bin/g++
/usr/bin/g++: Mach-O 64-bit executable x86_64
$ diff /usr/bin/g++ /usr/bin/gcc
Binary files /usr/bin/g++ and /usr/bin/gcc differ

Note that I have MacPorts installed, have real GCC 4.8 installed through it, and have configured it to "replace" Apple's "gcc". BTW, Apple's gcc is not a symlink.

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • From the last line of the error message, it seems that clang, - not gcc/g++ - was called for the linker stage. – Michel Billaud Apr 26 '15 at 12:34
  • @MichelBillaud I thought the same thing, but I think on Mac 'gcc' is just a symlink to 'clang' anyway, and 'g++' to 'clang++' which is of course what is needed. – davmac Apr 26 '15 at 12:34
  • Yes, on the Mac, `gcc` actually executes `clang`, which tries to act as GCC-like as possible but doesn't always succeed. – Mike DeSimone Apr 26 '15 at 12:38
  • 1
    Well, if it is clang, it should be clang++ or clang with some option http://stackoverflow.com/questions/9148488/how-do-i-compile-c-with-clang – Michel Billaud Apr 26 '15 at 12:38
  • @MichelBillaud yes... exactly as I said in the comment above. – davmac Apr 26 '15 at 12:39
  • Again, it's trying to behave like gcc, including assuming that you're linking a C program if `clang` is invoked (via the `gcc` pass-through in this case). Switching to `g++` (which would reroute to `clang++`) would fix things. – Mike DeSimone Apr 26 '15 at 12:47