How to Include external C library on windows

前端 未结 1 602
抹茶落季
抹茶落季 2021-01-21 03:19

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 compi

相关标签:
1条回答
  • 2021-01-21 03:42

    You have two parts to take care of:

    • compilation,
    • linking.

    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.

    0 讨论(0)
提交回复
热议问题