__declspec(dllimport) in release mode and debug mode in QT - c++

i want to use taglib in my QT project on windows, so i build taglib and zlib from source like below:
cmake CMakeLists.txt
cmake --build --config release
cmake command uses visual studio compiler, i build zlib and taglib from source with above command with no error, i added their include and lib path to QT project .pro file like below:
INCLUDEPATH+=F:\project\TAGLIB\taglib\taglib F:\project\TAGLIB\taglib\taglib\toolkit F:\project\TAGLIB\taglib F:\project\zlib-1.2.11\Release F:\project\zlib-1.2.11
LIBS+=-LF:\project\zlib-1.2.11\Release -lzlib
LIBS+= -LF:\project\TAGLIB\taglib\taglib\Release -ltag
LIBS+=-LF:\project\TAGLIB\taglib\taglib\Debug -ltag
LIBS+=-LF:\project\zlib-1.2.11\Debug -lzlibd
and in main.cpp i added:
#include<fileref.h>
#include<tag.h>
#include<QDebug>
and in main function:
TagLib::FileRef f("C:\\Users\\sub\\Downloads\\Mohsen Chavoshi - Sharhe Alef.mp3");
TagLib::String artist = f.tag()->artist();
when i run my application in debug mode,it doesn't run and give me below errors:
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl TagLib::String::~String(void)" (__imp_??1String#TagLib##UEAA#XZ) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: char const * __cdecl TagLib::String::toCString(bool)const " (__imp_?toCString#String#TagLib##QEBAPEBD_N#Z) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl TagLib::FileName::FileName(char const *)" (__imp_??0FileName#TagLib##QEAA#PEBD#Z) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl TagLib::FileRef::FileRef(class TagLib::FileName,bool,enum TagLib::AudioProperties::ReadStyle)" (__imp_??0FileRef#TagLib##QEAA#VFileName#1#_NW4ReadStyle#AudioProperties#1##Z) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl TagLib::FileRef::~FileRef(void)" (__imp_??1FileRef#TagLib##UEAA#XZ) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: class TagLib::Tag * __cdecl TagLib::FileRef::tag(void)const " (__imp_?tag#FileRef#TagLib##QEBAPEAVTag#2#XZ) referenced in function main
debug\testTag2.exe:-1: error: LNK1120: 6 unresolved externals
but when i run application in Release mode,the above error are just some Warning and application run with no problem, why this is happening?
thank you.

Related

Using non-qtcore libraries on Visual Studio using Qt VS Tools causes LNK2019

I am trying to use QSoundEffect from QtMultimedia. I have included the file like so:
#include <QtMultimedia/QSoundEffect>
and used QSoundEffect in my project.
When I try to compile my project after this, I get LNK2019 errors:
1>chatwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QSoundEffect::QSoundEffect(class QObject *)" (__imp_??0QSoundEffect##QEAA#PEAVQObject###Z) referenced in function "public: __cdecl ChatWindow::ChatWindow(class QWidget *)" (??0ChatWindow##QEAA#PEAVQWidget###Z)
1>chatwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QSoundEffect::~QSoundEffect(void)" (__imp_??1QSoundEffect##UEAA#XZ) referenced in function "int `public: __cdecl ChatWindow::ChatWindow(class QWidget *)'::`1'::dtor$3" (?dtor$3#?0???0ChatWindow##QEAA#PEAVQWidget###Z#4HA)
1>chatwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QSoundEffect::setSource(class QUrl const &)" (__imp_?setSource#QSoundEffect##QEAAXAEBVQUrl###Z) referenced in function "public: __cdecl ChatWindow::ChatWindow(class QWidget *)" (??0ChatWindow##QEAA#PEAVQWidget###Z)
1>mainwidget.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QSoundEffect::play(void)" (__imp_?play#QSoundEffect##QEAAXXZ) referenced in function "private: void __cdecl MainWidget::process_received_forever(void)" (?process_received_forever#MainWidget##AEAAXXZ)
How can I tell Visual Studio about the existence of QtMultimedia related files? Qt include and library paths are handled by the Qt VS Tools extension in Visual Studio, so I'm a bit confused.
For some weird reason, it worked when I manually added Qt5's libpath to library paths and Qt5Multimedia.lib to included libraries manually.

Compile quazip under windows 8.1 and qt creator

I'm new and trying to use Quazip under qt 5.5. So I just downloaded project sources from http://quazip.sourceforge.net/ and opened .pro file. When I try to build project I 've got multiple errors:
C1083: Cannot open include file: 'zlib.h': No such file or directory
To avoid building zlib on windows I just installed (from here: http://gnuwin32.sourceforge.net/packages/zlib.htm) complete pack for windows in C:\GnuWin32. Then I added this line in .pro file:
INCLUDEPATH += C:\GnuWin32\include
This generated another error:
Cannot open include file: 'unistd.h': No such file or directory
But changing this line
#if 1 /* HAVE_UNISTD_H -- this line is updated by ./configure */
into this fixed the problem.
#if HAVE_UNISTD_H
Unfortunately now I get 25 unresolved external symbols, some of them:
quaadler32.obj:-1: error: LNK2019: unresolved external symbol adler32 referenced in function "public: virtual unsigned int __cdecl QuaAdler32::calculate(class QByteArray const &)" (?calculate#QuaAdler32##UEAAIAEBVQByteArray###Z)
quacrc32.obj:-1: error: LNK2019: unresolved external symbol crc32 referenced in function "public: virtual unsigned int __cdecl QuaCrc32::calculate(class QByteArray const &)" (?calculate#QuaCrc32##UEAAIAEBVQByteArray###Z)
unzip.obj:-1: error: LNK2001: unresolved external symbol crc32
quagzipfile.obj:-1: error: LNK2019: unresolved external symbol gzopen referenced in function "private: void * __cdecl QuaGzipFilePrivate::open(class QString const &,char const *)" (?open#QuaGzipFilePrivate##AEAAPEAXAEBVQString##PEBD#Z)
quaziodevice.obj:-1: error: LNK2019: unresolved external symbol deflateInit_ referenced in function "public: virtual bool __cdecl QuaZIODevice::open(class QFlags<enum QIODevice::OpenModeFlag>)" (?open#QuaZIODevice##UEAA_NV?$QFlags#W4OpenModeFlag#QIODevice#####Z)
quaziodevice.obj:-1: error: LNK2019: unresolved external symbol inflate referenced in function "protected: virtual __int64 __cdecl QuaZIODevice::readData(char *,__int64)" (?readData#QuaZIODevice##MEAA_JPEAD_J#Z)
I tries adding this line:
LIBS += -LC:\GnuWin32\lib
But that didn't solve the problem. WHat is the easiest way to use Quazip under Qt?
Download quazip source from quzip site
Open by Qt Creator quazip.pri
Replace all strings #include "zlib.h" or "#include < zlib.h>"
to
#include <QtZlib/zlib.h>
Build project and run unit tests.

Linking Error using Cmake file to build Qt project in Visual Studio

I am trying to build a qt project using cmake. As a startinng point, I started off by just writing a simple code in qt creator, which contains main.cpp, mainwindow.cpp & mainwindow.h. Below are my CMakeLists.txt configuration.
cmake_minimum_required(VERSION 2.8.11)
project(testproject)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Widgets)
find_package(Qt5Core)
get_target_property(QtCore_location Qt5::Core LOCATION)
# Tell CMake to create the executable
add_executable(testproject WIN32 main.cpp mainwindow.cpp)
# Use the Widgets module from Qt 5.
target_link_libraries(testproject Qt5::Widgets)
Cmake did successfully generate the VS Project files. However, when I try to build them in visual studio, it gives me linking errors. Some of the errors are listed below. I could not post the whole error list as it is too long.
>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QWidget::show(void)" (__imp_?show#QWidget##QEAAXXZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QApplication::QApplication(int &,char * *,int)" (__imp_??0QApplication##QEAA#AEAHPEAPEADH#Z) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QApplication::~QApplication(void)" (__imp_??1QApplication##UEAA#XZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (__imp_?exec#QApplication##SAHXZ) referenced in function main
2>mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QMetaObject::Connection::~Connection(void)" (__imp_??1Connection#QMetaObject##QEAA#XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
2>mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QString::~QString(void)" (__imp_??1QString##QEAA#XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
2>testproject_automoc.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual int __cdecl QMainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (__imp_?qt_metacall#QMainWindow##UEAAHW4Call#QMetaObject##HPEAPEAX#Z) referenced in function "public: virtual int __cdecl MainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#MainWindow##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
2>testproject_automoc.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject" (__imp_?staticMetaObject#QMainWindow##2UQMetaObject##B) referenced in function "void __cdecl `dynamic initializer for 'public: static struct QMetaObject const MainWindow::staticMetaObject''(void)" (??__E?staticMetaObject#MainWindow##2UQMetaObject##B##YAXXZ)
2>MSVCRT.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain referenced in function __tmainCRTStartup
I am using Qt5.3.1, cmake 3.0.2 and visual studio 2012.
In order to fix this issue you have to remove the WIN32 statement from
add_executable(testproject WIN32 main.cpp mainwindow.cpp).
Possible causes:
Check the source file names in the add_executable command inside your CMakeLists.txt for spelling errors. File names in the source list that do not exist will produce the linker error error LNK2019: unresolved external symbol main referenced.
What does your entry point function look like?
WinMain requires add_executable(testproject WIN32 main.cpp mainwindow.cpp)
main requires add_executable(testproject main.cpp mainwindow.cpp)
Note: I suggest using int main(int argc, char *argv[]), as it will be portable to linux, while using WinMain won't.
I just ran into this error, I was using the x64-Debug configuration in Visual Studio, but the variable CMAKE_PREFIX_PATH in CMake was the path of x86 Qt, so the linker tried to link the x86 Qt libraries to the x64 program and the error occurred.
The solution is correcting the Visual Studio configuration and the Qt path to the same architecture.
You have to link against QtCore, you only link against Qt5::Widgets. Add Qt5::Core to your link libraries. At least for me, the CMake files don't include transitive dependencies (so linking against Qt5::Widgets is not enough.)

How to use lacewing project

I am a newbie to C++. I want to use, lacewing-project.org in my C++ project. I read the documentation on how to build it and successfully did it using both DLL - Release and Static Library - Release configurations.
Now I don't know how to use this in my project and how to setup and configure my project? I was following this tutorial, but was stuck in the middle because of missing options in the latest Visual Studio. I use Visual C++ Expression Ed., 2012 on Windows 8
I googled and did the following:
Added the lib file and the lacewing.h file in my project.
Under Visual C++ Project Properties, in the Linker folder -> Input I added the lib file as Additional Dependencies.
I placed the dll in Windows\System32 folder. Will I need to do anything with the dll other than placing it in win32 folder?
My code is from the hello world example given in the documentation.
When I try to compile I get:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1> Source.cpp
1>Source.obj : error LNK2019: unresolved external symbol __imp__lw_version referenced in function "void __cdecl on_get(struct lacewing::_webserver *,struct lacewing::_webserver_request *)" (?on_get##YAXPAU_webserver#lacewing##PAU_webserver_request#2##Z)
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl lacewing::pump_delete(struct lacewing::_pump *)" (__imp_?pump_delete#lacewing##YAXPAU_pump#1##Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: struct lacewing::_error * __thiscall lacewing::_eventpump::start_eventloop(void)" (__imp_?start_eventloop#_eventpump#lacewing##QAEPAU_error#2#XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct lacewing::_eventpump * __cdecl lacewing::eventpump_new(void)" (__imp_?eventpump_new#lacewing##YAPAU_eventpump#1#XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl lacewing::_stream::writef(char const *,...)" (__imp_?writef#_stream#lacewing##QAAXPBDZZ) referenced in function "void __cdecl on_get(struct lacewing::_webserver *,struct lacewing::_webserver_request *)" (?on_get##YAXPAU_webserver#lacewing##PAU_webserver_request#2##Z)
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall lacewing::_webserver::host(long)" (__imp_?host#_webserver#lacewing##QAEXJ#Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall lacewing::_webserver::on_get(void (__cdecl*)(struct lacewing::_webserver *,struct lacewing::_webserver_request *))" (__imp_?on_get#_webserver#lacewing##QAEXP6AXPAU12#PAU_webserver_request#2##Z#Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct lacewing::_webserver * __cdecl lacewing::webserver_new(struct lacewing::_pump *)" (__imp_?webserver_new#lacewing##YAPAU_webserver#1#PAU_pump#1##Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl lacewing::webserver_delete(struct lacewing::_webserver *)" (__imp_?webserver_delete#lacewing##YAXPAU_webserver#1##Z) referenced in function _main
1>C:\Users\Jayarathina\Desktop\New folder (3)\test\Debug\test.exe : fatal error LNK1120: 9 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please help...
To link statically with the library, one must define lw_import. If you don't do this, it will default to __declspec(dllimport):
#ifndef lw_import
#define lw_import __declspec (dllimport)
#endif
which causes the linker to try and pull the functions in from a DLL (which is wrong when statically linking). To do this, you can either add lw_import= to your preprocessor definitions in the project properties, or #define it before including lacewing.h, as so:
#define lw_import
#include <lacewing.h>
As you're statically linking the library, you will also need to link any library dependencies into your own project. You can find the list in the project properties of liblacewing.vcproj itself, but as of 0.5.1 these are ws2_32.lib, mswsock.lib, mpr.lib, secur32.lib and crypt32.lib.

Using Qt Creator with Allegro

I want to get Allegro 4 (MSCV 9) to work with Qt Creator. I am using Windows XP.
I have added these lines to my .pro:
INCLUDEPATH += C:/Allegro/include \
LIBS += C:/Allegro/lib/allegro-4.4.2-md.lib \
C:/Allegro/lib/allegro-4.4.2-monolith-md.lib \
C:/Allegro/lib/allegrogl-0.4.4-md.lib \
C:/Allegro/lib/loadpng-1.5-md.lib \
My code looks like this:
#include <iostream>
#include <allegro.h>
volatile int closedWindow = false;
void CloseButtonCallback() {closedWindow = true;}
END_OF_FUNCTION(CloseButtonCallback())
int main()
{
allegro_init();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 400, 300, 0, 0);
set_window_title("Testing");
LOCK_FUNCTION(closeButtonCallback)
set_close_button_callback(CloseButtonCallback);
while(!closedWindow){}
allegro_exit();
return 0;
}
END_OF_MAIN()
And I keep getting errors like these, when I try to compile:
main.obj:-1: error: LNK2019: unresolved external symbol imp__allegro_exit referenced in function "int __cdecl _mangled_main(void)" (?_mangled_main##YAHXZ)
main.obj:-1: error: LNK2019: unresolved external symbol _imp_set_close_button_callback referenced in function "int __cdecl _mangled_main(void)" (?_mangled_main##YAHXZ)
main.obj:-1: error: LNK2019: unresolved external symbol _imp_set_window_title referenced in function "int __cdecl _mangled_main(void)" (?_mangled_main##YAHXZ)
main.obj:-1: error: LNK2019: unresolved external symbol _imp_set_gfx_mode referenced in function "int __cdecl _mangled_main(void)" (?_mangled_main##YAHXZ)
main.obj:-1: error: LNK2019: unresolved external symbol _imp_set_color_depth referenced in function "int __cdecl _mangled_main(void)" (?_mangled_main##YAHXZ)
main.obj:-1: error: LNK2019: unresolved external symbol __imp_install_allegro_version_check referenced in function "int __cdecl _mangled_main(void)" (?_mangled_main##YAHXZ)
I'm using precompiled Allegro 4.4 for MinGW from http://www.allegro.cc/files/. I merged those three folders (bin, include, lib) inside the archive to MinGW folder which has same folders as well. Then added LIBS += liballegro-4.4.2-mt to .pro file and everything works fine.
Looks like you have to use the GCC-compiled version of Allegro for Qt.
Something like
LIBS +="path_to_allegro_for_mingw/lib/liballegro-4.4.2-md.a"
GCC uses different name mangling and different export libraries, so even while you're using the Allegro dlls, MSVC-compiled .lib wouldn't work.