How to Include external C library on windows

空扰寡人 提交于 2019-12-01 21:41:15
Mathieu

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!