Compiling SDL on Windows with Mingw gcc - c

I downloaded SDL for MinGW here : https://www.libsdl.org/download-2.0.php.
But when I try to compile with gcc it can't find the libraries.
It says it cannot find "SDL2/SDL.h".
I tried running GCC with the parameters to include the path after moving the folder containing
SDL2 to the same directory of the project.
I used this command:
gcc main.c render.c sprite.c logic.c menu.c level.c -o release -l:SDL2\x86_64-w64-mingw32\lib\libSDL2main.a -l:SDL2\x86_64-w64-mingw32\lib\libSDL2.a -ISDL2

If you want the compiler to find the libraries, you need to include the directory it is in using -I/dir/

Related

Why gcc under Windows O.S. produces a .o instead of a .lib file when compiling static libraries?

I am using gcc 8.1.0 on Windows. To install it I set up Code::Blocks on my computer and updated the environment variable list by adding the path to the gcc.exe program within the installation folder of CodeBlocks. The file editor I used was the built-in editor in Visual Studio. The terminal to compile was the power shell from Visual Studio as well.
In the library development folder I have the files mul.c and mul.h. Their content is irrelevant.
To compile the library I use the command:
gcc -c mul.c
When I run it, it creates a file object mul.o and not mul.lib. I needed to use the option -o mul.lib to successfully create the desired extension file. After placing the header, the .lib file and the main.c in the same parent folder I am obvioudly able to build the executable by running.
gcc main.c -I./include -L/static -lmul -o my_program.exe
I have two questions:
Why does gcc produces a .o if I am in a Windows environment?
I followed a tutorial that compile the static library under Linux and it names it libmul.o, in this way the -lmul option is able to retrieve the library. But if I call my generated static library libul.lib it generates the error:
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-ingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmul
collect2.exe: error: ld returned 1 exit status
Are these a normal behaviours by gcc, or is it side effect of making gcc available just by updating the Windows environmental variables list?
Thank you to the community in advance.
GCC comes from the *nix world where libraries have the .a extension. When using GCC+MinGW this remains the case.
Shared libraries in MinGW are .dll files but their libraries for linking are .dll.a files.
The advantage of .a files is that a lot of sources build out of the box on Windows with MinGW, especially when using MSYS2 shell.
If you use -l it will look for .a (or .dll.a for shared build) file adding the lib prefix and the extension automatically.
So -lmul will look for libmul.a (static, e.g. when --static linker flag is given) or libmul.dll.a (shared).
By the way, you are using quite an old GCC 8.1.0.
As I write this current version is 12.2.0. Check https://winlibs.com/ for a standalone download (instructions on how to configure in Code::Blocks are on the site) or use MSYS2's package manager pacman.

Dynamic linking libgit2 .so in gcc

I'm running a Debian (Buster) container and my goal is to compile a small program I wrote which relies on libgit2. First, I was installing libgit2 via the libgit2-dev package and my Makefile had the following:
gcc -O2 -fpic -shared -I /usr/local/include -lgit2 -o output.so my_app.c
However, I'd rather have a "cleaner" environment and install libgit2 via the libgit-27 which, AFAIK, only installs the shared object binary instead of also including the development files like libgit2-dev does.
Using find I can find where the .so file is installed into:
$ find / -name "*git2*" -print 2>/dev/null
/usr/lib/x86_64-linux-gnu/libgit2.so.0.27.7
/usr/lib/x86_64-linux-gnu/libgit2.so.27
/usr/share/doc/libgit2-27
/var/lib/dpkg/info/libgit2-27:amd64.list
/var/lib/dpkg/info/libgit2-27:amd64.symbols
/var/lib/dpkg/info/libgit2-27:amd64.md5sums
/var/lib/dpkg/info/libgit2-27:amd64.shlibs
/var/lib/dpkg/info/libgit2-27:amd64.triggers
and I've been trying several combinations of linking this .so with gcc like:
gcc -O2 -fpic -shared -L /usr/lib/x86_64-linux-gnu/ -libgit2.so.27 -o output.so my_app.c
but so far I always get the following error:
my_app.c:1:10: fatal error: git2.h: No such file or directory
#include <git2.h>
^~~~~~~~
compilation terminated.
I understand this is a glaring lack of knowledge on how C compilation works. My two questions are:
Is it possible to compile my program by just relying on the libgit2-27 Debian Buster package instead of libgit2-dev? If not, why?
If yes, an example and explanation would be appreciated!

