0

I just downloaded the MingW Compiler and the glfw and glad libraries. i set up Notepad++ to compile with mingW and now i cant figure out how to include the above mentiond libraries. do i have to put the .h files in my folder with my main.cpp file or smth? where do i have to unzip my libraries to. I have absolutly no idea and have been searching the web for hours.

I have unzipped the libs into the same folder as the main.cpp file and then called smth like this in the main.cpp include<librariename/include/lib.h>

noname
  • 3
  • 1
  • Related: [https://cboard.cprogramming.com/c-programming/131142-how-link-libraries-mingw.html](https://cboard.cprogramming.com/c-programming/131142-how-link-libraries-mingw.html) – drescherjm Nov 20 '22 at 17:00
  • ***I have unzipped the libs into the same folder as the main.cpp file*** Just dumping the source code for open source libraries into your same folder is not going to work. Some may have very complicated build methods which is way beyond the abilities of a beginner. My advice to get going quickly is to install msys2 to install MinGW and use the builtin package management of msys2 (pacman) to install the dependent libraries. That will solve getting the proper libraries for your compiler and the header and library locations but you will still have to specify the libraries you need to link – drescherjm Nov 20 '22 at 17:06
  • I have a .batch file named "msys" in my minGW installation, is that enough? – noname Nov 20 '22 at 17:10
  • It should be this: [https://www.msys2.org/](https://www.msys2.org/) – drescherjm Nov 20 '22 at 17:11
  • do i have to install mingw again? it works just fine. – noname Nov 20 '22 at 17:15

1 Answers1

0

First of all, consider MinGW-w64, it's much more up to date than MinGW and supports both Windows 32-bit and 64-bit. You can get standalone versions from https://winlibs.com/, or you can install it from MSYS2 using pacman.

To use a library you need to do several things:

  • Include the header file(s) in your code using #include <someheader.h>.
  • Tell the gcc compiler where to find the header file(s) using the -I (-Iheaderpath) compiler flag.
  • Tell the gcc linker where to find the library file using the -L (-Llibrarypath) linker flag.
  • Tell the compiler to actually use the library with the -l (-llibrary) linker flag. This will make the linker look for the library file by adding lib in front of the specified library name and .a after (or .dll.a in case of shared build).

So for example if you have the following files:

  • /C/Temp/bin/glfw.dll
  • /C/Temp/include/GL/glfw.h
  • /C/Temp/lib/libglfw.a

Then you shoule add #include <GL/glfw.h to your code and build like this (if your code is in main.c):

gcc -c -o main.o main.c -I/C/Temp/include
gcc -o main.exe main.o -L/C/Temp/lib -lglfw

In the above example the first line is the compiler step and the second line the linker step. You could combine both steps like this:

gcc -o main.exe main.c -I/C/Temp/include -L/C/Temp/lib -lglfw

But as your project grows it's better to keep compiler and linker steps separated.

In fact, as your project grows you may want to consider using some kine of build system (like make or cmake).

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40