Wrapping a C++ DLL with a managed class - c++

I'm trying to wrap a unmanaged C++ DLL with managed C++ and I keep getting linking errors.
even though I include my library.lib in the project and include the correct header file.
This is the managed class:
#pragma once
#include "..\Terminal\Terminal.h"
public ref class ManagedTerminal
{
private:
Terminal * m_unTerminal;
public:
ManagedTerminal(void)
{
m_unTerminal = new Terminal();
}
};
and this is the unmanaged class:
#include "..\Core1.h"
#include "..\Core2.h"
__declspec(dllexport) class Terminal
{
private:
CoreObj m_core;
public:
Terminal();
void Init(char* path, char* filename);
void Start();
void Stop();
void Run();
Array<Report> GetSnapshot();
~Terminal(void);
};
and the errors I get are:
Error 5 error LNK2028: unresolved token (0A0000B3) "public: __thiscall Terminal::Terminal(void)" (??0Terminal##$$FQAE#XZ) referenced in function "public: __clrcall ManagedTerminal::ManagedTerminal(void)" (??0ManagedTerminal##$$FQ$AAM#XZ) ManagedTerminal.obj TerminalWrapper
Error 6 error LNK2019: unresolved external symbol "public: __thiscall Terminal::Terminal(void)" (??0Terminal##$$FQAE#XZ) referenced in function "public: __clrcall ManagedTerminal::ManagedTerminal(void)" (??0ManagedTerminal##$$FQ$AAM#XZ) ManagedTerminal.obj TerminalWrapper
can anybody tell me what's wrong?
thanks :)

You have to match all of the build settings -- specifically the calling conventions (CDECL vs. STDCALL) -- in order to have a successful link.
Since .NET 2.0, you have also had to link to the c-runtime dynamically, so make sure that both the .dll and the managed C++ project do this.
Basically, go into the properties dialog for both projects and make sure that things that affect the call are the same.

Related

Unresolved Add_Test symbols, even with test fixture class included in the file

All of a sudden, after my tests had been working for hours, I'm getting errors that it can't find my text fixture functions (SetUp/TearDown). I'd had the fixture defined in a separate file, so as a quick fix I moved the class to the test file, but I still get the same problem!
Here's the current file. I didn't just omit the code for brevity, I've tried building it in this form to make sure the problem isn't somehow caused by the code inside the tests. I've left in the includes though.
#include "memory"
#include "../TestUtility/TestUtility.h"
#include "DependencyInjector.h"
#include "gtest/gtest.h"
#include "RecoverableErrorException.h"
namespace Test
{
class DependencyInjectorTest : public testing::Test
{
public:
DependencyInjectorTest();
virtual ~DependencyInjectorTest();
protected:
virtual void SetUp() { Framework::DependencyInjector::Destroy(); };
virtual void TearDown() override;
};
TEST_F ( DependencyInjectorTest, FindEmpty )
{
// content ommitted
}
TEST_F ( DependencyInjectorTest, Add )
{
// content ommitted
}
TEST_F ( DependencyInjectorTest, Find )
{
// content ommitted
}
} // namespace Test
And here are the errors:
1>DependencyInjectorUnitTests.obj : error LNK2019: unresolved external symbol "public: __thiscall Test::DependencyInjectorTest::DependencyInjectorTest(void)" (??0DependencyInjectorTest#Test##QAE#XZ) referenced in function "public: __thiscall Test::DependencyInjectorTest_Add_Test::DependencyInjectorTest_Add_Test(void)" (??0DependencyInjectorTest_Add_Test#Test##QAE#XZ)
1>DependencyInjectorUnitTests.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Test::DependencyInjectorTest::~DependencyInjectorTest(void)" (??1DependencyInjectorTest#Test##UAE#XZ) referenced in function "public: virtual __thiscall Test::DependencyInjectorTest_Add_Test::~DependencyInjectorTest_Add_Test(void)" (??1DependencyInjectorTest_Add_Test#Test##UAE#XZ)
1>DependencyInjectorUnitTests.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall Test::DependencyInjectorTest::TearDown(void)" (?TearDown#DependencyInjectorTest#Test##MAEXXZ)
UPDATE: The errors disappeared at some point. And then a few hours later, just as mysteriously and frustratingly, came back. Even on files I hadn't worked on since it was fine.
You need to define the constructor, destructor and TearDown methods of DependencyInjectorTest. Even if they are virtual, the compiler needs basic methods to initialize the vtable.
https://en.cppreference.com/w/cpp/language/virtual
By declaring them in the class definition, the macros used them implicitly, and they need to be defined. As they're not, linking results in an undefined symbol error.
You can either remove the declarations or define the functions.

Errors LNK 2019 and LNK2028 for one function in a class, other works fine

I normally work in c# and am out of my wits for this one . I used Walkthrough: Creating and Using a Dynamic Link Library (C++) to create a Dynamic Link Library.
I have defined two methods as shown below
DeveloperConsoleManager.h
#pragma once
#include "atlstr.h"
#ifdef DEVCONSOLEMANAGER_EXPORTS
#define DEVCONSOLEMANAGER_API __declspec(dllexport)
#else
#define DEVCONSOLEMANAGER_API __declspec(dllimport)
#endif
namespace DeveloperConsoleManager
{
class DeveloperConsoleLogic
{
public:
// Returns a + b
static DEVCONSOLEMANAGER_API double Add(double a, double b);
static DEVCONSOLEMANAGER_API bool CheckforValidFile(CString fileName);
};
}
DeveloperConsoleManager.cpp
// DeveloperConsoleManager.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "DeveloperConsoleManager.h"
namespace DeveloperConsoleManager
{
double DeveloperConsoleLogic::Add(double a, double b)
{
return a + b;
}
bool DeveloperConsoleLogic :: CheckforValidFile(CString fileName)
{
return false;
}
}
I use these methods in a .cpp file in a different project (type: Application (.exe)). When I Build the solution, there are following linker errors
Warning 1 warning C4273: 'DeveloperConsoleManager::DeveloperConsoleLogic::Add' : inconsistent dll linkage e:\md_69\developerconsolemanager\developerconsolemanager.cpp 10
Warning 2 warning C4273: 'DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile' : inconsistent dll linkage e:\md_69\developerconsolemanager\developerconsolemanager.cpp 16
Error 3 error LNK2028: unresolved token (0A0004F1) "public: static bool __cdecl DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile(class ATL::CStringT > >)" (?CheckforValidFile#DeveloperConsoleLogic#DeveloperConsoleManager##$$FSA_NV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function "public: void __thiscall CSaSsiConsoleUi::UploadSsiCheck(void)" (?UploadSsiCheck#CSaSsiConsoleUi##$$FQAEXXZ) E:\MD_69\DeveloperConsoleUI\SaSsiConsoleUI.obj
Error 4 error LNK2019: unresolved external symbol "public: static bool __cdecl DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile(class ATL::CStringT > >)" (?CheckforValidFile#DeveloperConsoleLogic#DeveloperConsoleManager##$$FSA_NV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function "public: void __thiscall CSaSsiConsoleUi::UploadSsiCheck(void)" (?UploadSsiCheck#CSaSsiConsoleUi##$$FQAEXXZ) E:\MD_69\DeveloperConsoleUI\SaSsiConsoleUI.obj
Error 5 error LNK1120: 2 unresolved externals E:\MD_69\Debug\DeveloperConsoleUi.exe
There is no linker error for the "Add" method.
I have already included "DeveloperConsoleManager.lib" in Linker -> Input -> Additional Dependencies. Please help me find out what exactly am I doing wrong.
I would be glad to add any additional information needed.
Thanks to #Igor Tandetnik and the awesome thing that is internet, I figured it out. I am adding it as an answer so that some one else might benefit.
The problem was with CString. The project in which the function was defined was a dynamic link library (dll) and the call was being made from an MFC application. Now, the issue was that, MFC uses for CString while the non-MFC dll uses .
CString in is defined as:
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
while in is defined as:
typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CString;
This, as you can clearly see is different. The workaround I used was using CAtlString instead of CString . However, please feel free to suggest any better way if you come across.

Create & Using Dll's On MFC Application

I just created dll project in Visual Studio 2013:New Project->MFC DLL->Next->Check "MFC Extention DLL" and finish.Now, I add new class:
class CMyTest
{
public:
CMyTest();
~CMyTest();
int Test(){ return 1; }
};
Next, I compiled the project and got .lib,.dll files.
In the another project (who using the dll's) I just add the include,lib directory and copy the .dll file to the .exe file location and add the .lib file to Additional Dependency on Linker->Input.
Now, i just create some object from CMyTest class on my OnInitDialog() Method:
CMyTest x;
And when I tried to compiled the project I got Link error:
Error 3 error LNK2019: unresolved external symbol "public: __cdecl CMyTest::CMyTest(void)" (??0CMyTest##QEAA#XZ) referenced in function "protected: virtual int __cdecl CUsingDllProjectDlg::OnInitDialog(void)" (?OnInitDialog#CUsingDllProjectDlg##MEAAHXZ) C:\Users\user\documents\visual studio 2013\Projects\UsingDllProject\UsingDllProject\UsingDllProjectDlg.obj UsingDllProject
Error 4 error LNK2019: unresolved external symbol "public: __cdecl CMyTest::~CMyTest(void)" (??1CMyTest##QEAA#XZ) referenced in function "protected: virtual int __cdecl CUsingDllProjectDlg::OnInitDialog(void)" (?OnInitDialog#CUsingDllProjectDlg##MEAAHXZ) C:\Users\user\documents\visual studio 2013\Projects\UsingDllProject\UsingDllProject\UsingDllProjectDlg.obj UsingDllProject
Where is the problem?
You need to declare the Test method like that (and also the ctr,dctr):
__declspec(dllexport) int Test(){ return 1; }
__declspec(dllexport), instruct the linker to export a symbol to a DLL.you can read about that here: https://msdn.microsoft.com/en-us/library/dabb5z75(VS.80).aspx

Unresolved External Symbol- Error in guide?

So I've been trying to fix a weird bug in a game engine SDK where the Windows loading cursor is used instead of the game's own cursor.
The fix for this is here: http://www.crydev.net/wiki/index.php/Use_Custom_Cursor#Step_1:_Fixing_The_Cursor_Bug.
I have followed the fix, but I keep getting these when building the game DLL:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall MODCursor::MODCursor(void)" (??0MODCursor##QAE#XZ) referenced in function "public: __thiscall CGame::CGame(void)" (??0CGame##QAE#XZ) C:\Users\User\Desktop\Crytek\Mods\CryEngine2\Code\Game.obj GameDll
Error 2 error LNK2019: unresolved external symbol "public: __thiscall MODCursor::~MODCursor(void)" (??1MODCursor##QAE#XZ) referenced in function "public: virtual __thiscall CGame::~CGame(void)" (??1CGame##UAE#XZ) C:\Users\User\Desktop\Crytek\Mods\CryEngine2\Code\Game.obj GameDll
Yeah, normally I can fix this issue quite easily by defining the class properly, but it hasn't worked in this case. What can I be doing wrong?
The files are as they are in the guide for the fix, so there isn't really any point in posting the files here since they would be a waste of space on here. If the files are really needed to investigate this issue, I'll upload them if anyone requests them.
Perhaps there's an error within the fix itself? One possible difference from the fix to my build is that the fix is using Visual Studio 2008, I am using Visual Studio 2013.
Maybe try to put it all inside .h file:
#ifndef _MOD_CURSOR
#define _MOD_CURSOR
#include <windows.h>
#include "resource.h"
#undef GetUserName // This is a macro in windows.h, gives issues with GetUserName() of ISystem
class MODCursor : public ISystemEventListener
{
public:
MODCursor() {
gEnv->pSystem->GetISystemEventDispatcher()->RegisterListener(this);
m_cursor = LoadCursor((HINSTANCE)g_hInst, MAKEINTRESOURCE(IDC_CURSOR1));
SetCursor(m_cursor);
}
~MODCursor(){
gEnv->pSystem->GetISystemEventDispatcher()->RemoveListener(this);
}
private:
virtual void OnSystemEvent( ESystemEvent event,UINT_PTR wparam,UINT_PTR lparam ) {
if(event == ESYSTEM_EVENT_TOGGLE_FULLSCREEN || event == ESYSTEM_EVENT_RESIZE || event == ESYSTEM_EVENT_CHANGE_FOCUS){
if (m_cursor != GetCursor())
SetCursor(m_cursor);
}
}
HCURSOR m_cursor;
};
#endif

C++ LNK2019 error with constructors and destructors in derived classes

I have two classes, one inherited from the other. When I compile, I get the following errors:
Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base#Parsables#Utility##QAE#XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Entity#Parsables#Utility##QAE#XZ)
Entity.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Utility::Parsables::Base::~Base(void)" (??1Base#Parsables#Utility##UAE#XZ) referenced in function "public: virtual __thiscall Utility::Parsables::Entity::~Entity(void)" (??1Entity#Parsables#Utility##UAE#XZ)
D:\Programming\Projects\Caffeine\Debug\Caffeine.exe : fatal error LNK1120: 2 unresolved externals
I really can't figure out what's going on.. can anyone see what I'm doing wrong? I'm using Visual C++ Express 2008. Here are the files..
"include/Utility/Parsables/Base.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_BASE_HPP
#define CAFFEINE_UTILITY_PARSABLES_BASE_HPP
namespace Utility
{
namespace Parsables
{
class Base
{
public:
Base( void );
virtual ~Base( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_BASE_HPP
"src/Utility/Parsables/Base.cpp"
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
Base::Base( void )
{
}
Base::~Base( void )
{
}
}
}
"include/Utility/Parsables/Entity.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#define CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
class Entity : public Base
{
public:
Entity( void );
virtual ~Entity( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
"src/Utility/Parsables/Entity.cpp"
#include "Utility/Parsables/Entity.hpp"
namespace Utility
{
namespace Parsables
{
Entity::Entity( void )
{
}
Entity::~Entity( void )
{
}
}
}
The relevant bit is this:
unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)"
You need to provide a definition for Base::Base and Base::~Base. A declaration is not good enough. Even if you have nothing to do in either function, you need to leave an empty function body, because C++ actually requires the function to exist. C++ puts things like virtual table maintenance inside your constructors and destructors, so they must be defined even if you don't need to do anything there -- C++ has to do things in there.
Are you sure Base.cpp is being included in the build?
Just encountered this exact same error today in Visual Studio 2015. Unfortunately the accepted answer didn't worked (as well as answers from many same questions). The thing that worked for me was right click on the base class cpp file, exclude and then include it again. I think somehow VS got confused while moving file around and renames and it just silently refused to compile it even though it was marked as "Included In project" = true in property editor as well as listed in vcproj file in group. This is horrible error and ended up spending good hour on it.
Either your base.cpp is not being compiled/linked or you have a misspelling in it