gcc -I 和 -L 选项似乎不起作用
我正在尝试使用 qmake 在我的系统中编译一个项目。项目的一些依赖项未安装,但驻留在我的主目录中,或多或少像这样: libs 文件:/home/myusername/local/lib
和我的包含目录 /home/myusername /local/include
。在包含目录中,我有一个文件夹 qjson
,其中包含库中所需的标头。在 lib 文件夹中,我有文件 libqjson.so libqjson.so.0 libqjson.so.0.7.1
。
我的 qmake 项目文件看起来像这样:
linux-g++ {
INCLUDEPATH += /home/myusername/local/include/
LIBS += -L/home/myusername/local/lib/ -lqjson
}
生成的 makefile 将生成如下命令:
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB \
-DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../qbuzz \
-I/usr/include/qt4/QtCore -I/usr/include/qt4/QtNetwork -I/usr/include/qt4/QtGui \
-I/usr/include/qt4 -I/home/myusername/local/include/ -I. -I. -I../myproject -I. \
-o qbuzz-result.o ../myproject/myfile.cc
很明显,我的包含目录位于 gcc 的 -I
选项中。 myfile.cc
包含这样的包含:
#include <qjson/parser.h>
但是,运行 make 后,我收到错误:
../myproject/myfile.cc:2:26: fatal error: qjson/parser.h: No such file or directory
compilation terminated.
现在,如果我修改环境变量 CPLUS_INCLUDE_PATH
以添加我的本地包含文件,我在那里没有问题,但在链接器阶段我收到错误:
/usr/bin/ld: cannot find -lqjson
collect2: ld returned 1 exit status
即使链接器命令是:
g++ -omyprogram main.o mainwindow.o myfile.o moc_mainwindow.o -L/usr/lib \
-L/home/myusername/local/lib/ -lqjson -lQtGui -lQtNetwork -lQtCore -lpthread
我也可以绕过修改环境变量LIBRARY_PATH
。然而,我正在寻找一个依赖于修改尽可能少的环境变量的解决方案,毕竟,为什么选项 -L 和 -I 在那里?
我在 Windows 上使用 MinGW g++ 没有任何问题。
I am trying to compile a project in my system using qmake. Some dependencies of the project are not installed but reside in my home directory, more or less like this: libs files: /home/myusername/local/lib
and my includes directory /home/myusername/local/include
. Inside the include directory I have a folder, qjson
with the needed headers from the library. In the lib folder I have the files libqjson.so libqjson.so.0 libqjson.so.0.7.1
.
My qmake project file looks something like this:
linux-g++ {
INCLUDEPATH += /home/myusername/local/include/
LIBS += -L/home/myusername/local/lib/ -lqjson
}
and the generated makefile will produce commands like this one:
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB \
-DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../qbuzz \
-I/usr/include/qt4/QtCore -I/usr/include/qt4/QtNetwork -I/usr/include/qt4/QtGui \
-I/usr/include/qt4 -I/home/myusername/local/include/ -I. -I. -I../myproject -I. \
-o qbuzz-result.o ../myproject/myfile.cc
It is clear that my include directory is in the -I
option of gcc. myfile.cc
contains an include like this one:
#include <qjson/parser.h>
However, after running make, I get the error:
../myproject/myfile.cc:2:26: fatal error: qjson/parser.h: No such file or directory
compilation terminated.
Now, if I modify the environment variable CPLUS_INCLUDE_PATH
to add my local include file, I have no problems there, but in the linker stage I got the error:
/usr/bin/ld: cannot find -lqjson
collect2: ld returned 1 exit status
Even though the linker command was:
g++ -omyprogram main.o mainwindow.o myfile.o moc_mainwindow.o -L/usr/lib \
-L/home/myusername/local/lib/ -lqjson -lQtGui -lQtNetwork -lQtCore -lpthread
I also can get around modifying the environment variable LIBRARY_PATH
. However I am looking for a solution that relies on modifying as few environment variables as possible, and after all, why are the options -L and -I there?
I works on Windows without problems using MinGW g++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我注意到 QT 的自动包含路径没有尾部斜杠,而你的有。您是否尝试过编写不带尾部斜杠的路径?
I notice that the QT's automatic include paths have no trailing slashes, and yours do. Have you tried writing the paths without trailing slashes?
G++ 和朋友(即 as、ld 等)不会直接告诉您它在哪里查找头文件和库文件。调试此问题的一种方法是运行 strace -o output.txt -eopen -s 1024 -f qmake 。这将运行
qmake
,记录qmake
的所有open
系统调用及其派生的所有子进程。然后,您将能够看到它在哪些目录中以及以什么顺序搜索头文件(和库)。 stdio.h 的输出摘录示例:G++ and friends (i.e. as, ld, etc) will not directly tell you exactly where it looks for header and library files. One way to debug this is to run
strace -o output.txt -eopen -s 1024 -f qmake
. This will runqmake
logging allopen
system calls ofqmake
and all of the child processes it forks. You will then be able to see in what directories and in what order it searches for header files (and libraries). Example output extract for stdio.h: