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?
- 把。h文件放在哪里?
- Where to put the .dll files?
- 在哪里放置.dll文件?
- How to compile?
- 如何编译?
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编译器选项告诉编译器在哪里找到它们,是这样吗?)
7 个解决方案
#1
6
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)
解压后,将库包含目录添加到项目的设置中(项目->属性-> C/ c++ ->附加目录)
-
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
在链接器输入中指定库的名称:Project ->属性->链接器->输入->附加依赖项
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
- 指定-I g++标志下的include目录
- Specify the Library directory under the -L g++ flag
- 在-L g++标志下指定库目录
- Specify the libraries to use like: -llibrary name (for example: -lxml2 for libxml2.so)
- 指定要使用的库:-llibrary名称(例如:-lxml2用于libxml2.so)
- Specify the static libraries like: library name.a
- 指定静态库,如: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/包含-L/work/my_library/lib -lmylib my_static。一个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模板,然后添加所有的东西。
#2
8
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 GCC you can use the-I
command line to feed in directories to be searched for header files.第一步编译所有的源文件—这将获取您所编写的源文件并将它们转换为所谓的对象文件。在这个阶段,编译器需要知道您在代码中(甚至在外部库中)使用的所有函数的声明,因此您需要使用#include来包含您使用的任何库的头文件。这也意味着您需要告诉编译器这些头文件的位置。使用GCC,您可以使用-I命令行在要搜索的目录中提供头文件。
-
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.下一个阶段是将所有的对象文件连接到一个可执行文件中。在这个阶段,链接器需要解析对外部库的调用。这意味着您需要对象形式的库。大多数的库会给你关于如何生成这个或可能提供它已经构建好的说明。在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.
希望有帮助。
#3
3
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共享库中。
#4
2
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 compile you should:
基本上,要编译你应该:
(1) have the library's include (.h) file location in the compiler's include path,
(1)在编译器的include路径中包含库的include (.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 run the program you need to have the shared libraries (dlls) where the loader can see them, for example in your system32 directory.
为了运行程序,您需要拥有共享库(dll),以便加载程序可以在其中看到它们,例如在system32目录中。
#5
1
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.
有些“库”是只包含头的,您只需要包含它们就可以了。有些由头文件和源文件组成。在这种情况下,您应该对应用程序编译并链接源文件,就像对所编写的源文件一样。
#6
1
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.
在编译时,假设您拥有与正在编译的源文件相同的文件夹中的lib和header,那么您需要将它们添加到您的编译行-L中。-我。无时无刻的。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文件夹中,第三方的libs在/libs文件夹中。在这种情况下,你可以用-I ./include和-L ./libs代替-I。和- 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
大多数项目使用makefile从命令行编译。您只能手工编译少量的文件,之后就会变得非常繁忙
#7
0
Also,
you may want to look over Dynamic Loading support 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.
此外,您可能还需要查看不同语言和不同平台上的动态加载支持。当您需要选择性地使用库,并且在库不可用时不希望程序失败时,这种支持非常有用。
#1
6
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)
解压后,将库包含目录添加到项目的设置中(项目->属性-> C/ c++ ->附加目录)
-
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
在链接器输入中指定库的名称:Project ->属性->链接器->输入->附加依赖项
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
- 指定-I g++标志下的include目录
- Specify the Library directory under the -L g++ flag
- 在-L g++标志下指定库目录
- Specify the libraries to use like: -llibrary name (for example: -lxml2 for libxml2.so)
- 指定要使用的库:-llibrary名称(例如:-lxml2用于libxml2.so)
- Specify the static libraries like: library name.a
- 指定静态库,如: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/包含-L/work/my_library/lib -lmylib my_static。一个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模板,然后添加所有的东西。
#2
8
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 GCC you can use the-I
command line to feed in directories to be searched for header files.第一步编译所有的源文件—这将获取您所编写的源文件并将它们转换为所谓的对象文件。在这个阶段,编译器需要知道您在代码中(甚至在外部库中)使用的所有函数的声明,因此您需要使用#include来包含您使用的任何库的头文件。这也意味着您需要告诉编译器这些头文件的位置。使用GCC,您可以使用-I命令行在要搜索的目录中提供头文件。
-
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.下一个阶段是将所有的对象文件连接到一个可执行文件中。在这个阶段,链接器需要解析对外部库的调用。这意味着您需要对象形式的库。大多数的库会给你关于如何生成这个或可能提供它已经构建好的说明。在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.
希望有帮助。
#3
3
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共享库中。
#4
2
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 compile you should:
基本上,要编译你应该:
(1) have the library's include (.h) file location in the compiler's include path,
(1)在编译器的include路径中包含库的include (.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 run the program you need to have the shared libraries (dlls) where the loader can see them, for example in your system32 directory.
为了运行程序,您需要拥有共享库(dll),以便加载程序可以在其中看到它们,例如在system32目录中。
#5
1
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.
有些“库”是只包含头的,您只需要包含它们就可以了。有些由头文件和源文件组成。在这种情况下,您应该对应用程序编译并链接源文件,就像对所编写的源文件一样。
#6
1
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.
在编译时,假设您拥有与正在编译的源文件相同的文件夹中的lib和header,那么您需要将它们添加到您的编译行-L中。-我。无时无刻的。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文件夹中,第三方的libs在/libs文件夹中。在这种情况下,你可以用-I ./include和-L ./libs代替-I。和- 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
大多数项目使用makefile从命令行编译。您只能手工编译少量的文件,之后就会变得非常繁忙
#7
0
Also,
you may want to look over Dynamic Loading support 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.
此外,您可能还需要查看不同语言和不同平台上的动态加载支持。当您需要选择性地使用库,并且在库不可用时不希望程序失败时,这种支持非常有用。