I have two C++projects(Visual Studio Community Edition 2019) called Game(e) and Tester in a solution map. Tester is a subdirectory of Game. Both projects creates exe files.
The projects Game hast two cpp and two header files(Foo.cpp, Foo.h, Foo2.cpp and Foo2.h).
Foo has two methods add1 and add2. Foo2 has the method plus_one. The method add2 from Foo uses plus_one.
In the project Tester there is a file called Main.cpp. I want to use all the files from Gamer in the project Tester. I have added ".." to "Additional Include Directories" so that I can include Foo.h in Tester\Main.cpp. I have added a reference to Games. The problem is that the linker doesn't like it:
Error LNK2019 unresolved external symbol "public: int __thiscall Foo::add(int,int)" (?add#Foo##QAEHHH#Z) referenced in function _main
Error LNK2019 unresolved external symbol "public: int __thiscall Foo::add2(int,int)" (?add#Foo##QAEHHH#Z) referenced in function _main
Does anybody know how to fix it? One solution is to import both cpp-files into the Tester project. But this solution is not acceptable for me. I am interested in a solution without importing all cpp files. I have uploaded it on github: Github Link
A possible way is to directly add the required object files as linker input for the Tester project.
Open the properties page with a right click on Tester project then Properties
Select Linker then Input
In Additional dependencies add (through Edit...)*:
$(SolutionDir)$(Platform)\$(Configuration)\Foo.obj
$(SolutionDir)$(Platform)\$(Configuration)\Foo2.obj
And everything should work as expected. As the main project is a dependency of the Tester one, VS will build it (if required) before trying to build Tester, and this is enough to ensure that the objects will be up to date.
* The path is the path of the intermediate directory of the main project. As you have chosen to put the main project and the solution in the same folder, the path is directly the solution path. If the main project was in its own folder, you would have to add that folder to the path.
Very new to C++ and the VS IDE, and I'm trying to start working with Dear imGUI and openGL with a hello world function. I've added resources with lib and dll files before for c++ (for openGL), but not something like imGUI which only has .h and .cpp files - feels like I'm struggling to understand how to inform VS of where those are and how they link up.
Here's an example of an error I saw when building:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol _glfwGetWindowSize referenced in function "void __cdecl ImGui_ImplGlfw_NewFrame(void)" (?ImGui_ImplGlfw_NewFrame##YAXXZ) imGUITrial1 ..\Coding\imGUITrial1\imgui_impl_glfw.obj 1
So I opened up imgui_impl_glfw and found the function, and it appears to reference a function declared in glfw3.h...but that function looks like just a declaration, with no definition. I had previously added each folder with these files in it to the project additional directories, so I went ahead and added glfw3.h as a resource to my sources folder in the solution as well. That didn't change anything, so now I'm not sure how to find where the function is actually defined, or if the issue is that I need to somehow tell VS that this file exists in a another nested properties window?
Maybe it will help to have a screenshot of the solution. These are largely files copied from the github that I haven't changed (which I chose from looking through the backend examples), so I don't think I broke anything in the files themselves.
enter image description here
I want to compile a project with Qt in Visual Studio 2010.
I have built all the prerequisite libraries and linked them in project properties.
I have also made the .cpp file from the project.qrc file (rcc) with the command below:
rcc project.qrc -name project -o qrc_project.cpp
Followed the instructions from http://www.qtcentre.org/archive/index.php/t-3425.html .
The project.coo file is produced with the following lines:
int QT_MANGLE_NAMESPACE(qInitResources_project)()
{
QT_PREPEND_NAMESPACE(qRegisterResourceData)
(0x01, qt_resource_struct, qt_resource_name, qt_resource_data);
return 1;
}
Q_CONSTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qInitResources_project))
I have also included the .cpp file in the project.
Although, I get the error below:
> Error 2611 error LNK2019: unresolved external symbol "int __cdecl
> qInitResources_project(void)" (?qInitResources_project##YAHXZ)
> referenced in function main D:\usr\Windows\main.obj project
Have I done something wrong with the rcc? Could anyone please help?
I solved my problem by producing a .rcc file and a .cpp file, both with the name of the project and not with the "qrc_" at the beginning of it. I have also linked both files on my project.
It seems that the compiler could not find the proper file, this is why i had the linking error.
The commands I used in order to produce the files mentioned above are:
rcc -binary <path_to_qrc_file>.qrc -o <path_and_filename>.rcc
rcc <path_to_qrc_file>.qrc -name <project_name> -o <path_and_filename>.cpp
For someone using CMake:
you should enable AUTORCC to auto compile *.qrc files.
add_executable(project project.cpp project.qrc)
// enable autorcc and automoc
set_target_properties(project PROPERTIES AUTOMOC TRUE)
set_target_properties(project PROPERTIES AUTORCC TRUE)
I had found a really trivial workaround if nothing on SO works for you:
In VS, you can simply put your resource(images or icons) not in the library qrc file, but directly in the main project qrc file. Then, in library project, you can refer to the images using the path in main project, and there is even no need to call QT_INIT_RESOURCE.
I think this is not a good practice, but just note down for whoever tried all the solutions online and still not working.
If your project.qrc file is in the same project than the main and the main is not in a namespace, juste add Q_INIT_RESOURCE(project); in your main.
Elsewise define a function with just Q_INIT_RESOURCE(project); in it outside of any namespace in the library you have your project.qrc file.
Call that function in your main.
I inherited a class from QObject :
class Parent: public QObject
{
Q_OBJECT
QObject* cl;
public:
Parent(QObject *parent=0):QObject(parent) {
cl = NULL;
}
QObject* getCl() const {
return cl;
}
void setCl(QObject *obj) {
cl = obj;
}
};
But when I write :
Parent ev;
I get the following error:
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Parent::metaObject(void)const " (?metaObject#Parent##UBEPBUQMetaObject##XZ)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall Parent::qt_metacast(char const *)" (?qt_metacast#Parent##UAEPAXPBD#Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall Parent::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#Parent##UAEHW4Call#QMetaObject##HPAPAX#Z)
You should delete the debug folder of your application and run it again to correct this problem.
If you're using Visual Studio, delete the line Q_OBJECT from the header file, save the file, put Q_OBJECT back into the header file, save the file again. This should generate the moc_* file and should build and link correctly.
I noticed some answers are based on Visual Studio.
This answer is based on Qt Creator.
Unlike the name suggest, Rebuild Project will not wipe out everything and build from scratch. If you recently added QObject (and/or Q_OBJECT) to your class, you'll have to run qmake again, e.g.
Clean Project
Run qmake
Build Project
This is because, by default, qmake only runs when you do significant changes to your solution like adding new source files or modify the .pro file. If you make edits to an existing file, it doesn't know it needs to run qmake.
As a fall back, to brute force Qt to build everything from scratch, delete the Debug or Release folder.
So the issue was I needed the Qt MOC compiler to compile my .h file. This is required for any classes that extend QObject or one of its children. The fix involed (for me) right-clicking on the header file, choosing Properties, and setting the Item Type to "Qt MOC Input", then hitting "Compile" on the header, and then adding the resulting moc_myfilename.cpp file to my project.
I added cpp/ui files to my project manually, but forgot to add the header file explicitly as header file. Now when compiling I got a similar error message as above and the moc_*.cpp file(s) were not generated in the debug (or release) directory of the build. That was not such an obvious mistake, qmake did not complain and other than the linker message I got no errors.
So if anyone encounters the same problem again (or makes the same copy & pase mistake): make sure the header files have also been added to your project file
If your moc files are generated in the visual studio project try to include them into project if they are not included into project then rebuild.
I had the same problem in Visual Studio, and solved it by taking the following steps:
Right-click header file in solution Explorer
Properties
Change "Item Type" to "Custom Build Tool"
Then in the Custom Build Tool configuration:
Go to General
set "Command Line" to:
"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" "-fStdAfx.h" "-f../../../src/FileName.h" -DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DWIN32_LEAN_AND_MEAN -DDIS_VERSION=7 -D_MATH_DEFINES_DEFINED "-I.\SFML_STATIC" "-I.\GeneratedFiles" "-I." "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtNetwork"
set "Outputs" to:
.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp
set "Additional Dependencies" to:
$(QTDIR)\bin\moc.exe;%(FullPath)
Your exact values may be different. They are usually applied via a Qt plugin.
I had this problem with Visual Studio 2012 when I had a Q_OBJECT class definition in my cpp file. Moving the class definition to the header file resolved the issue.
It looks like it should be possible to support Q_OBJECT class in cpp file by adding the cpp file to moc but I did not try this.
I use CMake to manage Qt projects and the new Q_OBJECT needs to be added under the QT4_WRAP_CPP call. This will generate the moc_*.cxx for inclusion in the project and clean up the unresolved externals.
My problem was that one of my files that used a Qt macro didn't get moc'ed. I found out, that the Qt Plugin for Visual Studio doesn't recognize the Q_NAMESPACE macro and therefore doesn't add the file to the moc'ing list.
So I used the solution from this answer to add the file to the mic'ing list:
You should find a .h file which has successfully generated
"moc_*", and copy all the contents in "Custom Build Tool -> General"
to the new .h file setting page.
Be careful with the different options for Debug and Release-Mode.
After that, build your project.
Build it once each in Debug and Release-Mode
Finally, add the generated "moc_*" file to your project.
Now, "moc_filename.cpp" should be in Generated Files\Debug and Generated Files\Release.
Right click on each of them and change thair properties:
The file in Debug: Change configuration to Release and then change General->Excluded from build to yes.
The file in Release: Change configuration to Debug and then change General->Excluded from build to yes.
Faced this issue in case of chained CMake targets. Turned out I had to enable CMAKE_AUTOMOC even in targets that didn't use Qt directly (transitively). Also turned out that CMAKE_AUTOMOC can't be used w/o find_package(QtX) in same CMakeLists.txt or CMakeLists.txt of parent.
In my case (using QtAdd-in with VS2012 and Qt v4.8.4) none of the above suggestions worked.
For some reason VS could not generate proper moc files (build output: No relevant classes found. No output generated.) and when I compiled relevant headers by hand (setting qt moc as a compiler and clicking 'Compile') it produced empty moc file.
What did work was to compile all necessary mocs from command line (moc -o moc_SomeClass.cpp SomeClass.h) and then replace the wrong ones in GeneratedFiles folder.
This is only workaround (and not a convenient one for a big project) to have your project build succesfully, but does not really explain strange VS/QtAdd-in behaviour.
Using QtAdd-in with VS2010 i realized the moc_*.cpp files were updated in the GeneratedFiles/Debug folder although i was in release mode. Copying the files into the Release folder worked for me.
I know that this is a very old question, but it seems to be still interesting (I've been here at least 4 or 5 times in the last months) and seems like I found another reason for which is possible to get this error.
In my case in the header file I wrongly typed:
#include "MyClass.h""
Only after inspecting the whole output I found out that at that line the compiler was emitting a warning.
Now that I removed the additional quotation mark my QObject compiles perfectly!
I've encountered this problem with the use of a "private class" in Qt when employing the "PIMPL" (private implementation) programming pattern. Qt uses this model all through out their source code. I have come to really like it myself.
This technique involves the use of a "private" forward-declared class in a public header file, which will be be used by the "public" class (i.e. it's "parent"). The parent then has a pointer to an instance of the private class as a data member.
The "private" class is defined entirely within the cpp file for the public one. There is NO header file for the private class.
All of the "dirty work" is done with that private class. This hides all of the implementation of your public class including every other private member typically (both data and functions).
I highly recommend learning about the PIMPL pattern - especially if you are going ever read the internal Qt source.
Without explaining that coding style further, here's the point as it relates to this question... To get the Q_OBJECT macro to work inside the cpp for the "private" class to be QObject which can use signals/slot etc., you needed to explicitly include the .moc to the public class inside the cpp:
#include "MyPublicClass.moc"
You can ignore any IDE warnings about this line.
I'm not sure if it matters exactly off hand, but that inclusion I always see done AFTER the private class definition, rather than at the top of the cpp (like includes are typically placed). So, the cpp layout goes like this:
"Normal" includes are defined.
The private class is defined.
The moc for the public class is #included.
The public class implementation is defined.
Visual Studio 2017.
I've added file to already set up Qt project and got this error. How I fixed it:
Right click on the header in Solution Explorer
Properties... -> Configuration Properties -> General -> Item Type
Change from C/C++ Header to Qt Meta-Object Compiler (moc)
voila :)
This happened to me recently when switching from MingW to MSVC. I had a prototyped class/struct listed as a class, and MingW didn't mind.
MSVC definitely sees a difference between class and struct when prototyping is concerned.
Hope that helps someone else one day.
In my case, none of the above worked but it was totally my mistake.
I had overrided virtual functions in .h file (declared them) but had never defined them in .cpp :)
Either answer works for me in the VS 2013 environment. I eventually solve the problem by removing the .h/.cpp from project, and adding it back.
I am working in VS2015 with an integrated Perforce p4v client.
In my case Perforce tried to add moc file to a depo, when I reverted this operation, Perforce removed this moc file from project and deleted it.
The file was recreated after the next compilation, but it wasn't included in project, I have to add it manually to Generated files, when I finally understood what was the problem.
I have the same problem, my solution was the encoding( my file with "UTF16LE BOM" can't generate with moc.exe ) , y create another file with ASCII enconding and it work.
HxD HexEditor can help you to see the codification.
For me, this is the cause: some header or source file not included in QT's project file
In my case I had the .h and the .cpp file for the problematic QObject ancestor in subfolders of the project. When I moved them next to the CMakeLists.txt (project root folder) it linked successfully. I'm probably missing some CMake command to include mocs fot files in subdirectories.
when I removed Q_OBJECT it works fine.
I'm using Clion + CMake + MSVC/14.31.31103.
Is it because Q_OBJECT no longer needed after these?
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
For Visual Studio 2022 (with Qt VS Tools extention)
In Solution Explorer right click on the affected header file and select Properties.
Under "Configurations Properties -> General -> Item Type" select the option "Qt Meta-Object Compiler (moc)".
Then rebuild the project.
In Qt Creator
It turned out I had this error because I added Q_OBJECT afterwards in the .h file.
I managed to fix it after removing and adding the file back into the project.
Happy coding...
I solved my problem by adding this to my header file :
#ifndef MYCLASSNAME_H
#define MYCLASSNAME_H
... // all the header file content.
#endif
I am receiving the following linker error when I build my application.
HIMyClass.obj:: error:
unresolved external symbol "public:
virtual struct QMetaObject const *
__thiscall CHIMyClass::metaObject(void)const
"
(?metaObject#CHIMyClass##UBEPBUQMetaObject##XZ)
File not found : HIMyClass.obj
HIMyClass.obj:: error:
unresolved external symbol "public:
virtual void * __thiscall
CHIMyClass::qt_metacast(char
const *)"
(?qt_metacast#CHIMyClass##UAEPAXPBD#Z) File not found : HIMyClass.obj
HIMyClass.obj:: error:
unresolved external symbol "public:
virtual int __thiscall
CHIMyClass::qt_metacall(enum
QMetaObject::Call,int,void * *)"
(?qt_metacall#CHIMyClass##UAEHW4Call#QMetaObject##HPAPAX#Z) File not found : HIMyClass.obj
My class declaration is like
class CHIMyClass:public QDialog
{
Q_OBJECT
....
};
When I comment Q_OBJECT the linker error goes off (and obviously I am not able to use signals and slots). I am using Qt Creator as IDE and Qt 4.5.3. When I give Rebuild All it's definite that QMake will be called. I guess that, its the generation of moc_* files is where the problem lies. I am using Windows XP and cl as the compiler.
What might be the reason behind this linker error?
Such errors usually mean that you haven't added the header of your class to "HEADERS" variable in pro file (meta object compiler generates moc_ files only for headers listed in this variable). Remember to run qmake after you change .pro file!
I had a similar problem and it was solved using andref's feedback. Within QT Creator I simply:
Build/Clean all
Build/Run qmake
Build/Run
Whenever you change QObject inheritance be sure to do a clean, qmake then build. The qmake is important since it updates moc* files for any new Qt changes in your .h files including QObject inheritance, ie Q_OBJECT. In fact, in some cases, you may even be able to simply do qmake then build for an incremental build.
Check in the file MakeFile.debug and maybe HIMyClass don't exists.
I just rename MakeFile.debug, Clean the Project and Rebuild All and it compiles.
I had the same problem but in my case it wasn't enough to clean -> build. So I had to delete manually all files created in build process (Mekefiles, ui descriptionns in cpp, and generally whole directory created by build process) and only then build succeded.
Check that the necessary Qt config options are present in the pro file (QT += core gui at least. Also try manually deleting everything built/created in the build directory. It sometimes happens that moc fails to run for some reason.
You can also try running the moc command yourself, and see what it outputs (you can find the command line in the tab "Compile output" in QtCreator.
UPDATE: this related problem seems to suggest you don't define QT_DLL when compiling. Can you try a fresh and new simple QtCreator project (with a widget that subclasses mainwindow for example) and try that. It should contain a Q_OBJECT header automatically and try to compare the .pro files and compiler output.
on my osx box this was due to missing moc* files.
i fixed this by removing the bom from my utf-8 encoded .pro file.
i will file a bug with qt.
error for goggle searches...
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for MainWindow", referenced from:
MainWindow::MainWindow(QWidget*)in mainwindow.o
I found another possible cause and solution to this error
This error will also occur if one has declared the slot in .h file but has not defined its body in the implementation
I had this problem.
Verify whether there is a description of the implementation of the slot in .cpp file.
I had removed #include "main.moc" from my main file, and forgot to re-add it... That was a fun time-waster!
Answers from #chalup and #ierax helped me. I had to close the Qt creator and open it again though for qmake to take effect. I followed these steps:
1. Moved the class definition to a header file.
2. Added header file to project and ensured it is listed against HEADERS += \ list in .pro file.
3. Clean-all
4. close QtCreator (on Windows 10)
5. Delete Makefiles from the project directory
6. Open QtCreator and open the project.
7. Qmake to ensure makefiles are generated.
8. Rebuild-all
These steps helped me solve this issue - I struggled for over an hour with various other answers and methods nothing worked. Before you run qmake ensure you delete makefiles and close QtCreator (applicable atleast on windows 10).