[英]Linking a program with two static libraries each containing functions that depend on functions in the other?
I'm actually trying to call a function a
(stocked in liba.a) from another function b
(stocked in libb.a) but I have a linkage error. 我实际上正在尝试从另一个函数
b
(存储在libb.a中)调用一个函数a
(存储在liba.a中),但是我遇到了链接错误。
There are my files: 有我的文件:
ac AC
#include <stdio.h>
void a()
{
printf("a\n");
}
bc 公元前
void a();
void b()
{
a();
}
main.c main.c中
void b();
int main(int argc, char const* argv[])
{
b();
return (0);
}
And how I compiled 以及我如何编译
gcc -c a.c
gcc -c b.c
ar rc liba.a a.o
ar rc libb.a b.o
ranlib liba.a
ranlib libb.a
gcc main.c liba.a libb.a
(the exact message error) (确切的消息错误)
b.c:(.text+0xa): undefined reference to `a'
Is there a way to do what I'm trying to do? 有没有办法做我想做的事? Thanks for your time
谢谢你的时间
EDIT: It appears that library order in the command line matters. 编辑:看来在命令行中的库顺序很重要。 But in my real problem libA depends on libB and libB depends on libA (which the MCVE code doesn't demonstrate).
但是在我真正的问题中,libA依赖于libB,而libB依赖于libA(MCVE代码未演示)。
EDIT: It appears that library order in the command line matters.
编辑:看来在命令行中的库顺序很重要。 But in my real problem libA depends on libB and libB depends on libA (which the MCVE code doesn't demonstrate).
但是在我真正的问题中,libA依赖于libB,而libB依赖于libA(MCVE代码未演示)。
Your real problem, then, is you've painted yourself in a corner. 那么,您真正的问题是您将自己描绘在一个角落。 The right thing to do is arrange the two libraries so that one is entirely independent of the other, and put that one after the other in the link command.
正确的做法是安排两个库,以使一个库完全独立于另一个库,然后在link命令中将一个库放在另一个库之后。
Without making that change, you might get away with mentioning the first library twice, 如果不进行更改,您可能会两次提到第一个库,
gcc main.c liba.a libb.a liba.a
The linker is pretty simple-minded when it comes to name resolution. 在名称解析方面,链接器非常简单。 Starting with a list of unresolved names, it marches through the list of libraries, resolving names as it finds them.
从未解析名称列表开始,它遍历库列表,在找到名称时解析名称。 It won't notice that you give it liba.a twice;
它不会注意到您两次给它liba.a。 it will resolve the
a
names supplied by b
, and then the b
names supplied by a
. 这既解决了
a
通过提供的名称b
,然后b
通过提供的名称a
。
But that trick is likely to bite you one day (if it works at all). 但是,这一技巧很可能会在一天之内咬住您(如果可以的话)。 Best, as I said, is to sort out your dependencies and simplify things for yourself and those who follow you.
正如我所说,最好的办法是理清您的依赖关系,并为您自己和跟随您的人简化事情。
Create the following Makefile
in the same directory that you already have ac
, bc
and main.c
: 在已经具有
ac
, bc
和main.c
目录中创建以下Makefile
:
#!/usr/bin/make
ar:
gcc -o a.o -c a.c
gcc -o b.o -c b.c
gcc -o m.o -c main.c
ar rc liba.a a.o
ar rc libb.a b.o
ar rc libm.a m.o
ranlib liba.a
ranlib libb.a
ranlib libm.a
gcc -o main m.o a.o b.o
install:
gcc -o a.o -c a.c
gcc -o b.o -c b.c
gcc -o m.o -c main.c
gcc -o main m.o a.o b.o
clean:
rm -f *.o
rm -f *.a
rm -f main
Basically, your question is an example of multiple files development in C. 基本上,您的问题是使用C开发多个文件的示例。
make install
command, the Makefile
will compile your program using standard gcc compilation process. make install
命令, Makefile
将使用标准gcc编译过程来编译程序。 make ar
, the compilation will be ar
packaging compliance. make ar
,则编译将符合ar
包装规范。 make clean
, the Makefile
will clean your previous build make clean
, Makefile
将清除您以前的版本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.