如何解决Qt生成的项目代码找到共享库文件,但在构建过程中仍获得未定义的引用
为我提供了用于控制打印机的第三方库的源代码。我从中建立了一个共享对象。我能够成功使用该共享库在C中运行一个简单程序。我试图将其与我构建的现有Qt GUI集成在一起,但是却遇到了未定义的引用错误。因此,我做了一个新的干净的Qt项目,但是仍然出现以下错误:
/usr/bin/ld: main.o: in function `test_printer(int,char const**)':
/home/user/printer-test/debug/../printer-test/main.cpp:142: undefined reference to `sii_api_open_device(void**,char*)'
/usr/bin/ld: /home/user/printer-test/debug/../printer-test/main.cpp:143: undefined reference to `sii_api_close_device(void*)'
.pro和main.cpp是:
printer-test.pro
QT += quick
CONFIG += c++17
HEADERS += sii_api.h
SOURCES += main.cpp
RESOURCES += qml.qrc
TRANSLATIONS += printer-test_en_US.ts
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# Qt generated the following lines when given my library path
unix:!macx: LIBS += -L$$PWD/printer_lib/ -lsii
INCLUDEPATH += $$PWD/printer_lib
DEPENDPATH += $$PWD/printer_lib
# I added this one afterward
unix:!macx: LIBS += -lrt
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "sii_api.h" //< Library header
void test_printer(int argc,const char *argv[])
{
SIIAPIHANDLE hSiiApiHandle = nullptr;
sii_api_open_device( &hSiiApiHandle,const_cast<char *>(argv[2]) );
sii_api_close_device( hSiiApiHandle );
}
int main(int argc,char *argv[])
{
//<Qt setting up the gui and engine>
const char* args[] = {"","0","/dev/ttyUSB0"};
test_printer(3,args);
return app.exec();
}
qmake生成一个看起来像这样的Makefile:
<snip>
LIBS = $(SUBLIBS) -L/home/user/printer-test/printer-test/printer_lib/ -lsii -lrt <snip>
printer-test: $(OBJECTS)
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
<snip>
我能够使该库与制造商提供给我的示例C程序链接。这是我用于C程序的Makefile:
PROGRAM = sample
all::
gcc $(PROGRAM).c -o $(PROGRAM) -L ./printer_lib -lsii -lrt
clean::
rm -f $(PROGRAM) *.o
如示例Makefile所示,我还需要包括rt库。除非rt库被包含在sii库之后,否则Makefile不起作用。尽管如此,我的Qt程序仍未链接。
解决方法
尝试将示例代码编译为C ++程序而不是C程序,这为我提供了所需的线索。
我需要在API标头周围包含extern "C"
。
我的Qt程序现在可以编译。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。