简体   繁体   English

如何将我的 main.c 文件与我的 library.a 文件链接?

[英]How to link my main.c file with my library .a file?

I am trying to make a static library that belongs to me.我正在尝试制作一个属于我的 static 库。 Suppose there are two files: stack.h stores the declarations which I want to store it in the ./include document, and stack.c implements the definition, and main.c call the functions in the stack.h .假设有两个文件: stack.h存储我要存储在./include文档中的声明, stack.c实现定义, main.c调用stack.h中的函数。

Make stack.a file which I want to store it in ./lib with those instructions:使用以下说明制作我想将其存储在./lib中的stack.a文件:

>> gcc -c stack.c
>> ar -cq libstack.a stack.o
>> mv stack.h include
>> mv libstack.a lib

If I want to link the main.c with my libstack.a , what I am going to do with gcc ?如果我想将main.c与我的libstack.a链接,我将如何处理gcc Do I need to put this lib into the PATH?我是否需要将此库放入 PATH 中? If so, how to do it?如果是这样,该怎么做? If next step, I want to do my dynamic library .so file, can anyone give some advice?如果下一步,我想做我的动态库.so文件,有人可以给点建议吗?

C Libraries C 库

In general, libraries are created from many library source files and are either built as archive files (libmine.a) that are statically linked into executables that use them, or as shared object files (libmine.so) that are dynamically linked into executables that use them.通常,库是从许多库源文件创建的,或者构建为静态链接到使用它们的可执行文件的存档文件 (libmine.a),或者构建为动态链接到可执行文件的共享 object 文件 (libmine.so)使用它们。 To link in libraries of these types, use the gcc command line options -L for the path to the library files and -l to link in a library (a.so or aa):要链接这些类型的库,请使用gcc命令行选项-L作为库文件的路径,使用-l链接库(a.so 或 aa):

    -L{path to file containing library} -l${library name}

For example, if I have a library named libmine.so in /home/newhall/lib/ then I'd do the following to link it into my program:例如,如果我在/home/newhall/lib/中有一个名为 libmine.so 的库,那么我将执行以下操作将其链接到我的程序中:

$ gcc -o myprog myprog.c  -L/home/newhall/lib -lmine

You may also need to specify and include path so the compiler can find the library header file: -I /home/newhall/include您可能还需要指定和包含路径,以便编译器可以找到库 header 文件: -I /home/newhall/include

If you create your own shared object files and do not install them in /usr/lib , then you need to set your LD_LIBRARY_PATH environment variable so that the runtime linker can find them and load them at run time.如果您创建自己的共享 object 文件并且不将它们安装在/usr/lib中,则需要设置 LD_LIBRARY_PATH 环境变量,以便运行时 linker 可以找到它们并在运行时加载它们。

For example, if I put my.so files in a directory named lib in my home directory, I'd set my LD_LIBRARY_PATH enviroment to the following:例如,如果我将 my.so 文件放在我的home目录中名为lib的目录中,我会将我的LD_LIBRARY_PATH环境设置为以下内容:

# if running bash: # 如果运行 bash:

>> export LD_LIBRARY_PATH=/home/newhall/lib:$LD_LIBRARY_PATH

# if running tcsh: # 如果运行 tcsh:

>> setenv LD_LIBRARY_PATH /home/newhall/lib:$LD_LIBRARY_PATH

USING AND LINKING LIBRARY CODE使用和链接库代码

