0

I'm trying to combine object files created from C++ files into an executable using gcc. Unfortunately, gcc is giving me thousands of undefined reference errors to strings, arrays, etc.

I am doing this on a Windows machine, so no terminal commands; only cmd commands.

I'm simply doing:

gcc a.o b.o c.o -o prgm.exe

What am I missing/doing wrong?

EDIT:

I recreated the .o files with g++ doing: g++ a.cpp -g -c -Wall -std=c++0x -lSDLmain -lSDL -lSDL_image -lSDL_ttf -IC:\SDL-1.2.14\include -o a.o, where a.cpp and a.o are the directories where i keep the files, not the g++ directory

Then, I did g++ a.o b.o c.o -o prgm.exe. This gave dozens (I guess that's an improvement?) errors like

undefined reference to `_SDL_SetColorKey'

but I included SDL didnt I?

The final error from this is:

c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.0/../../../li
bmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `_WinMain
@16'
collect2.exe: error: ld returned 1 exit status

int main(int argc, char * argv[]) is in the code

calccrypto
  • 8,583
  • 21
  • 68
  • 99
  • 2
    Do you have a `g++` command? If so try using that instead of `gcc` – Alan Curry Aug 20 '12 at 22:36
  • 1
    We'd need to know a lot more, like: gcc version, what libs do you need to specify for the linker, are you using cygwin? etc.. – j.w.r Aug 20 '12 at 22:38
  • im using gcc 4.7.0, not on cygwin. the only libs i need for the linker i think are SDL and the standard library – calccrypto Aug 20 '12 at 22:42
  • The problem with `_WinMain()` comes because CodeBlocks/MinGW thinks you are linking a Windows GUI API program, where the entry point is `WinMain()` rather than `main()`. You will either need to provide `WinMain()` instead of `main()`, or find the option to MinGW GCC that tells it to link a console application (non-GUI) instead of a GUI one. It should be a simple case of manual bashing. If you really can't get Google to help you, trying replying to this comment, but it should be straight-forward. (I'd guess a search term 'mingw gcc winmain main' should work; I live to be proved wrong.) – Jonathan Leffler Aug 21 '12 at 00:09

3 Answers3

2

You are trying to link a C++ program with the C linker. You need to use g++ instead of gcc.

Generally speaking gcc is for compiling/linking C, while g++ is for C++. IIRC compiling C++-code with gcc works by virtue of dispatching according to the file extension. Linking C++ code with gcc however does not work, since it won't link the C++ standard libraries, resulting in your undefined reference errors.

If this does not solve your problem, you might want to give us a more concrete description of your errors and your system.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
Grizzly
  • 19,595
  • 4
  • 60
  • 78
1

GCC is the C compiler. Your code is C++ so you need to use G++ to do the linking:

g++ a.o b.o c.o -o prgm.exe

This automatically adds the C++ libraries to the link line, resolving many if not all of your missing references.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Based upon your updates then I think you'd need to do the following:

g++ a.cpp b.cpp c.cpp -g -Wall -IC:\SDL-1.2.14\include -LC:\SDL-1.2.14\lib -std=c++0x -lSDLmain -lSDL -lSDL_image -lSDL_ttf  -o prgm.exe

I'm guessing C:\SDL-1.2.14\lib exists based upon where the headers are located.

j.w.r
  • 4,136
  • 2
  • 27
  • 29