如何使用GCC编译C库? [英] How to compile library on C using GCC?
问题描述
我制作了一个包含文件 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
然后您可以传递 然后在目录 等等等等。你真的想学习如何要使用 您可能想要将 请阅读程序库Howto 。 I made a library with the files Then I made another library. This library has the files and in the file as it has to be. But when I try to compile the file I tried to compile it with 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 Then you often want to be able to debug your code, so also pass So compile your first library, assuming its source files At this point, you should use a Then you can pass Then compile your second library by having your etc etc. You really want to learn how to use You may want to pass Read also the Program Library Howto. 这篇关于如何使用GCC编译C库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! -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
的示例;你会发现许多其他的!
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.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"
pila_funciones_extra.c
I put the next line:#include "pila_funciones_extra.h"
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.gcc pila_funciones_extra.c -c
and gcc pila_funciones_extra.c -c pila.o
but it doesn't work.-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.-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.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
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
-L../FirstLib -lfirst
as the last options to the gcc
command compiling and linking your program using your libfirst.a
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
make
and write your own Makefile
-s so take time to read the GNU make documentation.-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!