简体   繁体   中英

How to include Libraries without an IDE

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>

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM