简体   繁体   English

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

[英]How to compile library on C using GCC?

I made a library with the files pila.h and pila.c . 我用文件pila.hpila.c了一个库。 I compile the file pila.c with gcc pila.c -c and this library works fine. 我使用gcc pila.c -c编译文件pila.c ,这个库工作正常。 I have tested it. 我测试了它。

Then I made another library. 然后我又建了一个图书馆。 This library has the files pila_funciones_extra.h and pila_funciones_extra.c . 该库包含文件pila_funciones_extra.hpila_funciones_extra.c In this library I need to include the first library. 在这个库中,我需要包含第一个库。 In the file pila_funciones_extra.h I put the next line to include it: 在文件pila_funciones_extra.h我把下一行包含在内:

#include "pila.h"

and in the file pila_funciones_extra.c I 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.c the compiler doensn't recognize the inclusion of the library pila . 但是当我尝试编译文件pila_funciones_extra.c ,编译器不会识别包含库pila It says that the functions, structures, constants and macros that are defined in the library pila haven't been defined. 它表示库pila中定义的函数,结构,常量和宏尚未定义。

I tried to compile it with gcc pila_funciones_extra.c -c and gcc pila_funciones_extra.c -c pila.o but 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? 谁能告诉我正确的编译方法?

First, always take the habit to compile with -Wall (and perhaps even also -Wextra to get even more warnings) option to gcc ; 首先,总是习惯用-Wall (甚至可能还有 -Wextra来获得更多警告)选项来gcc ; it gives you almost all warnings, and you should improve your code till no warnings are given. 它几乎给你所有的警告,你应该改进你的代码,直到没有给出警告。

Then you often want to be able to debug your code, so also pass -g to gcc . 然后你经常希望能够调试你的代码,所以也把-g传递给gcc Once you are confident with your code you could ask gcc to produce optimized machine code with -O2 . 一旦您对代码有信心,就可以要求gcc使用-O2生成优化的机器代码。 Learn to use the gdb debugger. 学习使用gdb调试器。

So compile your first library, assuming its source files first1.c and first2.c are in FirstLib/ directory, with eg 所以编译你的第一个库,假设它的源文件first1.cfirst2.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 Makefile and learn how to use make , in particular because you want to get your libfirst.a with 此时,你应该使用Makefile并学习如何使用make ,特别是因为你想得到你的libfirst.a

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

Then you can pass -L../FirstLib -lfirst as the last options to the gcc command compiling and linking your program using your libfirst.a 然后你可以传递-L../FirstLib -lfirst作为gcc命令的最后一个选项,使用libfirst.a编译和链接你的程序

Then compile your second library by having your Makefile in directory SecondLib/ which very probably should contain 然后通过将Makefile放在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 make and write your own Makefile -s so take time to read the GNU make documentation . 等你真的想学习如何使用make并编写自己的Makefile -s,所以花点时间阅读GNU make文档

You may want to pass -H to gcc to 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 ). 您可能希望将-H传递给gcc以让它告诉您所有包含的文件,并且您可能还想使用重制 (另外和替换make )来调试更复杂的Makefile -s(特别是通过运行remake -x )。 Here is an example of Makefile ; Makefile一个例子; you'll find many others! 你会找到很多其他人!

Read also the Program Library Howto . 另请阅读程序库Howto

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). 如果是,则更新.o文件(或添加到此库中)。 The ranlib is used to randomize the library in a way that is useful for the loader. ranlib用于以对加载器有用的方式随机化库。

When you use this library, you do: 使用此库时,您可以:

gcc -o myapp myapp.c mylib.a

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

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