gcc -I 和 -L 选项似乎不起作用

发布于 2024-11-26 00:22:37 字数 1693 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尹雨沫 2024-12-03 00:22:37

我注意到 QT 的自动包含路径没有尾部斜杠,而你的有。您是否尝试过编写不带尾部斜杠的路径?

linux-g++ {
 INCLUDEPATH += /home/myusername/local/include
 LIBS += -L/home/myusername/local/lib -lqjson
}

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?

linux-g++ {
 INCLUDEPATH += /home/myusername/local/include
 LIBS += -L/home/myusername/local/lib -lqjson
}
诗化ㄋ丶相逢 2024-12-03 00:22:37

G++ 和朋友(即 as、ld 等)不会直接告诉您它在哪里查找头文件和库文件。调试此问题的一种方法是运行 strace -o output.txt -eopen -s 1024 -f qmake 。这将运行qmake,记录qmake的所有open系统调用及其派生的所有子进程。然后,您将能够看到它在哪些目录中以及以什么顺序搜索头文件(和库)。 stdio.h 的输出摘录示例:

26069 open("/usr/lib/gcc/x86_64-redhat-linux/4.6.0/include/stdio.h", O_RDONLY|O_NOCTTY) = -1 ENOENT (No such file or directory)
26069 open("/usr/local/include/stdio.h", O_RDONLY|O_NOCTTY) = -1 ENOENT (No such file or directory)
26069 open("/usr/include/stdio.h", O_RDONLY|O_NOCTTY) = 4

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 run qmake logging all open system calls of qmake 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:

26069 open("/usr/lib/gcc/x86_64-redhat-linux/4.6.0/include/stdio.h", O_RDONLY|O_NOCTTY) = -1 ENOENT (No such file or directory)
26069 open("/usr/local/include/stdio.h", O_RDONLY|O_NOCTTY) = -1 ENOENT (No such file or directory)
26069 open("/usr/include/stdio.h", O_RDONLY|O_NOCTTY) = 4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文