试图包含一个库,但不断收到“未定义的引用"消息 [英] Trying to include a library, but keep getting 'undefined reference to' messages

查看:21
本文介绍了试图包含一个库,但不断收到“未定义的引用"消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 libtommath 库.我在 Ubuntu linux 上为我的项目使用 NetBeans IDE.我已经下载并构建了库,我已经完成了make install"以将生成的 .a 文件放入/usr/lib/并将 .h 文件放入/usr/include

I am attempting to use the libtommath library. I'm using the NetBeans IDE for my project on Ubuntu linux. I have downloaded and built the library, I have done a 'make install' to put the resulting .a file into /usr/lib/ and the .h files into /usr/include

它似乎正确地找到了文件(因为我不再遇到那些错误,这是我在安装到/usr 目录之前所做的).

It appears to be finding the files appropriately (since I no longer get those errors, which I did before installing into the /usr directories).

但是,当我创建一个简单的 main 来调用 mp_init(在库中)时,当我尝试创建我的项目时出现以下错误:

However, when I create a simple main making a call to mp_init (which is in the library), I get the following error when I attempt to make my project:

mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
gcc -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.c
mkdir -p dist/Debug/GNU-Linux-x86
gcc -o dist/Debug/GNU-Linux-x86/cproj1 build/Debug/GNU-Linux-x86/main.o
build/Debug/GNU-Linux-x86/main.o: In function 'main':
/home/[[myusername]]/NetBeansProjects/CProj1/main.c:18: undefined reference to `mp_init'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cproj1] Error 1

所以,看起来链接器在库中找不到该函数,但它确实存在,所以我只是不知道是什么导致了这种情况.

So, it looks like the linker can't find the function within the library, however it IS there, so I just don't know what could be causing this.

如果我直接输入 gcc 命令并跳过 makefile,我会得到同样的错误,我还确保静态库也使用 gcc 编译.

I get the same error if I type the gcc command directly and skip the makefile, I also made sure the static library got compiled with gcc as well.

编辑添加:

如果我直接进行编译并使用 -l 或 -L 添加库,则会出现相同的错误:

I get these same errors if I do the compile directly and add the library with -l or -L:

$ gcc -l /usr/lib/libtommath.a main.c 
/usr/bin/ld: cannot find -l/usr/lib/libtommath.a
collect2: ld returned 1 exit status

$ gcc -llibtommath.a main.c 
/usr/bin/ld: cannot find -llibtommath.a
collect2: ld returned 1 exit status

$ gcc -Llibtommath.a main.c 
/tmp/ccOxzclw.o: In function `main':
main.c:(.text+0x18): undefined reference to `mp_init'
collect2: ld returned 1 exit status

$ gcc -Llibtommath.a main.c 
/tmp/ccOxzclw.o: In function `main':
main.c:(.text+0x18): undefined reference to `mp_init'
collect2: ld returned 1 exit status

我对这些东西非常生疏,所以我不确定我在这里使用了正确的命令,在 -L 示例中是否找到了库?如果没有找到图书馆,我怎么才能找到图书馆?它在/usr/lib 中,我已经尝试过使用当前目录中的 .a 文件等.是否需要设置环境变量?如果是这样,如何,等等

I am very rusty on this stuff, so I'm not sure I'm using the right command here, in the -L examples are the libraries being found? If the library isn't being found how on earth do I get it to find the library? It's in /usr/lib, I've tried it with the .a file in the current directory, etc. Is there an environment variable I need to set? If so, how, etc.

我尝试了一个完全不同的库 (GMP) 并且遇到了完全相同的问题.这一定是某种 Ubuntu 环境问题?有人知道如何解决这个问题吗?

I've tried a completely different library (GMP) and had the EXACT same problem. This has got to be some kind of Ubuntu environment issue? Anyone have any idea how to fix this?

推荐答案

这里的技巧是将库放在您正在编译的模块之后.问题是参考的东西.链接器按顺序解析引用,因此当库在编译模块之前时,链接器会感到困惑并且认为不需要库中的任何函数.通过将库放在模块之后,模块中库的引用由链接器解析.

The trick here is to put the library AFTER the module you are compiling. The problem is a reference thing. The linker resolves references in order, so when the library is BEFORE the module being compiled, the linker gets confused and does not think that any of the functions in the library are needed. By putting the library AFTER the module, the references to the library in the module are resolved by the linker.

这篇关于试图包含一个库,但不断收到“未定义的引用"消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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