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程序仍未链接。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)