简体   繁体   English

Qt Creator中的共享库与mingw给出了未定义的引用错误

[英]shared library in Qt Creator with mingw gives undefined reference error

Resolved. 解决。 When I add my library as a normal internal library everything is fine. 当我将我的库添加为普通内部库时,一切都很好。 What I had failed to do was use the MY_LIBRARY_EXPORT macro on some free functions defined in a namespace. 我没有做的是在名称空间中定义的一些自由函数上使用MY_LIBRARY_EXPORT宏。 Somehow I had manipulated the library import in such a way to get those functions working, but it broke in other ways as described here. 不知何故,我以某种方式操纵了库导入以使这些功能正常工作,但它以其他方式破坏了这里所描述的。

I have not been able to use a shared library using the Qt Creator wizard. 我无法使用Qt Creator向导使用共享库。 I have tried with dynamic linkage, and have failed, and cannot use as a static lib either. 我尝试过动态链接,但都失败了,也不能用作静态库。 In both cases I get an 'undefined reference to ` IMP _*' error. 在这两种情况下,我得到一个'未定义的引用' IMP _ *'错误。

Here are my pro files. 这是我的专业档案。 They are both in the same SUB_DIR parent project and the paths are correct. 它们都在相同的SUB_DIR父项目中,并且路径是正确的。

Relevant parts of the library pro file: 库专业文件的相关部分:

TARGET = Prospec
TEMPLATE = lib
CONFIG += staticlib    
QMAKE_CXXFLAGS += -std=c++11    
DEFINES += PROSPEC_LIBRARY

The entirety of the user .pro file. 整个用户.pro文件。 (ProspecTest is a unit test project for Prospec). (ProspecTest是Prospec的单元测试项目)。

#-------------------------------------------------
#
# Project created by QtCreator 2013-12-12T15:04:30
#
#-------------------------------------------------

QT       += testlib    
QT       -= gui

TARGET = prospectest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

QMAKE_CXXFLAGS += -std=c++11

SOURCES += prospectest.cpp \
    mltest.cpp \
    convertertest.cpp \
    numericitemtest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"

HEADERS += \
    utilities.h \
    mltest.h \
    convertertest.h \
    numericitemtest.h

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../Prospec/release/ -lProspec
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../Prospec/debug/ -lProspec

INCLUDEPATH += $$PWD/../Prospec/debug
DEPENDPATH += $$PWD/../Prospec/debug

win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../Prospec/release/libProspec.a
else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../Prospec/debug/libProspec.a

# Boost
INCLUDEPATH += C:/boost/boost_1_55_0/
LIBS += "-LC:/boost/boost_1_55_0/stage/lib/"

EDIT: the undefined reference errors happen only where I invoke functionality in a source file. 编辑:未定义的引用错误只发生在我调用源文件中的功能的地方。 For instance, if I construct a library object I get the error, but if I move that objects constructor into a header file, then compile is OK. 例如,如果我构造一个库对象,我得到错误,但如果我将该对象构造函数移动到头文件中,那么编译就可以了。 Right now I'm not sure if the problem is due to the Qt environment, or me not understanding shared libraries in general. 现在我不确定问题是由Qt环境引起的,还是我不了解共享库。

The error you describe points to a linker error. 您描述的错误指向链接器错误。 It happens when a necessary object/lib file is not specified. 当未指定必需的对象/ lib文件时,会发生这种情况。 The most likely scenario is this: you include a header and use a function from that header, but don't specify in which library that function is defined. 最可能的情况是:您包含标头并使用该标头中的函数,但不指定在哪个库中定义该函数。

Small example: 小例子:

//test.c
#include <winsock.h>
int main() { gethostbyname("localhost"); }

'undefined reference' linker error: 'undefined reference'链接器错误:

>gcc test.c
cczICEqq.o:test.c:(.text+0x1e): undefined refer ence to `gethostbyname@4'
collect2.exe: error: ld returned 1 exit status

In this case you would have to specify libwsock32.a like this: 在这种情况下,你必须像这样指定libwsock32.a:

gcc test.c -lwsock32

Also, note that a libsomething.a is not the only place where a reference can be resolved. 另请注意,libsomething.a不是唯一可以解析引用的位置。 You might have missed an object file. 您可能错过了一个目标文件。

Unfortunately without more details I am not able to tell you the exact problem. 不幸的是,没有更多细节,我无法告诉你确切的问题。

You probably forgot to export your symbols, as described here on MSDN and in various questions on this website. 您可能忘记导出符号,如MSDN和本网站上的各种问题所述。 It's a pain in general, and there are a few ways to handle this, none much better than the other. 一般来说这是一种痛苦,有几种方法可以解决这个问题,没有比另一种更好的方法。

This makes it so that the DLL and import library actually contain a reference to the symbols you compiled into it. 这使得DLL和导入库实际上包含对您编译到其中的符号的引用。 You can compare it partially to GCC's symbol visibility, although that only works in Linux, and GCC also supports the stuff described on MSDN for Windows. 您可以将其部分地与GCC的符号可见性进行比较,尽管这仅适用于Linux,而GCC也支持MSDN for Windows中描述的内容。

Had the same problem just now. 刚才有同样的问题。 Build was working fine on macOS/OSX but not on Windows with the same codebase. Build在macOS / OSX上运行正常,但在具有相同代码库的Windows上运行不正常。 Thanks to rubenvb , who pointed me in the right direction. 感谢rubenvb ,他指出了我正确的方向。 Here the full solution, how to do it for Qt: 这里是完整的解决方案,如何为Qt做到这一点:

Use Q_DECL_EXPORT on functions when building the lib. 在构建lib时,在函数上使用Q_DECL_EXPORT And use Q_DECL_IMPORT when using the functions from the lib. 使用lib中的函数时使用Q_DECL_IMPORT

Example how to do this here: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application#Creating_a_shared_library 示例如何在此处执行此操作: https//wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application#Creating_a_shared_library

Solved the problem in my case, hope it helps! 解决了我的问题,希望它有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM