Qt 生成的项目代码找到共享库文件但在构建过程中仍然得到未定义的引用
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 程序来获得库 linked。这是我用于 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 程序没有 link.
尝试将示例代码编译为 C++ 程序而不是 C 程序给了我所需的线索。
我需要在 API header 周围包含 extern "C"
。
我的 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 程序来获得库 linked。这是我用于 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 程序没有 link.
尝试将示例代码编译为 C++ 程序而不是 C 程序给了我所需的线索。
我需要在 API header 周围包含 extern "C"
。
我的 Qt 程序现在可以编译了。