1

I use mingw32 to compile some C++ code. After trying some things and a lot of search I have a questions : The flags orders seems very important thought I found barely nothing that explain that on the internet. For exemple :

g++ main.cpp -L/lib -I/include -lmingw32 -lSDL2 -lSDL2main -Wall -g -o main.exe -> Compile

g++ main.cpp -L/lib -I/include -lmingw32 -lSDL2main -lSDL2 -Wall -g -o main.exe -> Not compile

g++ main.cpp -L/lib -I/include -lSDL2 -lSDL2main -lmingw32 -Wall -g -o main.exe -> Compile

I can understand why -lSDL2main need -lSDL2 first but so why the place of -lmingw32 is not important ?

EDIT :

I explain : from what I know of compilation process .cpp come first, -l indicate clearly libraries (last step of compilation). So why if I put main.cpp to the end it's continue compilation process and finish with error from the library. What mingw use if he don't have any source file, he just compile lib ?

Poulpynator
  • 716
  • 5
  • 13
  • See [Link-Options](https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-Libraries): `It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.` – ssbssa Oct 10 '18 at 10:26

1 Answers1

0

It's not the order of the fags, it's the order of the libraries. The last libraries should be the ones that everyone requires, then the second to last is the one that the last doesn't need, etc.

Why libmingw32 is not required is probably because the SDL libraries don't need it.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • If I understand well *-lmingw32* is here for windows main definition, if the main is not define mingw will add the others lib without complain ? – Poulpynator Oct 09 '18 at 19:20
  • I think there are more things there. Don't remember all the details, but you can check with a call to nm to list what's exposed. – Matthieu Brucher Oct 09 '18 at 19:23