如何使用GCC编译C库? [英] How to compile library on C using GCC?

查看:123
本文介绍了如何使用GCC编译C库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个包含文件 pila.h pila.c 的库。我用 gcc pila.c -c 编译文件 pila.c ,这个库工作正常。我已经测试过了。

然后我又创建了另一个库。

这个库有文件 pila_funciones_extra.h pila_funciones_extra.c 。在这个库中我需要包含第一个库。在文件 pila_funciones_extra.h 中,我把下一行包括在内:

  #includepila.h

和文件 pila_funciones_extra。

  #includepila_funciones_extra.h

必须是。



但是当我尝试编译文件 pila_funciones_extra.c 编译器不会识别包含库 pila 。它说库中 pila 中定义的函数,结构,常量和宏没有被定义。



我试着用 gcc pila_funciones_extra.c -c gcc pila_funciones_extra.c -c pila.o 编译它,但它不起作用。

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



我正在研究Ubuntu。



任何人都可以告诉我编译它的正确方法吗?

解决首先,习惯用 -Wall (也许甚至是也是 -Wextra 以获得更多警告)选项 gcc ;它给你几乎所有的警告,你应该改善你的代码,直到没有警告。



然后你经常希望能够调试你的代码, code> -g gcc 。一旦您对自己的代码充满信心,您可以使用 -O2 来请求 gcc 产生优化的机器码。学习使用 gdb 调试器。



因此,编译你的第一个库,假设它的源文件 first1.c first2.c 位于 FirstLib / 目录中,例如

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

此时,您应该使用a Makefile 并学习如何使用 make ,特别是因为您想要获取 libfirst .a with

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

然后您可以传递 -L ../ FirstLib -lfirst 作为 gcc 命令的最后一个选项,使用 libfirst.a

然后在目录 SecondLib / 中编译你的第二个库,使得 Makefile code>很可能应该包含

 #在SecondLib / Makefile中$#b $ CC = gcc 
CFLAGS = -I ../ FirstLib / -Wall -g
##添加你的其他东西,让

等等等等。你真的想学习如何要使用 make 并编写你自己的 Makefile -s,所以花些时间阅读 GNU make documentation

您可能想要将 -H gcc 来让它告诉你所有包含的文件,你也可以使用翻拍(另外&替换 make )来调试更复杂的 Makefile -s(特别是通过运行 remake - X )。 这里 Makefile 的示例;你会发现许多其他的!



请阅读程序库Howto


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

Then I made another library. This library has the files pila_funciones_extra.h and pila_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:

#include "pila.h"

and in the file pila_funciones_extra.c I put the next line:

#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. It says that the functions, structures, constants and macros that are defined in the library pila haven't been defined.

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.

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

I'm working on 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; 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. Once you are confident with your code you could ask gcc to produce optimized machine code with -O2. Learn to use the gdb debugger.

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

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

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

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

# 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.

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). Here is an example of Makefile; you'll find many others!

Read also the Program Library Howto.

这篇关于如何使用GCC编译C库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