静态库中的 g++ 错误链接函数

g++ error linking functions from static library

本文关键字:错误 链接 函数 g++ 静态      更新时间:2023-10-16

我有一个Qt5 C++程序,我正在尝试链接到一个静态库。 静态库是senselock/libsenseEIV.a(相对于主文件.cpp(。 当我编译时,我看到下面的输出:

                                                                      ^
g++ -Wl,-rpath,/opt/Qt/5.7/gcc_64/lib -o test1 main.o   -Lsenselock/libsenseEIV.a -L/opt/Qt/5.7/gcc_64/lib -lQt5Core -lpthread 
main.o: In function `test1()':
/test1/main.cpp:31: undefined reference to `S4Enum'
/test1/main.cpp:58: undefined reference to `S4Enum'
/test1/main.cpp:71: undefined reference to `S4Open'
/test1/main.cpp:83: undefined reference to `S4Control'
/test1/main.cpp:102: undefined reference to `S4Close'
collect2: error: ld returned 1 exit status
make: *** [Makefile:216: test1] Error 1
11:55:27: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project test1 (kit: Desktop Qt 5.7.1 GCC 64bit)
When executing step "Make"

在我的 .pro 文件中,我有

LIBS += -Lsenselock/libsenseEIV.a

万一重要。 有人可以解释如何解决此错误吗? 未定义的引用错误都与位于该libsenseEIV.a库中的函数有关。

我不明白编译器是否找不到 .a 文件或存在其他错误。


更新:我尝试了这种语法

LIBS += -Lsenselock -lsenseEIV

但它会产生错误

/

usr/bin/ld: 找不到 -lsenseEIV

我是否使用了错误的库名称? 如果是这样,我将如何找到这个名字? (假设这被编译到 .a 文件中(

命令行的这一部分是错误的:

 -Lsenselock/libsenseEIV.a

应该是:

 senselock/libsenseEIV.a

(-Lfoo/bar.a告诉链接器在目录foo/bar.a/中搜索库,这根本不是你想要的。

没有-l前缀?

您可以通过以下(大部分等效(方式指定与libsenseEIV.a的链接:

senselock/libsenseEIV.a
-Lsenselock -lsenseEIV
-Lsenselock -l:libsenseEIV.a

您使用链接器标志错误,您应该在-L之后指定库路径,在-l之后指定库名称。换句话说,您需要在赋值中同时具有两者,以LIBS .pro 文件中的变量。

毕竟事实证明,您可以将LIBS += -L$$PWD/senselock/ -lsenseEIV用于您的案例

相关文章: