C语言 如何使用 GCC 在 C 上编译库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14919366/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:22:11  来源:igfitidea点击:

How to compile library on C using GCC?

cgcccompilationincludelibraries

提问by Adrian

I made a library with the files pila.hand pila.c. I compile the file pila.cwith gcc pila.c -cand this library works fine. I have tested it.

我用文件pila.hpila.c. 我编译文件pila.cgcc pila.c -c这个库工作正常。我已经测试过了。

Then I made another library. This library has the files pila_funciones_extra.hand pila_funciones_extra.c. In this library I need to include the first library. In the file pila_funciones_extra.hI put the next line to include it:

然后我做了另一个图书馆。这个库有文件pila_funciones_extra.hpila_funciones_extra.c. 在这个库中,我需要包含第一个库。在文件中,pila_funciones_extra.h我将下一行包含在内:

#include "pila.h"

and in the file pila_funciones_extra.cI put the next line:

在文件中pila_funciones_extra.c我放了下一行:

#include "pila_funciones_extra.h"

as it has to be.

必须如此。

But when I try to compile the file pila_funciones_extra.cthe compiler doensn't recognize the inclusion of the library pila. It says that the functions, structures, constants and macros that are defined in the library pilahaven't been defined.

但是当我尝试编译该文件时pila_funciones_extra.c,编译器无法识别包含 library pila。它表示库中定义的函数、结构、常量和宏pila尚未定义。

I tried to compile it with gcc pila_funciones_extra.c -cand gcc pila_funciones_extra.c -c pila.obut it doesn't work.

我试图编译它gcc pila_funciones_extra.c -cgcc pila_funciones_extra.c -c pila.o,但它不工作。

I made sure that all the files are in the same folder.

我确保所有文件都在同一个文件夹中。

I'm working on Ubuntu.

我在 Ubuntu 上工作。

Can anyone tell me the right way to compile it?

谁能告诉我编译它的正确方法?

回答by Basile Starynkevitch

First, always take the habit to compile with -Wall(and perhaps even also-Wextrato get even more warnings) option to gcc; it gives you almost all warnings, and you should improve your code till no warnings are given.

首先,始终养成使用-Wall(甚至可能还会-Wextra得到更多警告)选项编译的习惯gcc;它给你几乎所有的警告,你应该改进你的代码直到没有警告。

Then you often want to be able to debug your code, so also pass -gto gcc. Once you are confident with your code you could ask gccto produce optimized machine code with -O2. Learn to use the gdbdebugger.

然后你经常希望能够调试你的代码,所以也传递-ggcc. 一旦您对自己的代码充满信心,您就可以要求gcc使用-O2. 学习使用gdb调试器。

So compile your first library, assuming its source files first1.cand first2.care in FirstLib/directory, with e.g.

所以编译第一个库,假设它的源文件first1.c,并first2.cFirstLib/目录中,如与

cd FirstLib/
gcc -Wall -g -c first1.c -o first1.o
gcc -Wall -g -c first2.c -o first2.o

At this point, you should use a Makefileand learn how to use make, in particular because you want to get your libfirst.awith

在这一点上,你应该使用 aMakefile并学习如何使用make,特别是因为你想得到你libfirst.a

ar ruv libfirst.a first1.o first2.o
ranlib libfirst.a

Then you can pass -L../FirstLib -lfirstas the last options to the gcccommand compiling and linking your program using your libfirst.a

然后,您可以将-L../FirstLib -lfirst最后一个选项传递给gcc使用您的程序编译和链接程序的命令libfirst.a

Then compile your second library by having your Makefilein directory SecondLib/which very probably should contain

然后通过将您的Makefilein 目录编译您的第二个库,该目录SecondLib/很可能应该包含

# in SecondLib/Makefile
CC=gcc
CFLAGS= -I../FirstLib/ -Wall -g
## add your other stuff for make

etc etc. You really want to learn how to use makeand write your own Makefile-s so take time to read the GNU make documentation.

等等等等。你真的想学习如何使用make和编写你自己的Makefile-s 所以花时间阅读GNU make 文档

You may want to pass -Hto gccto have it tell you all the included files, and you may want to also use remake(in addition & replacement of make) to debug your more complex Makefile-s (notably by running remake -x). Hereis an example of Makefile; you'll find many others!

您可能希望传递-Hgcc它以告诉您所有包含的文件,并且您可能还希望使用remake(除了 & 替换 make)来调试更复杂的Makefile-s(特别是通过运行remake -x)。是一个例子Makefile; 你会发现很多其他的!

Read also the Program Library Howto.

另请阅读程序库方法

回答by kdmin

For a library composed of many files you can first compile then separately and then do this:

对于由许多文件组成的库,您可以先编译然后单独编译,然后执行以下操作:

ar ruv mylib.a pila_funciones_extra.o pila.o
ranlib mylib.a

This command causes the library to be created if it does not already exist. If it does, the .o files are updated (or added to this library). The ranlibis used to randomize the library in a way that is useful for the loader.

如果库不存在,此命令会导致创建库。如果是,则更新 .o 文件(或添加到此库中)。该ranlib的是用于随机库的方式,是装载机有用。

When you use this library, you do:

当你使用这个库时,你会:

gcc -o myapp myapp.c mylib.a