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.
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.
When compiling my code I receive this error.
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Gabe Phelan\Documents\Visual Studio 2013\Projects\PA3 test\Debug\PA3 test.exe : fatal error LNK1120: 1 unresolved externals
#include <iostream>
#include <vector>
using namespace std;
class Heap{
private:
vector<int> heap;
int size;
public:
Heap(bool x);
};
#include "Header.h"
Heap::Heap(bool order){
int dummy = 0;
heap.push_back(dummy);
size = heap.size() - 1;
}
You have to have have an entry point, which is a function called main. So you need this,
int main(int argv, char* argc[])
{
//your code here
}
You dont declare classes in this, but what you want the program to execute.
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.
I have the following .h and .cpp files
If i have to I will include the full codes of the function definitions
When i compile my program i get the errors shown at the end
hash.h
#define BUCKETS 64
#define B_ENTRIES 50000
int curr_tanker;
typedef unsigned long int ulong;
typedef struct bucket
{
int bucket_id;
ulong bucket_entries;
}bucket;
typedef struct tanker_record
{
ulong tanker_id;
ulong tanker_size;
ulong num_of_entries;
ulong bucket_entry_count;
}tanker_record;
typedef struct fpinfo
{
unsigned long chunk_offset;
unsigned long chunk_length;
unsigned char fing_print[33];
}fpinfo;
struct fpinfo* InitHTable(fpinfo *);
int CreateTanker(tanker_record tr[]);
int Hash_CreateEntry(struct fpinfo *,struct fpinfo he,tanker_record tr);
ht.cpp
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include <iostream>
#include "ht.h"
struct fpinfo* InitHTable(struct fpinfo ht[][B_ENTRIES])
{
}
int CreateTanker(tanker_record tr[])
{
}
int
Hash_CreateEntry(struct fpinfo *t[][B_ENTRIES],struct fpinfo he,tanker_record tr[])
{
}
static void
WriteHTtoFile(struct fpinfo *t[][B_ENTRIES],int this_tanker)
{
}
main.cpp
#include<iostream>
#include"ht.cpp"
#include<conio.h>
#include<stdlib.h>
void main(int argc, char **argv)
{
static fpinfo hash_table[BUCKETS][B_ENTRIES];
static tanker_record tr[100];
InitHTable(&hash_table[0][0]);
CreateTanker(tr);
struct fpinfo fp;
...
ar = Hash_CreateEntry(&hash_table[0][0], fp,tr[0]);
i get the following errors when i try to compile it using vc2010
1>main.obj : error LNK2005: "struct fpinfo * __cdecl InitHTable(struct fpinfo (* const)[50000])" (?InitHTable##YAPAUfpinfo##QAY0MDFA#U1##Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl CreateTanker(struct tanker_record * const)"
(?CreateTanker##YAHQAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl Hash_CreateEntry(struct fpinfo * (* const)[50000],struct fpinfo,struct tanker_record * const)" (?Hash_CreateEntry##YAHQAY0MDFA#PAUfpinfo##U1#QAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int curr_tanker" (?curr_tanker##3HA) already defined in ht.obj
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl Hash_CreateEntry(struct fpinfo *,struct fpinfo,struct tanker_record)"
(?Hash_CreateEntry##YAHPAUfpinfo##U1#Utanker_record###Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "struct fpinfo * __cdecl InitHTable(struct fpinfo *)" (?InitHTable##YAPAUfpinfo##PAU1##Z) referenced in function _main
THANKS FOR YOUR HELP!!
You're including ht.cpp from main.cpp, which will include all the definitions of functions already defined in ht.cpp itself.
You want to include ht.h instead.
It won't help in this situation, but you should also protect the header file with include guards:
#ifndef HT_H
#define HT_H
// contents of ht.h
#endif
You also need the arguments of the function declarations to match those of the definitions:
struct fpinfo* InitHTable(struct fpinfo[][B_ENTRIES]);
// Missing: ^^^^^^^^^^^
int CreateTanker(tanker_record tr[]); // OK
int Hash_CreateEntry(struct fpinfo*[][B_ENTRIES],struct fpinfo,tanker_record[]);
// Missing ^^^^^^^^^^^^^ ^^
Add an "include guard" in your header, so that it its contents aren't "seen" twice after preprocessing. For Microsoft, #pragma once at the beginning of the .h file. In general, add:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
Make sure to adopt a consistent "unique" naming scheme for each of your headers. __YOUR_HEADER_H would do, for example customio.h into __CUSTOM_IO_H.