-3

So, I have a command I run to compile this experiment. It looks like this.

"F:\Software-Development\mingw32\bin\gcc.exe" "F:\Software-Development\Sources\experiment\source\main.c" -L "F:\Software-Development\Sources\experiment\objects\" -Wall -o "F:\Software-Development\Sources\experiment\~build\EXP" -lSDL2

main.c is just a slightly modified tutorial from Lazyfoo so that it SHOULD work in pure C, though I can't actually test it. If the actual source code is required for whatever reason, I do have a testing package I assembled here. It probably won't work, but again, can't test it for errors. Please do not inform me of potential errors in my code, unless it's actually relevant to this error.

\objects\ is where my .a files are stored. For some inexplicable reason, however, whenever I execute this command, it throws an error. Not a cannot find -lSDL2: No such file or directory error, but rather an undefined reference to `SDL_X' error.

This makes no sense to me, the .a files are right there, and I'm not sure what is going on. My only clue in is that removing -L throws the cannot find -lSDL2 error, while modifying what's in the -L parameter changes literally nothing. I've asked elsewhere for help, I've looked around at problems, and it seems that I have left but to ask Stack Overflow for something that's probably really trivial.

Missingno50
  • 83
  • 10
  • 1
    There is always an _explicable_ reason, and that reason is _always_ that your environment is not correctly configured. – Dúthomhas Sep 24 '22 at 22:55
  • Cool. It doesn't explain where the misconfiguration is. How about you take a look at my batch file which does this and tell me where it is? [Because I sure don't see where I'm going wrong with this.](https://pastebin.com/maCQBvHu) – Missingno50 Sep 24 '22 at 23:05
  • Does this answer your question? [How do I use SDL2 in my programs correctly?](https://stackoverflow.com/questions/64396979/how-do-i-use-sdl2-in-my-programs-correctly) – HolyBlackCat Sep 25 '22 at 04:43
  • In particular, on Windows you need `-lmingw32 -lSDL2main -lSDL2` rather than just `-lSDL2`. If it still doesn't work, this means you've been using x32 libs with an x64 compiler, or vice versa. – HolyBlackCat Sep 25 '22 at 04:44
  • Well, that didn't fix things at all. I added all of the flags, and changed the location of the -Ldir flag, and that gave me a new error. Particularly, "cannot find -lSDL2x:". Fixing the position of the -Ldir flag back to where it was yielded no advancements, it's still throwing the undefined errors no matter which set of .a SDL2 files I'm using(i686 or x86_64). In batch, the command now looks like this "%compiler:~0,-1%\gcc.exe" %build:~0,-1%\source\main.c" -ggdb -Wall -L "%build:~1,-1%\objects\" -o %build:~0,-1%\~build\%dat:~1,-1%" -lmingw32 -lSDL2main -lSDL2". – Missingno50 Sep 25 '22 at 04:55
  • What's `:~1,-1%`? Why does it say `-lSDL2x:` rather than `-lSDL2`? Does it work if you compile from the terminal and specify the paths manually, not using variables? If not, where did you get your GCC? The one from [MSYS2](https://stackoverflow.com/q/30069830/2752075) is known to work correctly. – HolyBlackCat Sep 26 '22 at 06:19
  • `:~1,-1%` is the substring command for Batch(in this case, start at the 2nd letter in a string, end to the 2nd to last character in a string.) `-lSDL2x` is just a shorthand for `-lSDL2 -lSDL2main` and sometimes `-lSDL2test`, apologies that this was not clear. It does not work even when I specify manually where it's supposed to go. I got my GCC from [MinGW-w64's official website](https://www.mingw-w64.org/), specifically the latest[(Release 12.2.0-rt_v10-rev0)](https://github.com/niXman/mingw-builds-binaries/releases/tag/12.2.0-rt_v10-rev0) i686 and x86_64 sljl Win32 threading builds. – Missingno50 Sep 26 '22 at 23:54
  • Please use `@username` when replying, otherwise we don't get notifications. – HolyBlackCat Sep 29 '22 at 07:16
  • `-L` shouldn't change anything regarding `undefined reference` errors. Perhaps you have multiple sets of .a files, some of them not matching your compiler. Adding `-v` should print the library paths that were used. – HolyBlackCat Sep 29 '22 at 07:18
  • @HolyBlackCat I wish it were that easy to brush it off, but I didn't have multiple sets of the same .a files in there. This is proven by the fact that removing `-L` and just moving everything to the `lib` folder actually rectified the issue. Furthermore, I went on to count after the 5th or so retry, and I exchanged all of my .a files a whooping 92 times. The `lib` files are also the ones that were in the `object` folder from my very last test. The only conclusion I can possibly draw from this is that mingW's `gcc.exe` don't account for `-L` correctly. Atings take up valuable characters :( – Missingno50 Sep 29 '22 at 18:10
  • Again: add `-v` to commands with and without `-L`, and compare the resulting library paths. – HolyBlackCat Sep 30 '22 at 07:40
  • @HolyBlackCat Sorry, I didn't get your comment for some reason. Here's the pastebin with your requested test. https://pastebin.com/2xQ2Ngc2 I ran both. Use W/O as a ctrl+F term to find the split between the two tests. – Missingno50 Oct 24 '22 at 01:06

1 Answers1

1

So I've discovered the reason why. This is absolutely ridiculous, but mingw's GCC compilation does not appear to account for -L correctly. The only solution you can do to rectify this error is to remove the -L dir flag and putting the .a files into the mingX\lib folder.

Nabz C
  • 538
  • 1
  • 9
  • 28
Missingno50
  • 83
  • 10