Q_OBJECT Problem in Visual C++ - visual-studio

I have received a Visual C++ QT based project from our customer. I have installed QT libraries then I have compiled the project. The Project was compiled without any problems.
Now, I need to include a new additional GUI interface with the existing project. I have created a GUI in QT designer then saved in the source directory of VC++ project. Then I have written .h and .cpp file for new GUI and could call this interface. Now I need to include SIGNALS and SLOTS, when I include Q_OBJECT in .h file. I have compilation error.
These are the errors, please help me to solve this problem:
unresolved external symbol "public: virtual struct QMetaObject const * __thiscall BetaLineServer::metaObject(void)const " (?metaObject)
unresolved external symbol "public: virtual void * __thiscall BetaLineServer::qt_metacast(char const *)"
unresolved external symbol "public: virtual int __thiscall BetaLineServer::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#

Any time you add a Q_OBJECT macro to a class you need to be sure to rerun qmake and then compile.

All you need is to compile the header files with moc that contain Q_OBJECT macro. And how to do that? here it is
You can manually type commands for compiling moc or uic files OR
-> Install Qt-VS addin. http://qt.nokia.com/downloads/visual-studio-add-in
-> Now, open visual studio and create a new Qt project as described in here and then
-> Right click on a header file that contains the Q_OBJECT macro you should find something like below. Copy these commands into your project.
-> Replace the header file name with yours in the 'Command Line' command
-> compile once and that should generate moc_xxxxx.cpp files, include them in your project.
For future reference, if you create a Qt project in VS using this plugin you should have these commands automatically added

You probably just need to add your .ui, .cpp and .h files to the project file.
Qt will run 'moc' on the ui file if it's listed there, which creates source code to supply your missing symbols.

Exclude .h file from the project and include it again - moc_.cpp will appear in the "Generated Files", and linking errors are disapeared.

Related

LNK2019 error when building a Qt project in visual studio

I made a visual studio project using CMake, and when I try to compile it I get these errors. The error seem to be related to QDomDocument objects only, I dont get any link error related to other Qt classes;
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QDomNode::~QDomNode(void)" (__imp_??1QDomNode##QEAA#XZ) referenced in function "private: void __cdecl Graph::readFromFile(class QString)" (?readFromFile#Graph##AEAAXVQString###Z) Gps_test C:\Users\Cristi\Desktop\an2 sem 2\QT\gps_test1\build\graph.obj 1
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "__declspec(dllimport) public: class QDomNodeList __cdecl QDomNode::childNodes(void)const " (__imp_?childNodes#QDomNode##QEBA?AVQDomNodeList##XZ) referenced in function "private: void __cdecl Graph::readFromFile(class QString)" (?readFromFile#Graph##AEAAXVQString###Z) Gps_test C:\Users\Cristi\Desktop\an2 sem 2\QT\gps_test1\build\graph.obj 1
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "__declspec(dllimport) public: bool __cdecl QDomDocument::setContent(class QIODevice *,class QString *,int *,int *)" (__imp_?setContent#QDomDocument##QEAA_NPEAVQIODevice##PEAVQString##PEAH2#Z) referenced in function "private: void __cdecl Graph::readFromFile(class QString)" (?readFromFile#Graph##AEAAXVQString###Z) Gps_test C:\Users\Cristi\Desktop\an2 sem 2\QT\gps_test1\build\graph.obj 1
This is my CMake file for the source files:
# Defines the minimum CMake version required for the CMakeLists.txt file
# to be correctly interpreted. Older versions of CMake may not contain
# all the features to "understand" this file.
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
# Defines the name and language the project will be using.
project(Gps_test LANGUAGES CXX)
# Set the path to the Qt5 installation's CMake instructions.
set(Qt5_DIR "D:/qt/5.10.1/msvc2017_64/lib/cmake/Qt5")
# Automatically add the current source and build directories to the
# include path.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Automatically create Qt5 MOCs at compile time.
set(CMAKE_AUTOMOC ON)
# Automatically create Qt5 UICs at compile time.
set(CMAKE_AUTOUIC ON)
# Include Qt5 and its widgets.
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)
# Add to PROJECT_SOURCES variable all the filenames inside
# ${CMAKE_CURRENT_SOURCE_DIR} which respect the given pattern.
file(GLOB PROJECT_SOURCES "*.cpp")
# Instruct CMake to create an executable based on all the .cpp
# sources of the project. You may, for example, create multiple
# executables based on different source files, inside the same
# project.
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
# Specify libraries or flags to use when linking a given target.
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Widgets)
Anyone knows how I can fix this? Thanks.
Those are part of the Xml component. Try this:
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Xml)
...
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::Xml
)

libsndfile library build error in VS 2012 Vc++ app project

