-1

I bet the solution to this question is very simple, but I'm wondering why I can't get gcc to compile my c++ 11 code.

EDIT: Code removed to simplify problem. Any program would replicate the same error.

I am using the gcc compiler 4.7 as downoaded here: https://code.google.com/p/mingw-builds/downloads/detail?name=x86_64-mingw32-gcc-4.7.0-release-c%2Cc%2B%2B%2Cfortran-sjlj.zip&can=2&q=.

The command I am using to compile is simply gcc Swamp.cpp -std=c++11. The code only complains about c++ 11 stuff if I compile with gcc Swamp.cpp.

To my eyes, the errors given by the compilation is garbage:

 C:\Users\Owner\Documents\GitHub\CompetitiveProgramming\TestProject\CodeProject>gcc Swamp.cpp -std=c++11
 C:\Users\Owner\AppData\Local\Temp\ccRKn67x.o:Swamp.cpp:(.text+0x67): undefined reference to `__gxx_personality_sj0'
 C:\Users\Owner\AppData\Local\Temp\ccRKn67x.o:Swamp.cpp:(.text+0x30e): undefined reference to `__gxx_personality_sj0'
 C:\Users\Owner\AppData\Local\Temp\ccRKn67x.o:Swamp.cpp:(.text+0x72e): undefined reference to `__gxx_personality_sj0'
 C:\Users\Owner\AppData\Local\Temp\ccRKn67x.o:Swamp.cpp:(.text+0x78a): undefined reference to `std::cout'
 …etc…
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Bryce Sandlund
  • 487
  • 2
  • 5
  • 17

1 Answers1

7

There are two different commands for compiling C++ code with GCC, gcc and g++. The difference is really only that g++ links automatically with the C++ runtime library, and gcc doesn't.

The errors are all about missing symbols from the GCC C++ runtime library, and have nothing to do with C++11 features you use. To fix the errors you either need to link with the C++ runtime library yourself manually, or use the g++ command to handle it automatically.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Yeah, dunno why I forgot/ didn't try g++. It works. Thanks! – Bryce Sandlund Sep 10 '14 at 06:13
  • So here's the bigger question... The code produces different output via command line than via Visual Studio. Any ideas what could be the issue? – Bryce Sandlund Sep 10 '14 at 06:23
  • @BryceSandlund Step through the code with a debugger to find out. – Some programmer dude Sep 10 '14 at 06:31
  • I'm not sure how to use a debugger via command line. And when I debug through Visual Studio it functions exactly how I expect it to. – Bryce Sandlund Sep 10 '14 at 06:32
  • @BryceSandlund You might want to add the flags `-Wall -Wextra -pedantic` when compiling with GCC, this will enable quite a few warning options. Warnings in code is often a sign of you doing something you should not, probably something that will lead to undefined behavior. Also, when indexing the vectors, do you check that the indexing is valid? – Some programmer dude Sep 10 '14 at 06:34
  • Only getting warnings based on signed/ unsigned integer comparison with checking i < vector.size(), which shouldn't be an issue. Just changed some of the I/O which I wasn't sure about, but the outputs are still different. What do you mean by the indexing is valid? – Bryce Sandlund Sep 10 '14 at 06:51
  • @BryceSandlund Are all vector indexes inside the bounds of the vectors? You're never have an index out of bounds? Remember that a vectors index operator `[]` doesn't check bounds. – Some programmer dude Sep 10 '14 at 07:08
  • Problem was related to undefined behavior: `targs[s] = targs.size();`. http://stackoverflow.com/questions/25768486/c-code-producing-different-outputs-through-visual-studio-c-and-gcc. Thanks for your help! – Bryce Sandlund Sep 10 '14 at 17:31