[英]Qt generated project code finds shared library file but still getting undefined references during build
我獲得了用於控制打印機的第三方庫的源代碼。 我從中構建了一個共享對象。 我能夠成功地使用該共享對象在 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 庫。 除非在 sii 庫之后包含 rt 庫,否則 Makefile 不起作用。 盡管如此,我的 Qt 程序沒有鏈接。
嘗試將示例代碼編譯為 C++ 程序而不是 C 程序給了我所需的線索。
我需要在 API 標頭周圍包含extern "C"
。
我的 Qt 程序現在可以編譯了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.