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
Related
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've looked at other people's solutions to this problem and none of them, unfortunately, have solved my issue. I was having this error in my dev project, so I started from scratch, followed this tutorial to the letter, including the file names and locations, and I am still getting
1>unittest1.obj : error LNK2019: unresolved external symbol "public:
static int __cdecl HelloWorld::getTwo(void)"
(?getTwo#HelloWorld##SAHXZ) referenced in function "public: void
__thiscall UnitTest1::UnitTest1::TestMethodGetTwo(void)" (?TestMethodGetTwo#UnitTest1#1#QAEXXZ)
1>unittest1.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
HelloWorld::getN(void)const " (?getN#HelloWorld##QBEHXZ) referenced in
function "public: void __thiscall
UnitTest1::UnitTest1::TestMethodGetN(void)"
(?TestMethodGetN#UnitTest1#1#QAEXXZ)
The code is in the tutorial I linked but I will copy it below. I don't understand what I could be doing wrong at this point - my test project depends on my build project, my definitions for these functions are in the class.
HelloWorld.h
#pragma once
class HelloWorld
{
int n = 10;
public:
static int getTwo();
int getN() const;
};
HelloWorld.cpp
#include "stdafx.h"
#include "HelloWorld.h"
int main()
{
return 0;
}
int HelloWorld::getTwo()
{
return 2;
}
int HelloWorld::getN() const
{
return n;
}
unittest1.cpp (in test project)
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../HelloWorld/HelloWorld.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethodGetTwo)
{
Assert::AreEqual(2, HelloWorld::getTwo());
}
TEST_METHOD(TestMethodGetN)
{
HelloWorld hw = HelloWorld();
Assert::AreNotEqual(0, hw.getN());
}
};
}
For some reason the linker can't find my definitions, and I'm out of ideas about why that might be.
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 7 years ago.
I know this problem is well known, but none of the solutions work for me. I know a popular cause of this error is the compiler can't find the definition of a function in any of the source files, but I have defined the function them.
I am using Visual studio 2015 community.
Form.h
#pragma once
template<typename T>
class Form
{
public:
void GenerateForm(T i);
};
Form.cpp
#include "stdafx.h"
#include "Form.h"
template<typename T>
void Form<T>::GenerateForm(T i)
{
std::cout << i << endl;
}
Main.cpp
#include "stdafx.h"
#include "Form.h"
int main()
{
Form<int> f;
f.GenerateForm(12);
return 0;
}
Error:
PrimeForm.obj : error LNK2019: unresolved external symbol "public: void __thiscall Formula<double>::GenerateForm(int)" (?GenerateForm#?$Formula#N##QAEXH#Z) referenced in function _main
C:\Users\John\Dropbox\Visual Studio 2015\PrimeForm\Debug\PrimeForm.exe : fatal error LNK1120: 1 unresolved externals
When you try to compile form.cpp the compiler doesn't know what type T will be. Therefore it won't be able to compile this as an object file to be linked with your compiled main.cpp object file.
You'll need to include all of the declarations and definitions of a templated class to the files that need it (in this case your main.cpp file).
This can be simply done as follows:
Form.h
#pragma once
template<typename T>
class Form
{
public:
void GenerateForm(T i);
};
#include "Form.template" /* Note include Form.template here */
Form.template
#include "stdafx.h"
/* Don't include form.h in this file */
template<typename T>
void Form<T>::GenerateForm(T i)
{
std::cout << i << std::endl;
}
main.cpp
#include "stdafx.h"
#include "Form.h" /* including Form.h will include Form.template as well */
int main()
{
Form<int> f; /* Compiler now knows to instantiate a Form class with type Int as the template parameter */
f.GenerateForm(12);
return 0;
}
Note the main difference is that you don't include "Form.h" in Form.template but include "Form.template" at the bottom of Form.h
It is better practice to use the ".template" file ending for templated class implementation files.
I have two projects (call them Test and Intrados). Inside Intrados, I have the following namespace:
#include "Mapper.h"
#include "Director.h"
#include "Driver.h"
#include <iostream>
#include <string>
using namespace std;
namespace IntradosMediator {
void addVehicle(string);
}
void IntradosMediator::addVehicle(string vehicleName) {
Mapper* mapper = Mapper::getInstance();
mapper->addVehicle(vehicleName);
}
From within the Intrados project, calling "IntradosMediator::Mapper(addVehicle)" works just fine; yet, in project Test, the following code produces a link error:
#include "IntradosMediator.cpp"
#include "Mapper.h"
using namespace IntradosMediator;
int main(){
IntradosMediator::addVehicle("Car X");
return 0;
}
The error is:
Test.obj : error LNK2019: unresolved external symbol "public: static class Mapper *
__cdecl Mapper::getInstance(void)" (?getInstance#Mapper##SAPAV1#XZ) referenced in
function "void __cdecl IntradosMediator::addVehicle(class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >)"
(?addVehicle#IntradosMediator##YAXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##
std###Z)
I've made sure to add Intrados as a reference for Test, and also included it in the Include Directories. Not sure what to do here, since I'm new to C++. Thanks in advance for any advice.
Edit:
I'm adding the Mapper code here:
//.h
#ifndef MAPPER_H
#define MAPPER_H
#include <string>
using std::string;
class Mapper {
public:
static Mapper* getInstance();
void addVehicle(string);
private:
//this is a singleton
Mapper(){};
};
#endif
//.cpp
#include "Mapper.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
vector<string> vehicleList;
Mapper* Mapper::getInstance(){
static Mapper instance;
return &instance;
}
void
Mapper::addVehicle(string vehicleName) {
vehicleList.push_back(vehicleName);
}
The error says the linker can't find Mapper::getInstance (it seems to find your addVehicle function just fine). Might you be failing to include the library that implements "Mapper" in your link?
Could you paste your code for class Mapper?
It seems like you are missing addVehicle function in that class, which is what the compiler is complaining about.