To use a Library that is not linked into your program automatically by the compiler, you need to要使用未由编译器自动链接到程序的库,您需要

    1. include the library's header file in your C source file ( test.c in the example below)在 C 源文件中包含库的 header 文件(下面示例中的test.c
    1. tell the compiler to link in the code from the library .o file into your executable file:告诉编译器将库.o文件中的代码链接到可执行文件中:

    step 1: Add an include line (#include "somelib.h") in a program source file (eg, test.c).第 1 步:在程序源文件(例如 test.c)中添加一个包含行(#include "somelib.h")。

    step 2: Link the program's.c file with the library object file (ie specify the somelib.o file as a command line argument to gcc):第 2 步:将程序的 .c 文件与库 object 文件链接(即指定 somelib.o 文件作为 gcc 的命令行参数):

     >> gcc -o myprog test.c somelib.o

    The resulting executable file ( myprog ) will contain machine code for all the functions defined in test.c plus any mylib library functions that are called by.生成的可执行文件 ( myprog ) 将包含 test.c 中定义的所有函数的机器代码以及由其调用的任何 mylib 库函数。

CREATING AND USING YOUR OWN LIBRARY CODE创建和使用您自己的库代码

To create a Library of code you need to do the following:要创建代码库,您需要执行以下操作:

(1) Create an INTERFACE to your library: mylib.h (1) 为您的库创建一个接口:mylib.h

(2) Create an IMPLEMENTATION of your library: mylib.c (2) 创建库的实现:mylib.c

(3) Create a LIBRARY OBJECT FILE (.o) that can be linked with programs that use your library (3) 创建一个 LIBRARY OBJECT FILE (.o) 可以与使用你的库的程序链接

  • 3a. 3a。 or create a SHARED OBJECT FILE (.so) from many.o files that can be dynamically linked with programs that use your library或从 many.o 文件创建一个 SHARED OBJECT 文件 (.so),该文件可以与使用您的库的程序动态链接

  • 3b. 3b。 or create an ARCHIVE FILE (.a) from many.o files that can be statically linked with programs that use your library或从 many.o 文件创建一个 ARCHIVE FILE (.a),该文件可以与使用您的库的程序静态链接

(4) USE the library in other C code: (a) #include "mylib.h" (b) link in the libary code into a.out file (4) 使用其他 C 代码中的库: (a) #include "mylib.h" (b) 将库代码中的链接链接到 a.out 文件中

(5) Set LD_LIBRARY_PATH environment variable for finding shared objects in non-standard locations at runtime (5) 设置 LD_LIBRARY_PATH 环境变量,用于在运行时在非标准位置查找共享对象

Details:细节:

(1) INTERFACE: the header file to your library should contain definitions for everything exported by your library: (1) 接口:到您的库的 header 文件应包含您的库导出的所有内容的定义:

  • function prototypes with comments for users of your library functions function 原型,为您的库函数的用户提供注释

  • definitions for types and global variables exported by your library库导出的类型和全局变量的定义

You should have "boiler plate" code (#ifndef... #endif) around the header file's contents, to ensures that the preprocessor only includes the mylib.h file one time.您应该在 header 文件的内容周围有“样板”代码(#ifndef...#endif),以确保预处理器只包含一次 mylib.h 文件。

Here is what an example.h file might look like:下面是 example.h 文件的样子:

#ifndef MYLIB_H_                // _MYLIB_H_ is not allowed.
#define MYLIB_H_

    // a constant definition exported by library:
    #define MAX_FOO  20

    // a type definition exported by library:
        struct foo_struct {  
        int x;
        float y;
    };
    typedef struct foo_struct foo_struct;

    // a global variable exported by library
    // "extern" means that this is not a variable declaration, it 
    // just defines that a variable named total_foo of type int
    // exits and you can use it (its declaration is in some library source file)
    extern int total_foo;   

    // a function prototype for a function exported by library:
    extern int foo(float y, float z);   // a very bad function name

#endif

(2) IMPLEMENTATION: create a mylib.c file that #includes "mylib.h" and contains the implementation of every function in your library. (2) 实现:创建一个 mylib.c 文件,其中 #includes "mylib.h" 并包含库中每个 function 的实现。

    #include "mylib.h"

    ...
    int total_foo;

    int foo(float y, float z) { 
    ...
    } 

(3) create a LIBRARY OBJECT FILE that can be linked into other programs that use your library (use the -c option to gcc to tell it just to create an object file (ao file) rather than an executable: (3) 创建一个库 OBJECT 文件,该文件可以链接到使用您的库的其他程序(使用 gcc 的 -c 选项告诉它只是创建一个 ZA8CFDE6331BD59EB2AC96F8911C4B6 文件66Z文件)而不是可执行文件:

>> gcc -o mylib.o -c mylib.c

you can then use the mylib.o file as the "library file" and statically link it into other programs that use it, or...然后,您可以将 mylib.o 文件用作“库文件”并将其静态链接到使用它的其他程序,或者...

  • (3a) alternately, you can create a SHARED OBJECT FILE from one or more.o files that can be linked into other programs that use your library A shared object file is the Unix name for a dynamically linked library whose code is loaded into the a.out file at runtime. (3a) 或者,您可以从一个或多个.o 文件创建一个共享 OBJECT 文件,该文件可以链接到使用您的库的其他程序 共享 object 文件是 Unix 的名称,其代码被动态加载到名称中.out 文件在运行时。 To create a.so file use the -shared flag to gcc.要创建 a.so 文件,请使用 gcc 的 -shared 标志。 Here is what an example build might look like:以下是示例构建的样子:
     >> gcc -shared -o libmylib.so mylib.o blah.o grr.o -lm
  • (3b) you could also build an ARCHIVE FILE (a statically linked library, libmylib.a) from one or more.o files. (3b) 您还可以从一个或多个.o 文件构建 ARCHIVE FILE(静态链接库 libmylib.a)。 If you link with a static library, its code is copied into the a.out file at runtime.如果您与 static 库链接,则其代码会在运行时复制到 a.out 文件中。 See gcc documentation for more information on how to build.a and.so files.有关如何构建.a 和.so 文件的更多信息,请参阅 gcc 文档。

(4) USE the library in other programs: (4) 在其他程序中使用该库:

  • step 1: Add an include line (#include "mylib.h") in all program source files that use library definitions (eg, test.c).第 1 步:在所有使用库定义的程序源文件(例如,test.c)中添加一个包含行(#include "mylib.h")。

  • step 2: Link the program's.c file with the library object file (ie specify the mylib.o file as a command line argument to gcc):第 2 步:将程序的.c 文件与库 object 文件链接(即指定 mylib.o 文件作为 gcc 的命令行参数):

     gcc test.c mylib.o OR to link in libmylib.so (or libmylib.a): gcc test.c -lmylib OR to link with a library not in the standard path: gcc test.c -L/home/newhall/lib -lmylib The resulting a.out out will contain machine code for all the functions defined in test.c plus any mylib library functions that are called by the test.c code.

(5) RUNNING an executable linked with a shared object file: (5) 运行与共享 object 文件链接的可执行文件:

If the shared object file in not in /usr/lib, then you need to set your LD_LIBRARY_PATH environment variable so that the runtime linker can find and load your.so file into the executable at runtime:如果共享的 object 文件不在 /usr/lib 中,则需要设置 LD_LIBRARY_PATH 环境变量,以便运行时 linker 可以在运行时找到您的.so 文件并将其加载到可执行文件中:

in bash:在 bash 中:

>> export LD_LIBRARY_PATH=/home/newhall/lib:$LD_LIBRARY_PATH

in tcsh:在 tcsh 中:

>> setenv LD_LIBRARY_PATH /home/newhall/lib:$LD_LIBRARY_PATH

there is another useful link: demo for making libraries还有另一个有用的链接: 制作库的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM