0

I have written a small game in C using SDL2. It compiles, assembles and links just fine, no errors or warning. However when I try to build it under windows, it fist failed because I have lacked the header files. I have downloaded and extracted the the development libraries for MinGW. I have told gcc to use search for the headers, and it compiles and assembles as expected. But it fails to link. Then I added the the following to the gcc command

-LD:\SDL2-2.0.8\x86_64-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2

Now the complete linking commad looks like this

gcc -ID:\SDL2-2.0.8\x86_64-w64-mingw32\lib -LD:\SDL2-2.0.8\x86_64-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2 *.o

And yet it gives me undefined reference errors.

PS:If You want to see the source code for some reason, You can find it here.

Ervin
  • 55
  • 1
  • 7
  • I'm sorry @MikeKinghan , but I must disagree with You. I may not have been clear enough, but I am trying to link against the libs, that I think are needed. – Ervin Mar 23 '18 at 17:53
  • [Your linkage consumes libraries before the object files that refer to them](https://stackoverflow.com/a/43305704/1362568), as per @user3629249's answer – Mike Kinghan Mar 23 '18 at 17:57

1 Answers1

1

the compiler (in this case gcc) when linking, evaluates the parameters on the command line from left to right.

So since the posted command line has the library references to the left of the object file references, when the librarys are processed, there are NO unsatisfied external references to be 'fixed'. So nothing from the librarys is used.

The fix, references the object files, THEN reference the libraries

Also, this expression: -ID:\SDL2-2.0.8\x86_64-w64-mingw32\lib is a compile time parameter, not a link time parameter.

Suggest using:

gcc  *.o -LD:\SDL2-2.0.8\x86_64-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Thank You far the answer. I have corrected my command and it gives all the previous errors, but this time it gives an additional error: d:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16' – Ervin Mar 23 '18 at 16:48
  • @Ervin https://stackoverflow.com/questions/20020399/undefined-reference-to-winmain16-codeblocks – aram Mar 23 '18 at 18:02