链接器的困境 - 未定义的参考 [英] linker woes - undefined reference

查看:113
本文介绍了链接器的困境 - 未定义的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编译器遇到了一个问题,告诉我有一个'未定义的引用',我想在库中使用该函数。让我分享一些关于这个问题的信息:


  • 我使用gcc为C进行交叉编译。

  • 我正在调用一个库函数,通过包含另一个包含原型的头文件来访问它。

  • 我使用-I包含头文件目录,我确信它是被发现的。

  • 我首先创建.o文件,然后通过单独的命令将它们链接在一起。



所以我认为这可能是我包含库文件的顺序,但我不确定订购它们的正确方式是什么。我尝试在.o文件之前和之后包含头文件夹。



有些建议很棒,也许可以解释链接程序如何执行它的功能。 / p>

谢谢!




回应回应




  • 库中不存在.a库文件,只是.h和.c在库中,所以-l不适用

  • 我对库文件的理解是,它只是头文件和源文件的集合,但也许是从源文件创建的.o文件的集合!

  • 没有创建库对象文件,也许应该有?是的,似乎我不明白包含和库之间的区别...我会努力: - )



所有的回应!我从图书馆学到了很多东西。我想把所有的回答作为接受的答案: - )

解决方案

这听起来像你没有编译。 c文件生成.o文件。链接器会在通过编译库生成的.o文件中查找原型的实现。



您的构建过程是否编译库.c文件?



如果它实际上只是源代码,为什么称它为库?


I'm having a problem with my compiler telling me there is an 'undefined reference to' a function I want to use in a library. Let me share some info on the problem:

  • I'm cross compiling with gcc for C.
  • I am calling a library function which is accessed through an included header which includes another header, which contains the prototype.
  • I have included the headers directory using -I and i'm sure it's being found.
  • I'm first creating the .o files then linking them in a separate command.

So my thought is it might be the order in which I include the library files, but i'm not sure what is the correct way to order them. I tried with including the headers folder both before and after the .o file.

Some suggests would be great, and maybe and explanation of how the linker does its thing.

Thanks!


Response to answers

  • there is no .a library file, just .h and .c in the library, so -l isn't appropriate
  • my understanding of a library file is that it is just a collection of header and source files, but maybe it's a collection of .o files created from the source?!
  • there is no library object file being created, maybe there should be?? Yes seems I don't understand the difference between includes and libraries...i'll work on that :-)

Thanks for all the responses! I learned a lot about libraries. I'd like to put all the responses as the accepted answer :-)

解决方案

It sounds like you are not compiling the .c file in the library to produce a .o file. The linker would look for the prototype's implementation in the .o file produced by compiling the library

Does your build process compile the library .c file?

Why do you call it a "library" if it's actually just source code?

这篇关于链接器的困境 - 未定义的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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