I'm trying to write a simple class in C++ with Qt5. I can't figure out this error comes from :
main.obj : error LNK2019: unresolved external symbol "public: __cdecl ItemModel::ItemModel(class std::basic_string,class std::allocator >)" (??0ItemModel##QEAA#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function main
debug\AMWS.exe : fatal error LNK1120: 1 unresolved externals
Test.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = Test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
HEADERS += \
main.h \
itemmodel.h
SOURCES += \
main.cpp \
itemmodel.cpp
itemmodel.h
#ifndef ITEMMODEL_H
#define ITEMMODEL_H
#include <string>
class ItemModel
{
public:
ItemModel(std::string sku);
protected:
std::string SKU;
};
#endif // ITEMMODEL_H
itemmodel.cpp
#include "itemmodel.h"
using namespace std;
ItemModel::ItemModel(string sku) : SKU(sku)
{
}
main.h
#ifndef MAIN_H
#define MAIN_H
#include <QCoreApplication>
#include <iostream>
#include "itemmodel.h"
#endif // MAIN_H
main.cpp
#include "main.h"
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ItemModel product("dummy");
cout << "Great!!!" << endl;
return a.exec();
}
Try to delete the complete build folder (Makefiles included) and recompile.
Sometime Qt apps have weird linker errors due to makefiles not being updated by qmake when they should.
Related
I'm using Visual Studio 2022 17.4.0 Preview 2.1, WinUI 3 with the WindowsAppSDK 1.1.5 and C++/WinRT 2.0.220929.3, and want to create a ListView using a template. In this goal I need to create a DataType which will be used by the template but can't compile it. I created the 3 following files:
Contact.idl
namespace Pine
{
[default_interface]
runtimeclass Contact
{
Contact();
}
}
Contact.h
#pragma once
#include "Contact.g.h"
namespace winrt::Pine::implementation
{
struct Contact : ContactT<Contact>
{
Contact();
};
}
namespace winrt::Pine::factory_implementation
{
struct Contact : ContactT<Contact, implementation::Contact>
{
};
}
Contact.cpp
#include "pch.h"
#include "Contact.h"
#if __has_include("Contact.g.cpp")
#include "Contact.g.cpp"
#endif
using namespace winrt;
using namespace Microsoft::UI::Xaml;
namespace winrt::Pine::implementation
{
Contact::Contact()
{
}
}
These files seem to correspond to every other runtimeclass I have seen, however it generates two linking errors:
1>Contact.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl winrt::Pine::implementation::ContactT<struct winrt::Pine::implementation::Contact>::Connect(int,struct winrt::Windows::Foundation::IInspectable const &)" (?Connect#?$ContactT#UContact#implementation#Pine#winrt##$$V#implementation#Pine#winrt##UEAAXHAEBUIInspectable#Foundation#Windows#4##Z)
1>Contact.obj : error LNK2001: unresolved external symbol "public: virtual struct winrt::Microsoft::UI::Xaml::Markup::IComponentConnector __cdecl winrt::Pine::implementation::ContactT<struct winrt::Pine::implementation::Contact>::GetBindingConnector(int,struct winrt::Windows::Foundation::IInspectable const &)" (?GetBindingConnector#?$ContactT#UContact#implementation#Pine#winrt##$$V#implementation#Pine#winrt##UEAA?AUIComponentConnector#Markup#Xaml#UI#Microsoft#4#HAEBUIInspectable#Foundation#Windows#4##Z)
1>C:\Users\user\source\repos\Pine\x64\Debug\Pine\Pine.exe : fatal error LNK1120: 2 unresolved externals
I want to create a Class which I'll be able to use in the following way in Xaml files:
<ListView>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Contact">
...
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Thank you for your time.
There is a bug in the generated file Contact.g.h.
For some reasons, defined(WINRT_FORCE_INCLUDE_CONTACT_XAML_G_H) || __has_include("Contact.xaml.g.h") is true (the macro is not defined and the files does not exist) so the wrong code is used at the end of the file. I had to delete these lines below.
//#if defined(WINRT_FORCE_INCLUDE_CONTACT_XAML_G_H) || __has_include("Contact.xaml.g.h")
//
//#include "Contact.xaml.g.h"
//
//#else
namespace winrt::Pine::implementation
{
template <typename D, typename... I>
using ContactT = Contact_base<D, I...>;
}
//#endif
So I have 2 static libs defined like this:
StaticLib1
// StaticLib1.h
#pragma once
class StaticLib1
{
public:
void doSomething1();
};
cpp:
// StaticLib1.cpp
#include "pugixml.hpp"
#include "StaticLib1.h"
void StaticLib1::doSomething1()
{
pugi::xml_node node;
}
StaticLib2
// StaticLib2.h
#pragma once
class StaticLib2
{
public:
void doSomething2();
};
cpp:
// StaticLib1.cpp
#include "pugixml.hpp"
#include "StaticLib2.h"
void StaticLib2::doSomething2()
{
pugi::xml_node node;
}
Main
#include <iostream>
#include "StaticLib1.h"
#include "StaticLib2.h"
int main(int argv, char** argc)
{
StaticLib1 staticlib1;
StaticLib2 staticlib2;
staticlib1.doSemething1();
staticlib2.doSemething2();
getchar();
return 0;
}
Now, if I build this. I get a lot of linking errors. Here are the first few linking errors:
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "public: __thiscall pugi::xml_attribute::xml_attribute(struct pugi::xml_attribute_struct *)" (??0xml_attribute#pugi##QAE#PAUxml_attribute_struct#1##Z) already defined in StaticLib1.lib(StaticLib1.obj)
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "public: __thiscall pugi::xml_attribute::xml_attribute(void)" (??0xml_attribute#pugi##QAE#XZ) already defined in StaticLib1.lib(StaticLib1.obj)
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "private: __thiscall pugi::xml_attribute_iterator::xml_attribute_iterator(struct pugi::xml_attribute_struct *,struct pugi::xml_node_struct *)" (??0xml_attribute_iterator#pugi##AAE#PAUxml_attribute_struct#1#PAUxml_node_struct#1##Z) already defined in StaticLib1.lib(StaticLib1.obj)
...
...
Now, I understand that this linking error is because there is a pugixml.obj inside StaticLib1.lib, and there is pugixml.obj inside StaticLib2.lib. But I don't understand why this would cause linking error with pugixml signatures. Why would they be defined twice? If I call staticlib1.doSomething1() shouldn't main not care if there are multiple definitions of pugi? Shouldn't staticlib1.doSomething1() handle all of that?
on the pugiconfig.hpp I have these specific settings:
#ifndef HEADER_PUGICONFIG_HPP
#define HEADER_PUGICONFIG_HPP
#define PUGIXML_WCHAR_MODE
#define PUGIXML_HEAD_ONLY
#include "pugixml.cpp"
#endif
So yes, from user0042advice, I realize it is better to compile a pugixml.lib on your own rather than having #include "pugixml.cpp" on the config. I'm working with legacy code so these surprises are there. Now, I've fixed my issue and made my company code slightly cleaner.
I am getting 3 linker errors when i launch my windows desktop c++ clr project. I am using the Event Stream Client API by Mobotix (http://developer.mobotix.com/).
Errors:
error LNK2028: not listed token (0A000429) ""public: virtual __thiscall ie::MxPEG::MxPEG_SinkVideo::~MxPEG_SinkVideo(void)" (??1MxPEG_SinkVideo#MxPEG#ie##$$FUAE#XZ)", wich is linked in function ""public: __thiscall SinkVideo::SinkVideo(char const *)" (??0SinkVideo##$$FQAE#PBD#Z)". C:\Users\Hecom\Documents\Visual Studio 2012\Projects\HKS\HKS\SinkVideo.obj HKS
error LNK2019: link to not listed external symbol ""public: virtual __thiscall ie::MxPEG::MxPEG_SinkVideo::~MxPEG_SinkVideo(void)" (??1MxPEG_SinkVideo#MxPEG#ie##$$FUAE#XZ)" in function ""public: __thiscall SinkVideo::SinkVideo(char const *)" (??0SinkVideo##$$FQAE#PBD#Z)". C:\Users\Hecom\Documents\Visual Studio 2012\Projects\HKS\HKS\SinkVideo.obj HKS
error LNK1120: 2 not listed externs C:\Users\Hecom\Documents\Visual Studio 2012\Projects\HKS\Debug\HKS.exe HKS
... i translated this error codes into english because i`m running the german version of Visual Studio.
When i comment out the following constructor in my SinkVideo.cpp, there are no more errors:
SinkVideo::SinkVideo (const char *outFile) {
}
as you see, the constructor is empty. so, why causes this errors?
could this be an error in the API?
Header-File (SinkVideo.h):
#include <Windows.h>
#include "MxPEG_SinkVideo.hpp"
#include "MxPEG_Image.hpp"
#include "MxPEG_Defines.hpp"
using namespace ie::MxPEG;
class SinkVideo : public MxPEG_SinkVideo {
public:
SinkVideo (const char *outFile);
protected:
virtual
MxPEG_ReturnCode doConsumeVideo(MxPEG_Image *buffer);
private:
FILE * fVideoOut;
};
Source-File (SinkVideo.cpp):
#include "SinkVideo.h"
#include <assert.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
SinkVideo::SinkVideo (const char *outFile) {
// the constructor which causes the errors
}
MxPEG_ReturnCode SinkVideo::doConsumeVideo(MxPEG_Image *buffer) {
return er_Success;
}
Here is the code of the MxPEG_SinkVideo.hpp header file in the API:
#ifndef MXPEG_SRC_MXPEG_SINK_HPP_
#define MXPEG_SRC_MXPEG_SINK_HPP_
#include "MxPEG_Image.hpp"
namespace ie { namespace MxPEG
{
/*
* Interface of the Vido Sink.
*
* Pass an instance of this to the constructor when creating an instance of MxPEG_SDK(). The SDK will pass all decoded video frames to the doConsumeVideo()
* function of that instance.
*
*/
class MxPEG_SinkVideo
{
protected:
public:
virtual
~MxPEG_SinkVideo();
MxPEG_ReturnCode consumeVideo(MxPEG_Image *buffer);
protected:
friend class MxPEG_SinkSynchronized;
/*
* Receives each video frame right after it was decoded. Either YUV or RGB is possible (see MxPEG_Image.hpp and MxPEG_SDK_API.hpp)
*
* doConsumeVideo needs to take care of deleting the buffer once it is no longer needed.
*/
virtual
MxPEG_ReturnCode doConsumeVideo(MxPEG_Image *buffer) = 0;
};
} } /* namespace ie::MxPEG */
#endif /* MXPEG_SRC_MXPEG_SINK_HPP_ */
I am struggling with
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Book::Book(void)" (??0Book##QEAA#XZ) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl Book::update(class Order)" (?update#Book##QEAAXVOrder###Z) referenced in function main
From what I have read, it usually is because of missing selectors, but I can't see myself missing anything. Also, no static variables, parameters seem correct, ... going through the issue list below didn't help figure out the issue.
https://msdn.microsoft.com/en-us/library/799kze2z.aspx
#include <QCoreApplication>
#include <order.h>
#include <book.h>
#include <iostream>
#include <vector>
std::vector<Order> md_orders;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Order order;
order.setIsBid( true );
order.setSize( 500 );
order.setPrice( 10.00 );
double price = order.getPrice();
Book book;
book.update( order );
std::cout << price;
return a.exec();
}
book.h
#ifndef BOOK_H
#define BOOK_H
//#include <order.h>
#include <vector>
class Book
{
public:
Book();
Order getBest( double, bool );
void update( Order );
double getBestPrice( bool );
private:
std::vector<Order> _orders;
};
#endif // BOOK_H
book.cpp
#include "book.h"
#include <limits>
//#include <order.h>
#include <iostream>
int imax = std::numeric_limits<int>::max();
Book::Book()
{
}
void Book::update( Order order ) {
std::cout << order.getSize();
}
I am new to cpp. I seem to be doing the same with the book class as with the order class, where it works fine. This is very confusing. Any pointers?
So, out of curiosity, I created a new projected and added a class TestA and run it, all went fine. Then I created class TestB exactly the same way and it breaks with the same error for TestB, but not TestA
#include <QCoreApplication>
#include "testa.h"
#include "testb.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TestA A;
TestB B;
return a.exec();
}
testa.h
#ifndef TESTA_H
#define TESTA_H
class TestA
{
public:
TestA();
};
#endif // TESTA_H
testb.h
#ifndef TESTB_H
#define TESTB_H
class TestB
{
public:
TestB();
};
#endif // TESTB_H
throws this
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl TestB::TestB(void)" (??0TestB##QEAA#XZ) referenced in function main
That is all on qt creator. It just seems very weird behaviour
In your main.cpp,
Notice how you have include book.h
Compiler will not look for book.h in local directory and couldn't find definition of book in standard lib location.
Instead of
#include <book.h> use #include"book.h"
Adding further.
You haven't defined below method in book.cpp and from main function you are trying to access them. Reason for all your pain.
Order getBest( double, bool );
double getBestPrice( bool );
You should do the following (provided you run compiler and linker manually):
Compile main.cpp
Compile book.cpp
Link both files into a single executable
All of above can be done in one command if your compiler allows it.
Since I'm not familiar with MSVC compiler, I'll show how I would do this with gcc:
gcc -c -o main.o main.cpp # Compiles main.cpp into main.o
gcc -c -o book.o book.cpp # Compiles book.cpp into book.o
gcc -o my_app main.o book.o # Creates the my_app executable from main.o and book.o
or
gcc -o my_app main.cpp book.cpp # Does all of above in one command
Yesterday I've tried to make a socket server in C++, but I get errors upon compiling.
The errors:
Error 6 error LNK2019: unresolved external symbol _imp_socket#12 referenced in function "public: static unsigned long __cdecl Env::GetSocket(void)" (?GetSocket#Env##SAKXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 5 error LNK2019: unresolved external symbol _imp_listen#8 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 4 error LNK2019: unresolved external symbol _imp_htons#4 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 3 error LNK2019: unresolved external symbol _imp_bind#12 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 2 error LNK2001: unresolved external symbol "public: static class Network * Env::Network" (?Network#Env##2PAV0#A) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\HabboV5.obj HabboV5
Error 7 error LNK1120: 5 unresolved externals C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\Debug\HabboV5.exe HabboV5
My main .cpp class:
// HabboV5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "Env.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout.write("hi", 2);
cout << "Hello World!" << endl;
Env::Network = new Network();
Env::Network->Start();
while (1)
{
char input[256];
cin.getline(input, 256);
}
}
Network.h:
#pragma once
#include <WinSock2.h>
class Network
{
private:
SOCKET socket;
public:
Network(void);
void Start();
};
Network.cpp:
#include "StdAfx.h"
#include "Network.h"
#include <WinSock2.h>
#include "Env.h"
Network::Network(void)
{
}
void Network::Start()
{
this->socket = Env::GetSocket();
SOCKADDR_IN sInformation;
sInformation.sin_family = AF_INET;
sInformation.sin_addr.s_addr = INADDR_ANY;
sInformation.sin_port = htons(30000);
bind(this->socket, (SOCKADDR*) (&sInformation), sizeof(sInformation));
listen(this->socket, 10);
}
Env.h:
#include "stdafx.h"
#include "Network.h"
#include <WinSock2.h>
class Env
{
public:
static Network* Network;
static DWORD GetSocket()
{
return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
};
In the linker options (on the project right-click, linker, input) you need add wsock32.lib or ws2_32.lib to the list of input files.