#include "stdafx.h" //In order to use Visual C++
#include <iostream>
#include <Loki\SmallObj.h> //The header file to manage
// smalls objects allocator
class MySmallObj : public Loki::SmallObjAllocator //inherit from the base
//class SmallObjAllocator
{
public:
MySmallObj():SmallObjAllocator(sizeof(char), sizeof(long),0){};
};
int _tmain(int argc, _TCHAR* argv[])
{
MySmallObj * premier = new MySmallObj; //declaring my object derived from smallobjallcator
char * myChar = static_cast<char*>( premier->Allocate(1, true)); //calling allocate from my object and conveting the void pointer to char*
premier.Deallocate(myChar, 1);
return 0;
}
The loki library uses essentially generic programming in c++
I have got the code up there using Small object allocator of memory(Loki::SmallObjAllocator)
I m using visual c++ 2010
I get those errors:
> MyLoki.cpp
1>MyLoki.obj : error LNK2019: unresolved external symbol "public: void __thiscall Loki::SmallObjAllocator::Deallocate(void *,unsigned int)" (?Deallocate#SmallObjAllocator#Loki##QAEXPAXI#Z) referenced in function _wmain
1>MyLoki.obj : error LNK2019: unresolved external symbol "public: void * __thiscall Loki::SmallObjAllocator::Allocate(unsigned int,bool)" (?Allocate#SmallObjAllocator#Loki##QAEPAXI_N#Z) referenced in function _wmain
1>MyLoki.obj : error LNK2019: unresolved external symbol "protected: __thiscall Loki::SmallObjAllocator::SmallObjAllocator(unsigned int,unsigned int,unsigned int)" (??0SmallObjAllocator#Loki##IAE#III#Z) referenced in function "public: __thiscall MySmallObj::MySmallObj(void)" (??0MySmallObj##QAE#XZ)
I have founded an answer to my first question.
#include "stdafx.h" //In order to use Visual C++
#include <iostream>
#include <Loki\SmallObj.h> //The header file to manage
// smalls objects allocator
class MySmallObj : public Loki::SmallObjAllocator //inherit from the base
//class SmallObjAllocator
{
public:
MySmallObj():SmallObjAllocator(sizeof(char), sizeof(long),1){}; //the chunkSize < maxObjsize
};
int _tmain(int argc, _TCHAR* argv[])
{
MySmallObj * premier = new MySmallObj; //declaring my object derived from smallobjallcator
char * myChar = static_cast<char*>( premier->Allocate(1, true)); //calling allocate from my object and conveting the void pointer to char*
premier->Deallocate(myChar, 1);
return 0;
}
the errors of multiple
unresolved external symbol
are because of SmallObj.cpp , of Loki that was not included to the project
for example of Loki::SmallObjAllocator, Loki::SmallObject here
Related
I've been struggling to find why my linker gets an unresolved external symbol error. The error looks like this:
Error
LNK2019
unresolved external symbol "public: __thiscall Shader::Shader(char const *)" (??0Shader##QAE#PBD#Z) referenced in function "public: __thiscall GridWorldGPGPU::GridWorldGPGPU(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned int)" (??0GridWorldGPGPU##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##I#Z)
DV2556_Project
grid_world_GPGPU.obj
1
As far as I understand it it has something to do with that my linker finds the declaration of the Shader::Shader(char const *)-function but cannot find the definition. I have been staring at this for hours and cannot figure out why the linker becomes sad.
grid_world_GPGPU.h:
#ifndef GRID_WORLD_GPGPU_H
#define GRID_WORLD_GPGPU_H
#include "grid_world.h"
#include <../swift_open_gl.h>
class GridWorldGPGPU : public GridWorld {
private:
Shader* compute_shader_ = nullptr;
public:
GridWorldGPGPU(std::string in_path_shader, unsigned int in_side = 1);
};
#endif // !GRID_WORLD_GPGPU_H
grid_world_GPGPU.cpp:
GridWorldGPGPU::GridWorldGPGPU(std::string in_path_shader, unsigned int in_side)
: GridWorld(in_side) {
this->compute_shader_ = new Shader(in_path_shader.c_str());
}
The Shader-class is defined in the swift_open_gl.h file:
#ifndef SWIFT_OPEN_GL_H
#define SWIFT_OPEN_GL_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
class Shader {
public:
Shader(const char* cs_path);
};
#endif // !SWIFT_OPEN_GL_H
And swift_open_gl.cpp has this:
#include "..\swift_open_gl.h"
inline Shader::Shader(const char * cs_path) {
//Do stuff
}
I've tried with and without the inline (Visual Studio added it when I tried moving the function definition between the .h-file and a .cpp-file, so I decided to try it) and i have verified that the #include <../swift_open_gl.h> doesn't occur anywhere else in the project than the files listed above.
An extra set of eyes to look over this would be appreciated!
Provide default constructor of the class as well.
class Shader {
public:
Shader(){} // default constructor
Shader(const char* cs_path);
};
Dont use inline in Shader::Shader(const char* cs_path){} definition
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 7 years ago.
I am trying to figure out how to use classes with class template and I get these errors:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall AdtBag::AdtBag(void)" (??0?$AdtBag#H##QAE#XZ) referenced in function _main C:\Users\User\Documents\Visual Studio 2013\Projects\ADTBagAddition\ADTBagAddition\Source.obj ADTBagAddition
Error 2 error LNK2019: unresolved external symbol "public: __thiscall AdtBag::~AdtBag(void)" (??1?$AdtBag#H##QAE#XZ) referenced in function _main C:\Users\User\Documents\Visual Studio 2013\Projects\ADTBagAddition\ADTBagAddition\Source.obj ADTBagAddition
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall AdtBag::store_in_bag(int)" (?store_in_bag#?$AdtBag#H##QAEXH#Z) referenced in function _main C:\Users\User\Documents\Visual Studio 2013\Projects\ADTBagAddition\ADTBagAddition\Source.obj ADTBagAddition
Error 4 error LNK2019: unresolved external symbol "public: int __thiscall AdtBag::whats_in_bag(void)" (?whats_in_bag#?$AdtBag#H##QAEHXZ) referenced in function _main C:\Users\User\Documents\Visual Studio 2013\Projects\ADTBagAddition\ADTBagAddition\Source.obj ADTBagAddition
Error 5 error LNK1120: 4 unresolved externals C:\Users\User\Documents\Visual Studio 2013\Projects\ADTBagAddition\Debug\ADTBagAddition.exe ADTBagAddition
Here is my code:
source.cpp
#include <iostream>
#include "AdtBag.h"
using namespace std;
int main () {
AdtBag<int> BagInt;
int a = 78;
cout << "Int Bag Contains: " << endl;
BagInt.store_in_bag ( a );
cout << BagInt.whats_in_bag () << endl;
return 0;
}
AdtBag.h
#ifndef __ADTBAG__
#define __ADTBAG__
template<class ItemType>
class AdtBag {
private:
ItemType in_bag;
public:
AdtBag<ItemType> ();
~AdtBag<ItemType> ();
void store_in_bag ( ItemType into_bag );
ItemType whats_in_bag ();
};
#endif
AdtBag.cpp
#include "AdtBag.h"
template <class ItemType>
AdtBag<ItemType>::AdtBag () {
}
template <class ItemType>
AdtBag<ItemType>::~AdtBag () {
}
template<class ItemType>
void AdtBag<ItemType>::store_in_bag ( ItemType into_bag ) {
in_bag = into_bag;
}
template<class ItemType>
ItemType AdtBag<ItemType>::whats_in_bag () {
return in_bag;
}
Why is this producing the error messages? I'm using Visual Studio 2013 if that matters. I thought I did everything correctly, but I guess not. Any suggestions?
Loosely speaking, all template class code must be in the header.
Essentially, this is because template code is only compiled when a template is instantiated for some type.
This question already has an answer here:
C and hashlib LNK errors
(1 answer)
Closed 9 years ago.
Note that i am using Windows Forms Applications with the .NET
Here is my code:
#pragma once
#include <cstdlib>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <cstring>
#include <iostream>
#include <vcclr.h>
#include <hashlibpp.h>
namespace Launcher {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Diagnostics;
using namespace MySql::Data::MySqlClient;
using namespace std;
using namespace System::Runtime::InteropServices;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
//Component properties (not important)
}
#pragma endregion
void convert(String^ total1, char *ch){ //Converts string to const char*
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_t> c1 = PtrToStringChars(total1);
printf_s("%S\n", c1);
// Conversion to char* :
// Can just convert wchar_t* to char* using one of the
// conversion functions such as:
// WideCharToMultiByte()
// wcstombs_s()
// ... etc
size_t convertedChars = 0;
size_t sizeInBytes = ((total1->Length + 1) * 2);
errno_t err = 0;
ch = (char *)malloc(sizeInBytes);
err = wcstombs_s(&convertedChars,
ch, sizeInBytes,
c1, sizeInBytes);
if (err != 0)
printf_s("wcstombs_s failed!\n");
printf_s("%s\n", ch);
}
void MarshalString ( String ^ s, string& os ) { //Another converter
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
void MarshalString ( String ^ s, wstring& os ) { //Another converter
using namespace Runtime::InteropServices;
const wchar_t* chars =
(const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
//I removed some other functions from here because they are blank
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//MySQL variables
try{
conDataBase->Open();
myReader=cmdDataBase->ExecuteReader();
while(myReader->Read()){
String^ atmp_user = textBox1->Text;
String^ user = (myReader->GetString(1));//Gets Usernames from database
String^ atmp_pass = textBox2->Text;
String^ pass = (myReader->GetString(2));//Gets Passwords from database
atmp_pass->ToLower();//Lower Case's variable
atmp_user->ToLower();
String^ total1 = gcnew String(atmp_user+atmp_pass);//Combines 2 strings
string totala;//std::string
MarshalString(total1, totala);//Copies data from (total1) to (totala)
hashwrapper *myWrapper = new sha1wrapper();//SHA1 hashing begins
string hash1 = myWrapper->getHashFromString(totala);//creates new variable(hash1) and copies data from (totala)
String^ finalpass;//Creates new System::String
MarshalString(finalpass, hash1);//Copies data from (hash1) to (finalpass)
delete myWrapper;//Ends SHA1 hashing
//Login Script
if(atmp_user == user && finalpass == pass){
textBox1->Text = ("It worked!");
}
}
} catch(Exception^ex) {
MessageBox::Show(ex->Message);
}
}
//Other blank functions created
};
}
Here are the errors:
Error 4 error LNK2028: unresolved token (0A0000D5) "public: __clrcall md5wrapper::md5wrapper(void)" (??0md5wrapper##$$FQAM#XZ) referenced in function "public: class hashwrapper * __clrcall wrapperfactory::create(enum HL_Wrappertype)" (?create#wrapperfactory##$$FQAMPAVhashwrapper##W4HL_Wrappertype###Z) C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Launcher\hl_wrapperfactory.obj
Error 3 error LNK2028: unresolved token (0A00009D) "public: __clrcall sha384wrapper::sha384wrapper(void)" (??0sha384wrapper##$$FQAM#XZ) referenced in function "public: class hashwrapper * __clrcall wrapperfactory::create(enum HL_Wrappertype)" (?create#wrapperfactory##$$FQAMPAVhashwrapper##W4HL_Wrappertype###Z) C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Launcher\hl_wrapperfactory.obj
Error 2 error LNK2028: unresolved token (0A000094) "public: __clrcall sha512wrapper::sha512wrapper(void)" (??0sha512wrapper##$$FQAM#XZ) referenced in function "public: class hashwrapper * __clrcall wrapperfactory::create(enum HL_Wrappertype)" (?create#wrapperfactory##$$FQAMPAVhashwrapper##W4HL_Wrappertype###Z) C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Launcher\hl_wrapperfactory.obj
Error 1 error LNK2028: unresolved token (0A00005B) "public: __clrcall sha256wrapper::sha256wrapper(void)" (??0sha256wrapper##$$FQAM#XZ) referenced in function "public: class hashwrapper * __clrcall wrapperfactory::create(enum HL_Wrappertype)" (?create#wrapperfactory##$$FQAMPAVhashwrapper##W4HL_Wrappertype###Z) C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Launcher\hl_wrapperfactory.obj
Error 5 error LNK2019: unresolved external symbol "public: __clrcall sha512wrapper::sha512wrapper(void)" (??0sha512wrapper##$$FQAM#XZ) referenced in function "public: class hashwrapper * __clrcall wrapperfactory::create(enum HL_Wrappertype)" (?create#wrapperfactory##$$FQAMPAVhashwrapper##W4HL_Wrappertype###Z) C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Launcher\hl_wrapperfactory.obj
Error 6 error LNK2019: unresolved external symbol "public: __clrcall sha384wrapper::sha384wrapper(void)" (??0sha384wrapper##$$FQAM#XZ) referenced in function "public: class hashwrapper * __clrcall wrapperfactory::create(enum HL_Wrappertype)" (?create#wrapperfactory##$$FQAMPAVhashwrapper##W4HL_Wrappertype###Z) C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Launcher\hl_wrapperfactory.obj
Error 7 error LNK2019: unresolved external symbol "public: __clrcall sha256wrapper::sha256wrapper(void)" (??0sha256wrapper##$$FQAM#XZ) referenced in function "public: class hashwrapper * __clrcall wrapperfactory::create(enum HL_Wrappertype)" (?create#wrapperfactory##$$FQAMPAVhashwrapper##W4HL_Wrappertype###Z) C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Launcher\hl_wrapperfactory.obj
Error 8 error LNK2019: unresolved external symbol "public: __clrcall md5wrapper::md5wrapper(void)" (??0md5wrapper##$$FQAM#XZ) referenced in function "public: class hashwrapper * __clrcall wrapperfactory::create(enum HL_Wrappertype)" (?create#wrapperfactory##$$FQAMPAVhashwrapper##W4HL_Wrappertype###Z) C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Launcher\hl_wrapperfactory.obj
Error 9 error LNK1120: 8 unresolved externals C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Launcher\Debug\Launcher.exe 1
I don't understand these so i hope you guys can. Btw i am using the hashlib++ library in this project for the SHA1 hashing. I am assuming that's why there are so many references to it.
You need to link againts the library itself. In visual studio, go to project preferences, linker options, and choose the actual library you're using.
Here you can find an example with screenshoots on how it can be done.
This is a weird error I've run into. For some reason VC++ treats the methods as having __clrcall calling convention, even though they belong to unmanaged classes. I'm not sure what triggers it, it doesn't happen most of the time.
If all else fails, try explicitly marking methods of unmanaged classes (including constructors and destructors) with the __thiscall calling convention (which is the default for unmanaged methods):
class md5wrapper
{
public:
__thiscall md5wrapper();
__thiscall ~md5wrapper();
void __thiscall SomeMethod();
...
In order to test leveldb, I tried to reproduce the leveldb's example on VS 2008.
#include <assert.h>
#include "leveldb/db.h"
int main()
{
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options,"D:\dev\tools\tmp",&db);
}
I have included leveldb/include directory and linked libleveldb.lib.
Result :
error LNK2019: unresolved external symbol "public: static class
leveldb::Status __cdecl leveldb::DB::Open(struct leveldb::Options
const &,class std::basic_string,class std::allocator > const &,class
leveldb::DB * *)"
(?Open#DB#leveldb##SA?AVStatus#2#ABUOptions#2#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##PAPAV12##Z)
referenced in function _main
error LNK2019: unresolved external symbol public: __thiscall leveldb::Options::Options(void)"
(??0Options#leveldb##QAE#XZ) referenced in function _main
Does anyone know how to fix this ?
Solution :
Use levelDb-portable from zhangyafreikimi
I have an very strange error: when I want to use the SocialServer::Client class from my SocialServer::Server class, the linker threw me two LNK2019 errors :
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall SocialServer::Client::Handle(void)" (?Handle#Client#SocialServer##QAEXXZ) referenced in function "private: static unsigned int __stdcall SocialServer::Server::listenThread(void *)" (?listenThread#Server#SocialServer##CGIPAX#Z) C:\Users\benjamin\Documents\Visual Studio 2010\Projects\FCX Social Server\SocialServer Core\Server.obj SocialServer Core
Error 2 error LNK2019: unresolved external symbol "public: __thiscall SocialServer::Client::Client(unsigned int)" (??0Client#SocialServer##QAE#I#Z) referenced in function "private: static unsigned int __stdcall SocialServer::Server::listenThread(void *)" (?listenThread#Server#SocialServer##CGIPAX#Z) C:\Users\benjamin\Documents\Visual Studio 2010\Projects\FCX Social Server\SocialServer Core\Server.obj SocialServer Core
However , these 2 missing function are correctly implemented :
Client.h
#pragma once
#include "dll.h"
namespace SocialServer
{
class __social_class Client
{
public:
Client(SOCKET sock);
~Client();
void Handle();
private:
static unsigned __stdcall clientThread(void* value);
SOCKET _socket;
uintptr_t _thread;
unsigned int _thread_id;
};
}
Client.cpp
#pragma once
#include "Client.h"
namespace SocialServer
{
Client::Client(SOCKET socket)
{
this->_socket = socket;
}
Client::~Client()
{
}
void Client::Handle()
{
std::cout << " New client " << std::endl;
this->_thread = _beginthreadex(NULL, 0, Client::clientThread, &this->_socket, CREATE_SUSPENDED, &this->_thread_id);
ResumeThread((HANDLE)this->_thread);
}
unsigned __stdcall Client::clientThread(void* value)
{
// Some code to execute here ...
}
}
Where does the problem comes from ?
i've found the solution.
In a function that's used by _beginthreadex() (with unsigned __stdcall) , always add a return at the end.