I am creating Visual Studio VC++ app project on Win 7, that utilizes the libsndfile library.
The library( libsndfile-1.dll,libsndfile-1.dll) is at :
C:\SVN\US\AdditionalIncludeDir\libsndfile
and its headers(sndfile.hh,sndfile.h) at
C:\SVN\US\AdditionalIncludeDir\Include
the Visual Studio Project is at :
C:\SVN\US\Simulation\audioSim.vcxproj
I have added the library header reference as:
Project properties->C/C++->General->Additional Include Directories->
..\AdditionalIncludeDir\Include
and static lib reference as
Project properties->Linker>Additional dependencies:
libsndfile-1.lib
Project properties->Linker>Additional Library
Dir:..\AdditionalIncludeDir\libsndfile
I also have added
..\AdditionalIncludeDir\libsndfile
to path in user environment variable in Win 7.
The toplevel C++ file uses the library as:
SndfileHandle mic1(".\\filedata\\mic1.wav");
where mic1.wav is at
C:\SVN\US\Simulation\filedata
Building the project still gives me error:
error LNK2019: unresolved external symbol _sf_open referenced in
function "public: __thiscall SndfileHandle::SndfileHandle(char const
*,int,int,int,int)" (??0SndfileHandle##QAE#PBDHHHH#Z)
Why would this happen?
Thanks
sedy
I also had this problem. I had set "Additional Include Directories" (C:\Program Files (x86)\Mega-Nerd\libsndfile\include) and "Additional Library Directories" (C:\Program Files %28x86%29\Mega-Nerd\libsndfile\lib) in the project settings.
This fixed the problem:
Go to project properties -> Configuration properties -> Linker -> Input and type in the lib file name libsndfile-1.lib in the "Additional Dependencies" field.

Visual Studio 2010 linking errors using VTK

I'm having trouble to compile VTK 5.10.1 example code in Visual Studio 2010 on Windows 7 64 bit SP1.
First of all, I installed VTK using this tutorial.
No errors occured while installing VTK and the example executables like cone.exe located at C:\VTK5.10.1_cmake\bin\Release are running just fine.
In Visual Studio 2010 I started a new Win32 console application and tested the first example code from this site.
I edited the projekt properties (additional include directories, additional Library directories and additional dependencies).
The linker errors looked like that:
vtktest.obj : error LNK2019: unresolved external symbol ""public: void __thiscall vtkRenderer::AddActor(class vtkProp *)" (?AddActor#vtkRenderer##QAEXPAVvtkProp###Z)" referenced in function "_main".
Most of the the other people with the same error forgot to add the libs to the additional dependencies.
However I added all libs in the C:\VTK5.10.1_bin\lib\vtk-5.10 directory and the opengl32 library. I tried to seperate the libs by a space, a semicolon and a new line.
Then, I read that people are using the shared libraries. I'm not really familiar with dlls. I reinstalled VTK
with shared libraries options turned on and copied the resulting dlls to the System32 directory.
Now, the errors looks slightly different:
vtktest.obj : error LNK2019: unresolved external symbol ""__declspec(dllimport) public: void __thiscall vtkRenderer::AddActor(class vtkProp *)" (__imp_?AddActor#vtkRenderer##QAEXPAVvtkProp###Z)" referenced in function "_main".
Any help would be appriciated!
Nevermind, forgot to change the platform to x64 :/

error LNK2019: unresolved external symbol - only .h files

Argh... I've been struggling lately to make Visual Studio 2010 (VC++) include a bunch of 3rd party libraries I wanna use in my project. That's the issue: The linker seems not to be able to determine every symbol that is generated in my code which come from the 3rd party libraries definitions. I've included the header files path on my include directories and also the sources path on my source directory, but it is still not working. I've googled it for a while and in most of cases, the issues is cause by a missing reference of the .lib file on linker's additional dependencies, however the library don't come with them.
Here's a piece of sample code:
#include "stdafx.h"
#include <fuzzylite\FuzzyEngine.h>
int _tmain(int argc, _TCHAR* argv[])
{
fl::FuzzyEngine eng;
return 0;
}
So that's the output VS shows
Fuzzycolors.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall fl::FuzzyEngine::~FuzzyEngine(void)" (??1FuzzyEngine#fl##UAE#XZ) referenced in function _wmain
Fuzzycolors.obj : error LNK2019: unresolved external symbol "public: __thiscall fl::FuzzyEngine::FuzzyEngine(void)" (??0FuzzyEngine#fl##QAE#XZ) referenced in function _wmain
So i wonder if is there a way to build my sources with the .h and .cpp files of my 3rd party library.
Thank you.
Caio
Check the new version of fuzzylite-2.0 at http://www.fuzzylite.com. That problem has been solved.
Windows requires to add __declspec(dllexport) to classes, which was not present in previous versions. Today, every class starts with class FL_EXPORT Engine, where FL_EXPORT is the missing definition.

visual studio 2008 linker error

In visual studio 2008, I have created a static dll called test_static.dll. I am trying to call this from one application. I have included this dll in source files folder and the header file related to it in headers folder. When i am running the application I am getting following liking error. Please give me a solution.
error LNK2019: unresolved external
symbol "struct morph_output * __cdecl
morpho_data(struct morph_input *)"
(?morpho_data##YAPAUmorph_output##PAUmorph_input###Z)
referenced in function _wmain
1>D:\test_app\Debug\test_app.exe :
fatal error LNK1120: 1 unresolved
externals 1>Build log was saved at
"file://d:\test_app\test_app\Debug\BuildLog.htm"
Here test_app is application that is using static dll. and morpho_data is the dll function which is taking input as structure and returning another structure.
When compiling the dll you need to create an import library - a .lib file (Linker->Advanced->Import Library) and include it into the list of libraries your .exe depends on (Linker->Input->Additional Dependencies and Linker->General->Additional Library Directories).
Did you add test_static.lib as input to the linker in the project settings?
Did you include the header file where the function is declared ?

Resources