Linking libraries with a MinGW project in VS code

I am using MinGW version 4.5.2 on windows 10. I am trying to link multiple libraries to a project in VS code. When I try run this command: g++ main.c -o main.exe -I"C:\\Users\\USER\\Programming\\OPenGL Library\\glfw\\include\\glfw3.h" -L"C:\\Users\\USER\\Programming\\OpenGL Library\\glfw\\lib-mingw-w64\\". I get a compile error: main.c:2:24: fatal error: GLFW\glfw3.h: No such file or directory What is wrong with the command above that gives this linking error. As far as I can tell I have pointed MinGW to the library and specified the header file. I am not sure what else I need to add to the command for link the GLFW library as well as other libraries such as GLEW or glm.
If your include directive looks like
#include <GLFW/glfw3.h>
you need to supply a path to a directory which itself contains a directory named GLFW.

Compile GLFW cannot find -lglfw3

I want to learn OpenGL programming with GLFW library. But I have problem when compiling the program. I followed tutorial on http://www.glfw.org/docs/latest/quick.html and copied the full program. I download the 64-bit version library on http://www.glfw.org/download.html.
I changed the first line from include <GLFW/glfw3.h> to include "GLFW/glfw3.h". I've put the GLFW folder inside the folder along with glfw3.dll, glfw3dll, and libglfw3.a
I'm running on Windows 7. This is my gcc version
gcc --version
gcc (tdm64-1) 5.1.0
I use this command for compiling
gcc test.c -Llib-mingw/ -lglfw3 -lopengl32 -lgdi32
But I got this error
cannot find -lglfw3
What should I do to make it work?
If you are still having problems with this compilation/linking problem, try set the glfw full lib folder path in gcc or g++ lib path with -L parameter, as simplified below. Double quotes and non-relative paths should work.
g++ "-L<path_to_glfw>\\glfw\\lib-mingw" -o <executable>.exe <.o files list> -lopengl32 -lglfw3 -lgdi32
Two notes:
There is no need to pass .a extension. MinGW g++/gcc compiler will automatically understand filename.
If you are using the 64-bit version of glfw, switch -L parameter to \glfw\lib-mingw64 (or respective 64-bit library location folder)

Eclipse doesn't compile *.C with gcc

I am using Eclipse Kepler and running it under Win7 64-Bit. As compiler I use the gcc (4.8.1) from MinGW. Now I have the following problem:
//edit: Reformulated question to make it more clear
I have a project containing of one source-file with C-Code: main.c
This file can be compiled over 2 ways:
Start the compiler over the command-line: gcc -o main.exe main.c
Start the compiler over Eclipse by starting the normal build-routine (which also calls the gcc)
Now for some reason I want to add some C++-Code, but I still want to compile it with the gcc.
The gcc itself decides how to compile over the file extension - This means, if main.c contains C++-Code and I call gcc -o main.exe main.c it won't work. To make the compiler realize it's C++ I have to change the file-extension to somthing like .C or .cpp and then it will work.
Now back to Eclipse:
When I change my Sourcefile to main.C Eclipse interprets it as C++ File, meaning it changes the Code-Highlighting. When I now start a build process over Eclipse it just tells me
Info: Nothing to build for PROJECT
This means there is not even a call to the gcc-compiler. My guess is, that Eclipse somehow doesn't want to call the gcc, because the source-file is marked as C++-File.
//edit2: Just tried - when I have a C++-Projekt Eclipse just ignores the *.C or the *.cpp-files. I guess I have to add them manually, so they're built too ... but where?
The g++ is the compiler to compile C++ codes.
Change you compiler in you eclipse project.

Resources