I'm facing a weird issue here. I have my VS2012 project all set up, working properly. But when i tried the simple task of adding a method to one of my classes, it won't link correctly, i get
error LNK2019: unresolved external symbol "public: void __thiscall Camera::calcularDirecao(class GLFWwindow *)" (?calcularDirecao#Camera##QAEXPAVGLFWwindow###Z)
Here's my Camera class:
class Camera {
public:
Camera() { ... inline constructor ... }
~Camera() {}
... other methods ( which link fine ) ...
void calcularDirecao(GLFWwindow *);
};
And in my implementation file i have
void Camera::calcularDirecao(GLFWwindow *janela) {
... code ...
}
... other methods ...
I already tried rebuilding and all, with no success. Thanks in advance.
Recreate your project from scratch, check if it works or not. After that compare your vcxproj files. They are text files. Any text compare tool will work. Their difference may tell a lot.
Also try to view your source file in a hex editor. Look for any non ASCII symbols in your troubled method and around. Such symbols might be not displayed in the IDE viewer but still may confuse the compiler.
I doubt that your GLFWwindow definition is getting overridden by some other file included in your .cpp.
Related
So, I've tried to run the simplest C++ program imaginable in Visual Studio 2022:
main.cpp:
#include "TestClass.h"
int main() {
TestClass().testMethod();
}
TestClass.h:
#pragma once
class TestClass {
public:
void testMethod();
};
TestClass.cpp:
#include "TestClass.h"
inline void TestClass::testMethod() {
}
But for some reason, I get nothing but a linker error:
error LNK2019: unresolved external symbol "public: void __cdecl TestClass::testMethod(void)" (?testMethod#TestClass##QEAAXXZ) referenced in function main
I know that there are tons of questions on Stack Overflow discussing that specific error, but I wasn't able to find anything that applied to my situation, except for this one, which doesn't have an answer.
All files are included in the project (everything was generated in Visual Studio), I do not get any warnings from IntelliSense, and every file on its own compiles just fine (using Ctrl+F7)
I have no clue what is going on and would appreciate any help.
In C++ inline functions must have their body present in every translation unit from which they are called.
Removing inline doesn't change anything
Try to test it. I'm not the only one who proves that inline causes the lnk error. As
Sedenion says, it's a usage error.
I recommend reporting this wrong behavior to Developer Community and posting the link in this thread.
I have a library that uses QwtPlotMagnifier, amongst other Qwt classes. I decided to subclass QwtPlotMagnifier so that I can emit a signal when the plot is rescaled.
The library (mylib.lib) compiles, but the application using it is now complaining about an unresolved external related to the moc output of QwtPlotMagnifier.
I am linking qwt statically; so the requirement to have the preprocessor directive QWT_DLL in the lowest level library doesn't apply here.
Here's the error (the subclass is called PlotMagnifier):
mylib.lib(moc_PlotMagnifier.obj) : error LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlotMagnifier::staticMetaObject" (?staticMetaObject#QwtPlotMagnifier##2UQMetaObject##B)
Nothing special about the subclass declaration:
#pragma once
#include "qwt_plot_magnifier.h"
/**
subclass of QwtPlotMagnifier to provide a signal when zooming is complete
*/
class PlotMagnifier : public QwtPlotMagnifier
{
Q_OBJECT
public:
explicit PlotMagnifier(QWidget *w);
virtual ~PlotMagnifier();
signals:
void rescaled();
protected:
virtual void rescale(double factor);
};
I'm on visual studio 2013 fwiw. My application still includes qwtd.lib as it always has. This has got to be a stupid mistake on my part.. kickstart my brain please, someone!
Add this line to .pro file to give the compiler a hint for an external symbol:
DEFINES += QWT_DLL
In the file qwt_global.h have the macro. Without this macro, the compiler will think this is an internal symbol.
Check, if you have all needed includes in you Visual Studio project.
C/C++ / Additional Include Directories
Here should be a path to <qwt_dir\include> be
Linker / General / Additional Library Directories
Here should be a path to <qwt_dir\lib> be
Linker / Input
Should include qwtd.lib (for debug configuration) and qwt.lib (for release)
Also, check that you have those entries in Release and Debug configurations, it is easy to configure only Debug, while working on Release configuration.
Also, check that you have moc_* file (something like moc_plotmagnifier.cpp) for your PlotMagnifier under Generated Files in your project view, sometimes Qt addin fails to add them.
I had a solution named fun.sln with a project called fun.vcxproj.
I created a whole bunch of name spaces ready to be used.
I made another project called no_more_fun.vcxproj.
I added the includes directory for fun.vcxproj to the configuration of no_more_fun.vcxproj.
I added this to no_more_fun.cpp
#include "candy.h"
void main(void)
{
candy::get();
return;
}
candy.h is in the default directory for fun.vcxproj(which was added to the config)
But I get...
LNK2001 unresolved external symbol "int __cdecl candy::get(unsigned long)" (?get#candy##YAHK#Z) .....
Visual Studio shows no error before compiling.
The "candy" namespace works fine in the "fun" project so idn...
Is there a guide or something so that i can understand how i can go about sharing code efficiently among different projects within ONE solution?
This is a linker error. You didn't get any error at compile time, because the compiler found the candy::get() method in candy.h header, but the implementation (which I suppose is in a candy.cpp file) is not found by the linker.
Just add candy.cpp too to no_more_fun.vcxproj.
ps. I didn't observe but in the error message you can see that the function waits a parameter.
call it like this:
unsigned long foo = 0;
candy::get(foo);
This is going to sounds stupid but...i just dragged the files in to visual studio so that the no_more_fun project had the "files" in its "directory" too.
wow... I shouldn't need to do that...Am I wrong?(sarcasm).
I am currently trying integrate the library Qt in my C++ project. First I thought I could use it like any other library. I downloaded it, added the include path and libs to my VS project and tried a small sample of code that just creates a simple window with a text edit and a button. This worked and the small user interface was displayed.
Now I started to try implementing a class derived from a QWidget and realised, that it might not be that simple. The class I tried creating looked like this:
#include <QtWidgets\qstylepainter.h>
#include <QtWidgets\qwidget.h>
class MapRenderer : public QWidget{
Q_OBJECT
public:
MapRenderer(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent* event);
};
And the corresponding cpp file:
MapRenderer::MapRenderer(QWidget *parent) : QWidget(parent){
}
void MapRenderer::paintEvent(QPaintEvent *event){
QPainter p(this);
p.setPen(QPen(Qt::black, 3));
p.drawPoint(QPointF(10, 20));
}
Now as I tried compiling this I started getting linker three linker errors that looked like this:
error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl MapRenderer::metaObject(void)const " (?metaObject#MapRenderer##UEBAPEBUQMetaObject##XZ) G:\Documents\C++ Projects\HistoricalBorders\MapRenderer.obj
error LNK2001: unresolved external symbol "public: virtual void * __cdecl MapRenderer::qt_metacast(char const *)" (?qt_metacast#MapRenderer##UEAAPEAXPEBD#Z)
error LNK2001: unresolved external symbol "public: virtual int __cdecl MapRenderer::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#MapRenderer##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
So I had a look around on the internet and found out that Qt apparently requires a far more complicated build process than a 'normal' c++ project with 'normal' libraries. I read of qmake and a Visual Studio Qt Add-In that can be used to create Qt-Projects in Visual Studio.
So I gave it a try and installed the Visual Studio Add-In and specified my Qt-version's root directory in its settings. Afterwards my project still wouldn't compile, but the only error message then was:
error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
Now I don't really get this, because my Qt library is 64bit and my project configuration also is 64bit as well as all the other libraries I am using. Furthermore, before installing the add-in I didn't have this problem. I have to say, my project is a 'normal' Visual Studio C++ console application. I saw that since I installed the VS Add-in it is also possible to choose 'Qt-console application' when creating a new project in Visual Studio. I also gave this a try but it didn't even compile after creating it. I also don't really know what the difference of a "Qt-project" to a normal VS-project is, except that the Qt libraries are included by default.
The other thing I have to mention is that I am using cmake to create my VS project file because I use version control (mercurial) and multiple people shall work on the project.
At the moment I am totally confused on how to get this library to work.
Now what is my question? Honestly, I don't really know. What I'm asking myself is:
Where does the architecture mismatch linker error come from?
Can I create a normal VS console application and get it to work with Qt?
What's the difference of a 'Qt-console application" to a normal VS project?
Is it possible to generate a Qt-compatible VS project with cmake?
EDIT: Now I uninstalled the Add-In again, reinstalled it but am now getting the old linker errors again (no longer the architecture mismatch). I have no idea why.
EDIT: Ok, some clarification. Yes, I want to use cmake. I also already have a cmakelists.txt and a findQt.cmake that I wrote myself and that asks you for your Qt root directory and then adds the necessary include paths and library paths to the project. This also works so far. E.g. the following code compiles without problems and shows an interface:
QApplication app(argc, argv);
QTextEdit *textEdit = new QTextEdit;
QPushButton *quitButton = new QPushButton("&Quit");
QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textEdit);
layout->addWidget(quitButton);
QWidget window;
window.setLayout(layout);
window.show();
app.exec();
Now if I try to create a class as shown above (the MapRenderer) I get the linker errors also shown above. Now I thought I am doing something wrong. My only question is what. From your comment, drescherjm, I see that I problably missed the moc-step (sorry but I have no idea what exactly that is).
If you have Qt project file (.pro) (or can get it somehow), you can run
cd <pro_folder>
qmake -tp vc
to (re-)generate Visual Studio project file with Qt build process properly set up, including
moc (meta object compiler; that's what you are missing now),
uic (processing designer's files)
rcc (resource compiler).
All generated files will be compiled and linked properly.
You don't even need AddIn for that.
I've got a problem with my code. It basically looks like this:
someclass.hpp:
class SomeClass
{
public:
SomeClass();
~SomeClass();
//... some other methods
};
someclass.cpp:
#include "SomeClass.hpp"
SomeClass::SomeClass()
{
}
SomeClass::~SomeClass()
{
}
SomeClass.cpp doesn't include anything else.
The constructors are actually empty, too. I just don't want to leave them undefined or leave the standard constructor because I'm probably going to need it later.
SomeClass.hpp is actually a gamestate class which is included in only one place:
main.cpp
#include "SomeClass.hpp"
int main()
{
DoSomethingWithGamestate(new SomeClass());
return 0;
}
And then the project consists of a lot of other files, all of which are independent from SomeClass, though.
The problem is that whenever I change anything in the code, no matter in which file, I have to recompile the entire solution because if I only compile the changes the linker throws this:
error LNK2001: unresolved external symbol "public: __thiscall SomeClass::SomeClass(void) (??blablaSomeClassblabla)
Obviously, there's something weird going on here, since SomeClass() is clearly defined in someclass.cpp.
What could be the source of this?
In addition to what #Nawaz says, it's likely your build files are out of sync with the linker so that when Visual Studio goes to re-link, it can't because the files are out of sync. Just re-build and be done with it.
By the way, it may even be a bug in Visual Studio 2010 as seen here.