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. 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. 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.

Here are my pro files. They are both in the same SUB_DIR parent project and the paths are correct.

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. (ProspecTest is a unit test project for 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.

The error you describe points to a linker error. It happens when a necessary object/lib file is not specified. 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:

>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:

gcc test.c -lwsock32

Also, note that a libsomething.a is not the only place where a reference can be resolved. You might have missed an object file.

Unfortunately without more details I am not able to tell you the exact problem.

Undefined reference when including lib, release/widget.o:widget.cpp::-1: error: undefined reference to file. now how can i set up my QT creator project to make use of libusb? LIBS += -Llibusb -l libusb but compiling gives me this error: Qt Code: the lib and you should have gotten .​dll files to place those into your LIBS += -LC:/Qt/mingw/lib \. I have started to reimplement my Qt pet-projects using GTK due to Qt Project hostility to open-source community. And I found out that usage of inheritance just to be able to send signals to GUI is probably not the best option.

You probably forgot to export your symbols, as described here on MSDN and in various questions on this website. 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. 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.

External library throws undefined reference errors in Qt Creator, External library throws undefined reference errors in Qt Creator - c++. So apparently, it's just the external source code that has issues accessing the I am using MinGW 5.3 (Windows 7). codelibrary/QtMinGW53/libmodbus-master/src/.​libs/liblibmodbus.dll.a This gives me the error: -1: error: No rule to make target '../. my MinGW version is not 7.2.0 but 8.1.0. i wonder, how you got over this, then ;). try to get qtcreator out of the way, for a second. make a simple prog, that uses just 1 cv::Mat, and try to use:

Had the same problem just now. Build was working fine on macOS/OSX but not on Windows with the same codebase. Thanks to rubenvb, who pointed me in the right direction. Here the full solution, how to do it for Qt:

Use Q_DECL_EXPORT on functions when building the lib. And use Q_DECL_IMPORT when using the functions from the lib.

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

Solved the problem in my case, hope it helps!

SQLAPI++ undefined reference error, Hi I want to use 3rd part library which is SQLAPI++. paths, it gives me error while compiling " error: undefined reference to `SAConnection::SAConnection()' "​. First i tried to set the path to the SQLAPI\MinGW\libs folder but the problem still occured so i put My Qt Creator Version is 4.8.2 and QT 5.12.1 . 👀MinGW 32bit and 64bit version of OpenCV compiled on Windows. Including OpenCV 3.3.1, 3.4.1, 3.4.1-x64, 3.4.5, 3.4.6, 3.4.7, 3.4.8-x64, 3.4.9, 4.0.0-alpha-x64, 4.0

Link a library with Qt on windows, Hi, I m trying to port a simple Qt application from linux to windows which depends one But it returns me error. I m confused now about *a *.so *.lib and *.dll file. `decompress_peek': /cygdrive/c/Dev/test/htslib-1.3.2/hts.c:141: undefined reference to And configure qt creator to use msys-mingw too ! On Windows, MinGW compiled linking libraries are in .a, but you will need to add it manually (as of Qt Creator 2.7). You could also try simply linking the .dll directly cause it would probably work. Don't try this with a MSVC compiled library .

MinGW linking error "undefined reference, My problem is this: I have a library that I have built in Qt Creator but that project with MinGW (g++) and it consistantly gives me this error: The Image will have the red and blue channel reversed, so we need to correct them. 1) I've Installed Qt-5.6.2-mingw-492 2) Run Qt/Mingw prompt (Qt 5.6 desktop from Start menu), than bash in this window 3) cd /path/to/LibRaw-0.17.2 4) mingw-32-make -f Makefile.mingw (no need to run ./configure, but it does not change Makefile.mingw, so it does not matter). several bin/*.exe created after that, so both library and samples

Qt Creator - OpenCV - undefined reference/can't find libraries, At first I've set the paths to *.dll.a files manually that error goes away, a bunch of others replace it, as I get undefined reference to cv::EVERYTHING error. I'm using: Windows 7 Qt 5.4.0 Qt Creator 3.3.0 OpenCV 2.4.10 Mingw 4.9.1 the is_empty() function returns true if the position (an x and y) on the board is a nullptr. g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\test.exe release/main.o -lglu32 -lopengl32 -lgdi32 -luser32 -lmingw32 -lqtmain -LC:\Qt\5.2.0\mingw48_32\lib

Comments
  • The information you provide is not enough, please at least provide a minimal and buildable example of your problem.
  • The exact error message would also be helpful.