如何在Windows上包含外部C库 [英] How to Include external C library on windows
问题描述
我是C的新手,我试图在文本编辑器和 minGW 中使用任何IDE包含外部库不带 > windows cmd上的编译器。该库是 libPNG
在这种情况下,我真的想了解该过程如何不仅适用于此库。
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.
如果有更好的方式(也更容易)这样做,我也想知道。
推荐答案
你有两个部分需要处理:
You have two parts to take care of:
- 汇编,
- 链接。
在编译中,当你在目标文件中转换源文件时,你的编译器必须知道外部库提供的函数是什么。
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>
gcc
-
/ I< path_to_lib_folder>
cl
(视觉工作室)编译器)
-I<path_to_lib_folder>
withgcc
/I<path_to_lib_folder>
withcl
(the visual studio compiler)
在链接过程中,放置对象和库文件一起构造一个可执行文件。
In linking process, you put the object and library files together to construct an executable file.
你需要告诉链接器
- 必须使用哪些文件和
- 它可以找到库文件
你告诉链接器使用哪些文件与 -l </ code>选项,例如,
-lfoo
将告诉它搜索libfoo.so lib
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
注意:使用 cl
,您可以指明在源代码中直接使用哪个库使用 #pragma comment(lib,libfoo.lib)
Note: with cl
you can tell specify which library to use directly in your source code with #pragma comment (lib, "libfoo.lib")
添加y ou指定在哪里:
Add you specify where with:
-
-L< path_to_lib_folder>
gcc
-
/ LIBPATH:< path_to_lib_folder>
withlink
(可视工作室链接器)
-L<path_to_lib_folder>
withgcc
/LIBPATH:<path_to_lib_folder>
withlink
(the visual studio linker)
您也可以使用动态链接,但让我们从第一步开始。
You can also use dynamic linking, but let's start with the first step.
这篇关于如何在Windows上包含外部C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!