I am fairly new to C and I am trying to include a external library without using any IDE, only text-editor and the minGW compiler on windows cmd. The library is libPNG
in this case, I would really like to understand how the process work not only for this library.
If there is a better way (and easier) to do this, I would also like to know.
You have two parts to take care of:
Compilation
In compilation, when you transform source files in object files, your compiler must know what are the functions provided by the external library.
You could declare each function you use or you can include the library header file(s) in your code:
#incude <library_file.h>
It's not enough, you will have to tell your compiler where it can find this file:
-I<path_to_lib_folder>
with gcc
/I<path_to_lib_folder>
with cl
(the visual studio compiler)
Linking
In linking process, you put the object and library files together to construct an executable file.
You need to tell the linker
- what files it must use and
- where it can find the library file
You tell the linker what files to use with the -l
options, for instance, -lfoo
will tell it to search for the libfoo.so lib
Note: with cl
you can tell specify which library to use directly in your source code with #pragma comment (lib, "libfoo.lib")
Add you specify where with:
-L<path_to_lib_folder>
with gcc
/LIBPATH:<path_to_lib_folder>
with link
(the visual studio linker)
You can also use dynamic linking, but let's start with the first step.