There are a few questions which seem to be similar, but nothing really helps me out. I want to create a static library inside a project and use it in the same project, but linking error occurs.
A good example, which meets my conditions very well is attached to the Qt Ticket QTBUG-45706 https://bugreports.qt.io/browse/QTBUG-45706. In a simple explanation, we have an app which should use some self-made libraries. Just modifiy a few things to see my problem.
app -> main.cpp
#include <QCoreApplication>
#include <lib.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Lib l1;
return a.exec();
}
lib.pro
CONFIG += staticlib
If you now compile the project, you will see the following error
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl Lib2::Lib2(void)" (__imp_??0Lib2##QEAA#XZ) referenced in function main
Use Qt Creator 4.0.3 based on Qt 5.6.1, qmake with mscv2013
What is needed to bring this to work?
CLARIFY:
The project structure is as follow:
subdirs_test.pro (subdir project)
\- app (app project, includes lib and lib2)
\-- app.pro
\-- main.cpp
\- lib (static library)
\-- lib.pro
\-- lib.h
\-- lib_global.h
\-- lib.cpp
\- lib2 (static library)
\-- lib2.pro
\-- lib2.h
\-- lib2_global.h
\-- lib2.cpp
The 'app' project should use the classes from lib and lib2, which are static libraries.
As suggested, use the "Add Library..." doesn't change a thing. In my case, this code will be generated.
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../lib/release/ -llib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../lib/debug/ -llib
else:unix: LIBS += -L$$OUT_PWD/../lib/ -llib
INCLUDEPATH += $$PWD/../lib
DEPENDPATH += $$PWD/../lib
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/release/liblib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/debug/liblib.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/release/lib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/debug/lib.lib
else:unix: PRE_TARGETDEPS += $$OUT_PWD/../lib/liblib.a
You can use QtCreators Subdirs project. Here's a detailed step by step instructions how to achieve that with QtCreator.
Pick Subdirs Project from the New Project wizard menu.
Add Subrojects by clicking on created Subdirs project with right
mouse button and selecting New Subproject....
By following wizards you should have a GUI or console Subproject and
a library Subproject. Then click on subproject where you want to link
your library subproject with right mouse button and select Add
Library....
Select Internal library in the dialog and you will be prompted to
choose library you want to add.
Make sure your library subproject is included before gui/console
subproject as subdir project will fail to build.
TEMPLATE = subdirs
SUBDIRS += \
LibProject \
CoreProject
Can you try doing next steps:
Right button on project
Add library
Choose type (external or other)
Set flag on static, like this picture
Related
I prepare TagLib for use in my TagLib project as follows:
Windows 10
Build the Makefiles:
Open CMake GUI.
Set Paths.
"Where is the source code" and "Where to build the binaries"
Example, Both would be: C:\GitRoot\taglib
Tick: Advanced
Select: Configure
Select: Generator
Tick: Use default native compilers
Select: Finish
Wait until done.
If using ZLib, Scroll down.
(to the bottom of the list of options ... should go over them all)
Edit: ZLIB_INCLUDE_DIR
Edit: ZLIB_LIBRARY
Select: Generate
Build the project:
MinGW:
C:\GitRoot\taglib> gmake
Install the project:
MinGW:
C:\GitRoot\taglib> gmake install
I copy the above generated files into my Taglib project on Qt, so that now this is my taglibtest project folder structure:
then add the following files to my project:
F:*\taglibtest\taglib\lib\libtag.dll.a**
F:*\taglibtest\taglib\lib\libtag_c.dll.a**
as you can see below in tablibtest.pro
taglibtest.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = taglibtest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/lib/ -llibtag.dll
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/lib/ -llibtag.dlld
INCLUDEPATH += $$PWD/taglib/include
DEPENDPATH += $$PWD/taglib/include
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/lib/ -llibtag_c.dll
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/lib/ -llibtag_c.dlld
INCLUDEPATH += $$PWD/taglib/include
DEPENDPATH += $$PWD/taglib/include
HEADERS += \
taglib/include/taglib/aifffile.h \
taglib/include/taglib/aiffproperties.h \
etc.... \
etc.... \
etc....
In the main:
**main.cpp**
#include <QCoreApplication>
#include <QDebug>
#include <taglib/include/taglib/fileref.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TagLib::FileRef f("E:/Dire Straits - Sultans of Swing.mp3");
return a.exec();
}
I get the following error when I run the project:
:-1: error: cannot find -llibtag.dll
:-1: error: cannot find -llibtag_c.dll
collect2.exe:-1: error: error: ld returned 1 exit status
What am I missing in my project? Which files should I include in my project to use the Taglib Framework
Thank you all in advance.
M'kay so I'm new to Qt and I'm trying to include the boost/filesystem lib to my project. However, when I try to build it, I always get this error:
LNK1104: File "libboost_filesystem-vc140-mt-1_61.lib" cannot be
opened.
Now, I know that this library needs to be build beforehand, so, as described on the Boost-Website I made:
bootstrap
.\b2
and it built just fine. I also used the Qt dialog to add the libary to my project, my .pro-file looks like this:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ProjectName
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
someclass.cpp \
someclass2.cpp
HEADERS += mainwindow.h \
someclass.h \
someclass2.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/libarys -llibboost_filesystem-vc140-mt-1_61
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/libarys -llibboost_filesystem-vc140-mt-1_61
else:unix: LIBS += -L$$PWD/libarys -llibboost_filesystem-vc140-mt-1_61
INCLUDEPATH += $$PWD/boost
DEPENDPATH += $$PWD/boost
Apparently, I did something wrong, but I can't figure out, WHAT exactly it is. The internet knows the problem, but it seems there's no "easy" solution to it.
Turned out, my project didn't like the lib-version.
I rebuilt my lib files:
>b2 --build-dir="destination/directory" toolset=msvc --build-type=complete link=static threading=multi --with-filesystem stage
Then, I used the library in
release/link-static/runtime-link-static/threading-multi/
Which is:
libboost_filesystem-vc140-mt-s-1_61
And re-ran qmake for the .pro-file. Then built and executed the application and the error is gone.
I have installed qt-opensource-windows-x86-msvc2013_64_opengl-5.4.0.exe and compiled boost_1_58_0.zip with this command: b2 toolset=msvc --build-type=complete stage. It works fine with Visual Studio, but when I try use it with Qt I get this error:
:-1: error: LNK1104: cannot open file 'libboost_filesystem-vc120-mt-gd-1_58.lib'
Here is my .pro file:
TEMPLATE = app
QT += qml quick widgets
SOURCES += main.cpp \
testclass.cpp
RESOURCES += qml.qrc
INCLUDEPATH += C:\boost
LIBS += "-LC:\boost\stage\lib\libboost_filesystem-vc120-mt-gd-1_58.lib"
#Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
testclass.h
In the LIBS variable use just "-L" for correct library path (-L). You made a mix, specifying a file (lowercase l) while libs directory is missing.
You do not need to specify the library, boost has pragmas for that.
I am trying to include the assimp library (http://assimp.sourceforge.net/main_doc.html) for importing assets into my QT project.
I managed to include it in VS2012 but not in QT.
The import wizard gives me this code:
unix|win32: LIBS += -L$$PWD/assimp--3.0.1270-sdk/lib/assimp_debug-dll_win32/ -lassimp
INCLUDEPATH += $$PWD/assimp--3.0.1270-sdk/include
DEPENDPATH += $$PWD/assimp--3.0.1270-sdk/include
win32: PRE_TARGETDEPS += $$PWD/assimp--3.0.1270-sdk/lib/assimp_debug-dll_win32/assimp.lib
else:unix: PRE_TARGETDEPS += $$PWD/assimp--3.0.1270-sdk/lib/assimp_debug-dll_win32/libassimp.a
When i build the code it throws this error
undefined reference to `Assimp::Importer::Importer()'
If i understand it right:
QT / VS2012
INCLUDEPATH =Include directories
LIBS = Library directories
PRE_TARGETDEPS=Additional dependancies
Do i have to manualy add the location off the .dll file into .pro or what am i missing?
I have a codebase where there are some shared libraries. For example LIB1 does some sort of processing that APP1 needs (depends on).
The qmake *.pro files are setup like the following. There is one root *.pro that is a TEMPLATE=subdirs that lists APP1 and LIB1 as it's subdirs.
LIB1.pro:
TARGET = LIB1
TEMPLATE = lib
QT += core
QT += xml
DESTDIR = path/to/libs/directory
SOURCES += \
File1.cpp \
FileN.cpp
HEADERS += \
File1.h \
FileN.h
APP1.pro:
#------ dependencies ------#
LIBS += -Lpath/to/libs/directory
# LIB1
INCLUDEPATH += path/to/libs/directory/LIB1lib
DEPENDPATH += path/to/libs/directory/LIB1lib
LIBS += -lLIB1
HEADERS = \
FileNplus1.h \
FileM.h
SOURCES = \
FileNplus1.cpp \
FileM.cpp
The problem is that when you compile the root *.pro file LIB1 will compile but APP1 fails to compile with the error QDomElement: No such file or directory because APP1.pro doesn't have QT += xml.
There is a hack that I use but I would like to overcome this hack. The hack involves adding the following line to APP1.pro:
# add this to APP1.pro... let's it compile again
QT += xml
Is there anyway to setup your qmake *.pro files such that, APP1.pro depends on LIB1 without needing to modify APP1.pro to add the QT += xml?
(The reason for this question is let's say you have other libraries that depend on other stuff... I would like to have Qt/Qmake take care of the dependencies like for example the Qt += xml dependency.)
qmake allows you to create your own configuration features. (last paragraph)
So you can create your own feature and move your lib1-linking code to it. You can add QT+=xml here. And in app.pro you'll just write CONFIG += lib1