I am brand new to C programming (but not programming) and am trying to understand how libraries and header files work together, particularly with respect to packaging and distribution.
我是C编程的新手(但不是编程),我试图理解库和头文件如何协同工作,特别是在打包和分发方面。
After reading this excellent question and its answer, I understand that the header file(s) act as the API to a library, exposing capabilities to the outside world; and that the library itself is the implementation of those capabilities.
在阅读了这个优秀的问题及其答案后,我理解头文件充当了库的API,向外界展示了功能;并且库本身就是这些功能的实现。
However one thing that I cannot seem to find a good explanation of is: how are header files packaged into or distributed with the libraries?
然而,我似乎无法找到一个很好的解释的一件事是:如何将头文件打包到库中或与库一起分发?
- Are the libs and their headers packaged into an archive (zip, tarball, etc.)?
- libs和它们的头文件是否打包成一个档案(zip,tarball等)?
- Are headers compiled into libs and distributed alongside them?
- 标题是否编译成lib并与它们一起分发?
When I do a #include "mylib.h"
, how does the linker know where to find:
当我做#include“mylib.h”时,链接器如何知道在哪里找到:
- the header file itself,
mylib.h
- 头文件本身,mylib.h
- the library implementing the functions declared in
mylib.h
. - 实现mylib.h中声明的函数的库。
1 个解决方案
#1
2
how does the linker know where to find: (1) the header file itself,
mylib.h
链接器如何知道在哪里找到:(1)头文件本身,mylib.h
- With a notation like
#include <mylib.h>
, it searches the header file in the system defined include PATH. -
使用#include
之类的表示法,它会搜索系统定义的包含PATH的头文件。 - With a notation like
#include "mylib.h"
, it searches the header file in the system defined include PATH and in the current directory. - 使用#include“mylib.h”这样的表示法,它会搜索系统定义的包含PATH和当前目录中的头文件。
if the header file is present in some other hierarchy, you can provide the path to get the header file with -I
option with gcc
.
如果头文件存在于某个其他层次结构中,则可以提供使用gcc获取带-I选项的头文件的路径。
(2) the library implementing mylib.h?
(2)实现mylib.h的库?
You need to provide the path to the library using -L
(in case of non-standard path to the library) and link the library using -l
option.
您需要使用-L(如果是库的非标准路径)提供库的路径,并使用-l选项链接库。
As per the convention, if the (shared) library is named libmylib.so
, you can use -lmylib
to link to that directory.
按照惯例,如果(共享)库名为libmylib.so,则可以使用-lmylib链接到该目录。
For example , consider the pow()
function.
例如,考虑pow()函数。
It is prototyped in math.h
, so in your source file, you need to add #include <math.h>
to get the function declaration.
它是在math.h中的原型,因此在源文件中,需要添加#include
Then, at compile (rather, linking) time, you need to link it with the "math" library using -lm
to get the function definition.
然后,在编译(而不是链接)时,您需要使用-lm将其与“math”库链接以获取函数定义。
#1
2
how does the linker know where to find: (1) the header file itself,
mylib.h
链接器如何知道在哪里找到:(1)头文件本身,mylib.h
- With a notation like
#include <mylib.h>
, it searches the header file in the system defined include PATH. -
使用#include
之类的表示法,它会搜索系统定义的包含PATH的头文件。 - With a notation like
#include "mylib.h"
, it searches the header file in the system defined include PATH and in the current directory. - 使用#include“mylib.h”这样的表示法,它会搜索系统定义的包含PATH和当前目录中的头文件。
if the header file is present in some other hierarchy, you can provide the path to get the header file with -I
option with gcc
.
如果头文件存在于某个其他层次结构中,则可以提供使用gcc获取带-I选项的头文件的路径。
(2) the library implementing mylib.h?
(2)实现mylib.h的库?
You need to provide the path to the library using -L
(in case of non-standard path to the library) and link the library using -l
option.
您需要使用-L(如果是库的非标准路径)提供库的路径,并使用-l选项链接库。
As per the convention, if the (shared) library is named libmylib.so
, you can use -lmylib
to link to that directory.
按照惯例,如果(共享)库名为libmylib.so,则可以使用-lmylib链接到该目录。
For example , consider the pow()
function.
例如,考虑pow()函数。
It is prototyped in math.h
, so in your source file, you need to add #include <math.h>
to get the function declaration.
它是在math.h中的原型,因此在源文件中,需要添加#include
Then, at compile (rather, linking) time, you need to link it with the "math" library using -lm
to get the function definition.
然后,在编译(而不是链接)时,您需要使用-lm将其与“math”库链接以获取函数定义。