编译时包含库
首先,背景信息:Ubuntu 10.10、gcc 4.4.5、C++、Qt Creator 2.3.1、FLANN。
问题:每次编译代码时,我都会收到一条错误消息,提示“未定义对 [FLANN 函数] 的引用”。
说明:我一直在 Qt Creator 中开发一个 GUI,它将利用我构建的一些类,其中包括对 FLANN 库的引用(不一定需要了解)。当然,一切都很顺利,直到我合并这些类及其头文件。我将库添加到 .pro 文件中以防万一,但这并没有解决我的问题。与我使用 Makefile 测试类时生成文件的方式相比,我将范围缩小到 Qt 生成文件的方式:
My Makefile: g++ -g process_stuff.o driver.o -o test.exe /usr /local/lib/libflann_s.a
我将此库附加到 gcc 命令的末尾,并且它运行良好。此外,如果我删除 /usr/local/lib/libflann_s.a 我会得到同样的错误,导致我退出 Qt。
问题:通过在 gcc 调用末尾包含该库,我到底在做什么?我该怎么做才能在 Qt Creator 中发生这种情况?感谢所有帮助,并提前致谢。
First, Background info: Ubuntu 10.10, gcc 4.4.5, C++, Qt Creator 2.3.1, FLANN.
Problem: Every time I compile my code I get an error saying 'undefined reference to [function from FLANN]'.
Explanation: I've been working on a GUI in Qt Creator that will utilize a few classes I built that include references to the FLANN library (not necessarily that important to know). Everything was going smoothly until I incorporated these classes and their header files, of course. I added the library to the .pro file just in case, but that didn't solve my problem. I narrowed down the situation to how Qt is making the files as compared to how I was making the files when I was testing my classes with a Makefile:
My Makefile: g++ -g process_stuff.o driver.o -o test.exe /usr/local/lib/libflann_s.a
I'm appending this library to the end of the gcc command, and it works perfectly. Further, if I remove the /usr/local/lib/libflann_s.a I get the same error making it myself that I got out of Qt.
Question: What exactly am I doing by including the library at the end of my gcc calls, and what can I do to have this happen in Qt Creator? All help is appreciated, and thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过在 gcc 命令末尾添加库来做什么的简短答案是告诉构建系统在哪里可以找到该库以及您想要将哪个库链接到代码中。 -L 部分表示库的路径,-l 是您要链接的库的名称。我怀疑这两个都没有包含在您的 Qt 项目文件中,这就是为什么当您正常运行它时它没有构建。您可以将这两个添加到项目中,如下所示:
The short answer to what you're doing by adding the library at the end of the gcc command is telling the build system where it can find the library and what library you want to link into your code. The -L part indicates the path to libraries and the -l is the name of the library you wish to link against. Both of which I suspect are not being included in your Qt Project file, which is why it isn't building when you're running it normally. You can add the two in the project as:
外部库通常以两种形式提供:静态库和共享库。静态库是“.a”文件。当程序链接到静态库时,程序使用的任何外部函数的目标文件中的机器代码都会从库复制到最终的可执行文件中。
External libraries are usually provided in two forms: static libraries and shared libraries. Static libraries are the ‘.a’ files . When a program is linked against a static library, the machine code from the object files for any external functions used by the program is copied from the library into the final executable.
但过了一会儿...... unix:!macx:!symbian: LIBS += -L$$PWD/../../../../usr/local/lib/ -lflann_cpp_s 将其修复为:unix :!macx:!symbian: LIBS += -L$$PWD/../../../../usr/local/lib/ -lflann_s
Qt 库向导决定包含错误文件作为库。更改文件后,一切正常。
But moments later.... unix:!macx:!symbian: LIBS += -L$$PWD/../../../../usr/local/lib/ -lflann_cpp_s Upon fixing that to: unix:!macx:!symbian: LIBS += -L$$PWD/../../../../usr/local/lib/ -lflann_s
The Qt library wizard decided to include the wrong file as the library. After changing the file, everything works.