I'm trying to compile some relatively simple code in Qt C++ but I'm getting the following linker error and can't figure out what the issue is.
main.obj:-1: erreur : LNK2019: unresolved external symbol
"class QUuid __cdecl uuid::fromString(class QString const &)"
(?fromString#uuid##YA?AVQUuid##ABVQString###Z) referenced in function _main
Other files compiles fine but somehow these two (uuid.h/uuid.cpp) have this error.
Also if that's relevant, if I right-click on "uuid.cpp" in Qt Editor and select "Compile", I get this error, which also doesn't happen for other cpp files:
Start : "C:\Qt\Tools\QtCreator\bin\jom.exe" -f Makefile.Debug debug/uuid.obj
Error: Target debug/uuid.obj doesn't exist.
Any idea what the issue could be?
uuid.h
#ifndef UUID_H
#define UUID_H
#include <stable.h>
namespace uuid {
QUuid fromString(const QString& s);
}
#endif // UUID_H
uuid.cpp
#include <stable.h>
#include "uuid.h"
namespace uuid {
QUuid fromString(const QString& s) {
// ....
}
}
main.cpp
#include <stable.h>
#include "uuid.h"
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QUuid test = uuid::fromString("f601c97e494a46a7b393bb0366e679a1");
return app.exec();
}
Removing the build folder, and in particular the make files, fixed the issue. Running the Clean command from Qt Creator was not enough.
Related
I've been trying to install and properly link bit7z into my C++ code as I have to do a task for my internship which concludes in automatically zipping a certain directory and sending the zip-file out as an email. Right now the email is not interesting to me as I can't even get the base program. I just keep getting the Linker Error 2019 and I don't know what to do anymore. I'll provide as much info as I can.
I use Visual Studio 2019.
My .pro file:
TEMPLATE = app
TARGET = aixLogger
DESTDIR = ./Debug
CONFIG += debug console
DEPENDPATH += .
MOC_DIR += .
OBJECTS_DIR += debug
UI_DIR += GeneratedFiles
RCC_DIR += GeneratedFiles
LIBS += -D:/local/aretz/Programmierung/git-workplace/aixLogger/Dependencies/bit7z/lib -lbit7z
INCLUDEPATH += D:/local/aretz/Programmierung/git-workplace/aixLogger/Dependencies/bit7z/include
include(aixLogger.pri)
My .h
#pragma once
#include <qwidget.h>
#include <qobject.h>
#include <bit7z.hpp>
class AIXLogger : public QWidget
{
Q_OBJECT
public slots:
public:
void CompressDir();
void Execute();
};
My .cpp
#include <QCoreApplication>
#include <string>
#include <iostream>
#include <filesystem>
#include <bit7z.hpp>
#include "main.h"
#include "bitcompressor.hpp"
namespace fs = std::filesystem;
using namespace bit7z;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string path = "C:/Users/aretz/Downloads/test";
for (const auto& entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
//return a.exec();
}
void AIXLogger::CompressDir() {
try {
Bit7zLibrary lib{ L"C:/Program Files/7-Zip/7z.dll" };
//BitCompressor compressor{ lib, BitFormat::Zip };
std::vector< std::wstring > files = { L"aretz/downloads/test/test1.txt", L"aretz/downloads/test/test1.txt" };
//Zip Archiv erstellen
//compressor.compress(files, L"output_archive.zip");
//Directory zippen
//compressor.compressDirectory(L"dir/path/", L"dir_archive.zip");
}
catch (const BitException& ex) {
//irgendwas mit &ex machen
}
}
void AIXLogger::Execute() {
CompressDir();
}
I'm also adding pictures of the properties I changed.
Additional DependenciesAdditional Library DirectoriesAdditional Include Directories
EDIT: Here is the actual Error I'm getting with just the line "Bit7zLibrary lib {L"C:/Program Files/7-Zip/7z.dll" };:
Error LNK2019 unresolved external symbol "public: __thiscall bit7z::Bit7zLibrary::Bit7zLibrary(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" (??0Bit7zLibrary#bit7z##QAE#ABV?$basic_string#_WU?$char_traits#_W#std##V?$allocator#_W#2##std###Z) referenced in function "public: void __thiscall AIXLogger::CompressDir(void)" (?CompressDir#AIXLogger##QAEXXZ) aixLogger D:\local\aretz\Programmierung\git-workplace\aixLogger\main.obj 1
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: virtual __thiscall bit7z::Bit7zLibrary::~Bit7zLibrary(void)" (??1Bit7zLibrary#bit7z##UAE#XZ) referenced in function "public: void __thiscall AIXLogger::CompressDir(void)" (?CompressDir#AIXLogger##QAEXXZ) aixLogger D:\local\aretz\Programmierung\git-workplace\aixLogger\main.obj 1
The problem you're having is that the linker cannot find the bit7z static library.
By default, the bit7z library is built to bit7z\bin\$(PlatformShortName)\, where $(PlatformShortName) is either x86 or x64 according to your target architecture.
However, you specified a different library directory (bit7z\lib\), which is wrong (unless you changed the directory where you output the built library).
You can fix it by changing the path to $(SolutionDir)Dependencies\bit7z\bin\$(PlatformShortName)\.
Also, please note that, on x86, the default library name is just bit7z.lib, while on x64 is bit7z64.lib.
I am receiving a link error when trying to compile my "hello world" Qt program.
Error 1 error LNK2001: unresolved external symbol "__declspec(dllimport) protected: virtual void __thiscall QObject::disconnectNotify(char const *)" (__imp_?disconnectNotify#QObject##MAEXPBD#Z) C:\Users\Matthew\documents\visual studio 2010\Projects\FBIDecryptor\FBIDecryptor\main.obj FBIDecryptor
I believe I am missing a library input, but I don't know which library.
This is the code I am trying to execute:
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt");
label->show();
return app.exec();
}
You are missing reference to QtCore, QtWidget and other Core library modules. Adding the Qt4.8/bin path to the environment variable should fix this.
Found a useful set up guide here
I am trying to compile a very simple CMake project using Visual Studio 2013, however I am getting the following error upon trying to compile it:
error LNK1120: 1 unresolved externals cmake_issue\build\Debug\cmake_issue.exe 1 1 cmake_issue
error LNK2001: unresolved external symbol "public: static int Other::value" (?value#Other##2HA) cmake_issue\build\test.obj cmake_issue
I have a base directory with the following CMakeLists.txt in:
project(cmake_issue)
add_subdirectory(other)
add_executable(cmake_issue src/test.cc)
target_link_libraries(cmake_issue other)
And the contents of src/test.cc:
#include <cstdio>
#include "other/other.h"
int main(int argc, char *argv[]) {
printf("value = %d\n", Other::value);
return 0;
}
And a subdirectory called other with the following CMakeLists.txt in:
add_library(other SHARED src/other.cc)
target_include_directories(other PUBLIC include)
target_link_libraries(other)
And the contents of other/include/other/other.h:
#ifndef _OTHER_H_
#define _OTHER_H_
class __declspec(dllexport) Other {
public:
static int value;
};
#endif
And the contents of other/src/other.cc:
#include "other/other.h"
int Other::value = 30;
If I build the project with cmake and then open the generated sln in Visual Studio, both of the projects appear in the Solution Explorer.
If I right click and build other, it builds fine. However if I try and build cmake_issue, I get the errors above. It looks like the cmake_issue solution is not using the other.dll (or other.lib) files being generated when compiling the other solution.
I can upload a zip of the source if it is needed.
Okay, the problem is not on CMake side, but with C++. When you used dllexport'ed class in your executable, its definintion should read class __declspec(dllimport) Other. This code works fine, for example:
#include <cstdio>
class __declspec(dllimport) Other {
public:
static int value;
int a();
};
int main(int argc, char *argv[]) {
printf("value = %d\n", Other::value);
return 0;
}
Here's a link with complete solution: https://social.msdn.microsoft.com/Forums/en-US/6c43599d-6d9d-4709-abf5-4d1e3f5e4fc9/exporting-static-class-members
I may be missing something obvious, but I can't get rid of a linking error in my Qt user interface.
I've isolated the part that's causing trouble. Basically, I'm implementing a subclass of QGraphicsView to display an interactive overhead map. For some reason, I can't get the constructor to be resolved.
OverheadMap.h :
#ifndef OVERHEADMAP_H
#define OVERHEADMAP_H
#include <QGraphicsView>
class OverheadMap : public QGraphicsView {
Q_OBJECT
public:
OverheadMap();
};
#endif // OVERHEADMAP_H
OverheadMap.cpp :
#include "OverheadMap.h"
OverheadMap::OverheadMap() {
// Body
}
main.cpp :
#include "OverheadMap.h"
int main(int argc, char *argv[])
{
OverheadMap *map = new OverheadMap();
}
LNK2019 :
main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall OverheadMap::OverheadMap(void)" (??0OverheadMap##QAE#XZ) referenced in function _main
I can without any trouble use QtCreator's auto-completion with OverheadMap, and I have done a similar subclass implementation of a QFrame that's working, so I doubt there's a syntax error here.
What am I missing?
Thanks.
This code works just fine for me.
So, the solution is that you have to re-run qmake because based on your comments, you modified the project structure without telling that to qmake.
You need to call your base class's constructor
OverheadMap::OverheadMap() : QGraphicsView()
{
// Body
}
I tried to write a simple Qt application like this:
main.cpp:
#include <QApplication>
class MyApp : public QApplication {
Q_OBJECT
public:
MyApp(int argc, char* argv[]);
};
MyApp::MyApp(int argc, char* argv[]) :
QApplication(argc,argv) {
}
int main(int argc, char* argv[]) {
MyApp app(argc,argv);
return app.exec();
}
But when I tried to compile and link it with Qt Creator 2.3.1 (Qt 4.7.4) I get 3 "unresolved external symbol" errors:
main.obj:-1: error:
LNK2001: unresolved external symbol
""public: virtual struct QMetaObject const * __thiscall MyApp::metaObject(void)const "
(?metaObject#MyApp##UBEPBUQMetaObject##XZ)".
main.obj:-1: error:
LNK2001: unresolved external symbol
""public: virtual void * __thiscall MyApp::qt_metacast(char const*)"
(?qt_metacast#MyApp##UAEPAXPBD#Z)".
main.obj:-1: error:
LNK2001: unresolved external symbol
""public: virtual int __thiscall MyApp::qt_metacall(enum QMetaObject::Call,int,void * *)"
(?qt_metacall#MyApp##UAEHW4Call#QMetaObject##HPAPAX#Z)".
I think they are somehow related to the MetaObjectCompiler of Qt, but I can't figure out a solution.
I know it's not considered good programming style in c++ to put declarations and definitions in one file, but that's not the point here. In my opinion it should be possible since there is nothing syntactically wrong here.
Use the code below, and make sure to run qmake (Build > Run qmake) before building.
#include <QApplication>
class MyApp : public QApplication {
Q_OBJECT
public:
MyApp(int argc, char* argv[]);
};
MyApp::MyApp(int argc, char* argv[]) :
QApplication(argc,argv) {
}
int main(int argc, char* argv[]) {
MyApp app(argc,argv);
return app.exec();
}
#include "main.moc"
Explanation: When you include the Q_OBJECT macro, this signals Qt to do a bunch of stuff that is not standard C++, such as signals and slots. It does this by running moc, which in large part is a code generator. Running qmake creates the metadata so that when your project is built, it knows which files to moc, etc.
I think you need to moc the file and include the resulting main.moc at the bottom.
I think this has something to do with QMake. It's not that the executable app can't see the exported DLL class. It's that the obj file for the class doesn't exist. Running QMake from the QT Creator Build menu and then building seems to work.
Why does this Simple Qt Application not link
I've just met the same problem, and it has been solved by changing the Character set of my header from Unicode to ANSI.