windows 您实际上如何使用 C 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1749079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you actually use a C library?
提问by unknown
I'm sure this question has been asked many times, but I can't figure this out. Bear with me.
我确定这个问题已经被问过很多次了,但我想不通。忍受我。
So when you download a library, you get a bunch of .c and .h files, plus a lot of other stuff. Now say you want to write a program using this library.
所以当你下载一个库时,你会得到一堆 .c 和 .h 文件,还有很多其他的东西。现在假设你想使用这个库编写一个程序。
I copy all the .h files into my project directory. It just doesn't compile.
我将所有 .h 文件复制到我的项目目录中。它只是不编译。
Great, so then I get the library as a bunch of .dll's, and i copy the dlls into my project directory. Still doesn't compile.
太好了,然后我将库作为一堆 .dll 文件获取,然后将这些 dll 文件复制到我的项目目录中。还是不编译。
How does this work?
这是如何运作的?
What do you do, like right after creating the folder for your project? What parts of the library package do you copy/paste into the folder? How do you make it so that it can compile? Go through the steps with me please.
在为您的项目创建文件夹后,您会做什么?您将库包的哪些部分复制/粘贴到文件夹中?你如何制作它以便它可以编译?请和我一起完成这些步骤。
- Where to put the .h files?
- Where to put the .dll files?
- How to compile?
- .h 文件放在哪里?
- 将 .dll 文件放在哪里?
- 如何编译?
Thanks.
谢谢。
(the library I'm trying to get working is libpng, I'm in windows with MinGW, and i'm looking to compile from command-line like usual.)
(我正在尝试使用的库是 libpng,我在 Windows 中使用 MinGW,我希望像往常一样从命令行进行编译。)
(from what i gather, you put the .h files in directory A and the .dll files in directory B and you can use -l and -L compiler options to tell the compiler where to find them, is this correct?)
(根据我收集的信息,您将 .h 文件放在目录 A 中,将 .dll 文件放在目录 B 中,您可以使用 -l 和 -L 编译器选项告诉编译器在哪里可以找到它们,这是正确的吗?)
回答by Mark Pim
Here's a brief guide to what happens when you compile and build a basic C project:
以下是编译和构建基本 C 项目时发生的情况的简要指南:
The first stage compiles all your source files - this takes the source files you've written and translates them into what are called object files. At this stage the compiler needs to know the declaration of all functions you use in your code, even in external libraries, so you need to use
#include
to include the header files of whatever libraries you use. This also means that you need to tell the compiler the location of those header files.With GCCyou can use the-I
command line to feed in directories to be searched for header files.The next stage is to link all the object files together into one executable. At this stage the linker needs to resolve the calls to external libraries. This means you need the library in object form.Most libraries will give you instructions on how to generate this or might supply it ready built. Under Linux the library file is often a
.a
or.so
file, though it might just be a.o
. Again you can feed the location of the library's object file to GCC with the -L option.
第一阶段编译您所有的源文件 - 这会将您编写的源文件转换为所谓的目标文件。在这个阶段,编译器需要知道你在代码中使用的所有函数的声明,即使是在外部库中,所以你需要使用
#include
包含你使用的任何库的头文件。这也意味着您需要告诉编译器这些头文件的位置。使用GCC,您可以使用-I
命令行输入要搜索头文件的目录。下一阶段是将所有目标文件链接到一个可执行文件中。在这个阶段,链接器需要解析对外部库的调用。这意味着您需要对象形式的库。大多数库都会为您提供有关如何生成它的说明,或者可能会提供现成的。在 Linux 下,库文件通常是一个
.a
或.so
文件,尽管它可能只是一个.o
. 同样,您可以使用 -L 选项将库目标文件的位置提供给 GCC。
Thus your command line would look like this:
因此,您的命令行将如下所示:
gcc myProg.c -I/path/to/libpng/include -L/path/to/libpng/lib -lpng -o myProg.exe
(Note that when using the -l
command line GCC automatically adds lib
to the start of the library, so -lpng
causes libpng.a
to be linked in.)
(注意在使用-l
命令行时 GCC 会自动添加lib
到库的开头,所以-lpng
会导致libpng.a
被链接进去。)
Hope that helps.
希望有帮助。
回答by Ferenc Deak
Doing it under windows (supposing you user Visual Studio)
在 Windows 下进行(假设您使用 Visual Studio)
After unpacking add the library include directories to your projects' settings (Project -> Properties -> C/C++ -> Additional Include Directories)
Do the same thing for the Libraries Directory (Project -> Properties -> Linker -> Additional Library Directories)
Specify the name of the library in your Linker Input: Project -> Properties -> Linker -> Input -> Additional Dependencies
解压后将库包含目录添加到您的项目设置(项目 -> 属性 -> C/C++ -> 附加包含目录)
对库目录做同样的事情(项目 -> 属性 -> 链接器 -> 附加库目录)
在链接器输入中指定库的名称:项目 -> 属性 -> 链接器 -> 输入 -> 附加依赖项
After this hopefully should compile.
在此之后希望应该编译。
I don't recommend adding the directories above to the Global settings in Visual Studio (Tools -> Options -> Project and Solutions) since it will create and environment where something compiles on your computer and does NOT compile on another one.
我不建议将上面的目录添加到 Visual Studio 中的全局设置(工具 -> 选项 -> 项目和解决方案),因为它会创建一个环境,其中某些内容在您的计算机上编译而不会在另一台计算机上编译。
Now, the hard way, doing it for a Makefile based build system:
现在,困难的方法是为基于 Makefile 的构建系统执行此操作:
- Unpack your stuff
- Specify the include directory under the -I g++ flag
- Specify the Library directory under the -L g++ flag
- Specify the libraries to use like: -llibrary name (for example: -lxml2 for libxml2.so)
- Specify the static libraries like: library name.a
- 打开你的东西
- 在 -I g++ 标志下指定包含目录
- 在 -L g++ 标志下指定库目录
- 指定要使用的库,例如:-llibrary 名称(例如:-lxml2 表示 libxml2.so)
- 指定静态库,如:library name.a
at the end you should have a command which is ugly and looks like:
最后你应该有一个丑陋的命令,看起来像:
g++ -I/work/my_library/include -L/work/my_library/lib -lmylib my_static.a -o appname_exe MYFILE.CPP
g++ -I/work/my_library/include -L/work/my_library/lib -lmylib my_static.a -o appname_exe MYFILE.CPP
(the line above is not really tested just a general idea)
(上面的行并没有真正经过测试,只是一个大概的想法)
I recommend go, grab a template makefile from somewhere and add in all your stuff.
我建议去,从某个地方获取模板 makefile 并添加所有内容。
回答by jldupont
You must link against a .lib or something equivalent i.e. add the ".lib" to the libraries read by the linker. At least that's how it works under Linux... haven't done Windows so a long while.
您必须链接到 .lib 或等效的东西,即将“.lib”添加到链接器读取的库中。至少它在 Linux 下是这样工作的……好久没有在 Windows 上使用了。
The ".lib" contains symbols to data/functions inside the .dll shared library.
“.lib”包含 .dll 共享库中数据/函数的符号。
回答by Amnon
It depends on the library. For examples, some libraries contain precompiled binaries (e.g. dlls) and others you need to compile them yourself. You'd better see the library's documentation.
这取决于图书馆。例如,某些库包含预编译的二进制文件(例如 dll),而其他库则需要您自己编译。你最好看看图书馆的文档。
Basically, to compileyou should:
基本上,要编译你应该:
(1) have the library's include (.h) file location in the compiler's include path,
(1) 在编译器的包含路径中有库的包含 (.h) 文件位置,
(2) have the library stubs (.lib) location in the linker's library path, and have the linker reference the relevant library file.
(2) 在链接器的库路径中有库存根 (.lib) 位置,并让链接器引用相关的库文件。
In order to runthe program you need to have the shared libraries (dlls) where the loader can see them, for example in your system32 directory.
为了运行该程序,您需要拥有加载程序可以看到它们的共享库 (dll),例如在您的 system32 目录中。
回答by aib
There are two kinds of libraries: static and dynamic (or shared.)
有两种库:静态和动态(或共享)。
Static libraries come in an object format and you link them directly into your application.
静态库采用对象格式,您可以将它们直接链接到您的应用程序中。
Shared or dynamic libraries reside in a seperate file (.dll or .so) which must be present at the time your application is run. They also come with object files you must link against your application, but in this case they contain nothing more than stubs that find and call the runtime binary (the .dll or the .so).
共享或动态库驻留在单独的文件(.dll 或 .so)中,在您的应用程序运行时必须存在该文件。它们还带有您必须针对您的应用程序链接的目标文件,但在这种情况下,它们仅包含用于查找和调用运行时二进制文件(.dll 或 .so)的存根。
In either case, you must have some header files containing the signatures (declarations) of the library functions, else your code won't compile.
无论哪种情况,您都必须有一些包含库函数签名(声明)的头文件,否则您的代码将无法编译。
Some 'libraries' are header-only and you need do nothing more than include them. Some consist of header and source files. In that case you should compile and link the sources against your application just as you would do with a source file you wrote.
一些“库”仅包含标题,您只需要包含它们即可。有些由头文件和源文件组成。在这种情况下,您应该像处理您编写的源文件一样,针对您的应用程序编译和链接源代码。
回答by laura
When you compile, assuming you have the libs and the headers in the same folder as the sources you are compiling, you need to add to your compile line -L . -I . -lpng
. -L tells the linker where to look for the library, -I tells the compiler where to look for the headers and -lpng tells the linker to link with the png library.
编译时,假设库和头文件与正在编译的源文件位于同一文件夹中,则需要将-L . -I . -lpng
. -L 告诉链接器在何处查找库,-I 告诉编译器在何处查找头文件,-lpng 告诉链接器与 png 库链接。
[Edit]
Normal projects would have some sort of hierarchy where the headers are in an /include
folder and the 3rd party libs are in a /libs
folder. In this case, you'd put -I ./include
and -L ./libs
instead of -I .
and -L.
[编辑] 普通项目会有某种层次结构,其中标题位于/include
文件夹中,而第 3 方库位于/libs
文件夹中。在这种情况下,你应该把-I ./include
and-L ./libs
而不是-I .
and-L.
[Edit2] Most projects make use of makefile in order to compile from the command line. You can only compile manually for a small number of files, it gets quite hectic after that
[Edit2] 大多数项目使用 makefile 以便从命令行编译。您只能手动编译少量文件,之后就会变得非常忙碌
回答by sud03r
Also,
you may want to look over Dynamic Loadingsupport in various languages and on various
platforms.
This support is very handy in cases when you want to use a library optionally and you don't want your program to fail in case that library is not available.
此外,
您可能希望查看各种语言和各种平台上的动态加载支持。
如果您想可选地使用库并且不希望程序在库不可用时失败,则此支持非常方便。