unresolved external symbol in C++ managed - visual-studio-2012

I have C++ managed dll compiled with /clr that has some global functions.
for example
void Managed2UnManaged(DataStructures::AAA^ elem, DataStructures::CPP::AAA* copy_elem);
when I try to use these functions in a C++ managed console application I get the following errors
error LNK2028: unresolved token (0A00048C) "void __clrcall Managed2UnManaged(class DataStructures::AAA ^,class DataStructures::CPP::AAA *)" (?Managed2UnManaged##$$FYMXP$AAVAAA#DataStructures#NextIn##PAV1CPP#23##Z)
error LNK2019: unresolved external symbol "void __clrcall Managed2UnManaged(class DataStructures::AAA ^,class DataStructures::CPP::AAA *)" (?Managed2UnManaged##$$FYMXP$AAVAAA#DataStructures#NextIn##PAV1CPP#23##Z)
I added to the console application a reference to the dll but it still gives the error.
I also tried adding __declspec(dllexport) but then I get
error C3395: 'Managed2UnManaged' : __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention
and if I specify __stdcall I get
error C4439: 'Managed2UnManaged' : function definition with a managed type in the signature must have a __clrcall calling convention
I am using VS2012.
the h file has
class XXX {
public:
static void Managed2UnManaged(DataStructures::RegResult^ elem, DataStructures::CPP::RegResult* copy_elem);
};
and the cpp file
void XXX::Managed2UnManaged(DataStructures::RegResult^ elem, DataStructures::CPP::RegResult* copy_elem)
{
}
Problem Solved. the problem was that I also needed to declare the class public ref. this caused a different problem because that the native c++ types are private. so I had to use the make_public pragma.
Thanks all.

I think that the problem may be that the CLR doesn't allow global functions.
You must create a managed class and write your function as a static member function of that class, for that function to be exported.

Related

visual studio/ C++ unresolved external symbol: public static class

I'm a newbee in C++,
I'm trying to make a library for my program, and I've got this problem.
I have a .h file for declaration member functions.
class Quaternion
//qua.h
{
private:
public:
inline static Quaternion M2Qua(Matrix&);
}
a matrix.h file for declaration matrix class. And an Qua.cpp file for the define function, and I want to link the M2Qua function in the qua.h file.
//Qua.cpp
Quaternion Quaternion::M2Qua(Matrix& R)
{
}
When I called the function in my main program, the compiler printed an error.
the error is:
LNK 2019: unresolved external symbol "public: static class Quaternion _cdecl Quaternion::M2Qua(class Matrix&)
I read some relevant topics but I can't figure out where exactly the problem is. I just suppose it relates to the static member, that I declared in qua.h file. I hope someone helps me. Thanks in advance.

SFML can't create static sf::SoundBuffer* class variable?

I am using SFML's audio libraries.
In my class I tried to have a private static variable that holds a SoundBuffer* as such:
static sf::SoundBuffer* breakBlockBuffer;
This gives a linker error when I try to build:
Chunk.obj : error LNK2001: unresolved external symbol "private: static class sf::SoundBuffer * Chunk::breakBlockBuffer" (?breakBlockBuffer#Chunk##0PAVSoundBuffer#sf##A)
myprogram.exe : fatal error LNK1120: 1 unresolved externals
However, if I declare it as an instance variable, it works:
sf::SoundBuffer* breakBlockBuffer;
This happens regardless of whether I dynamically link or statically link the libraries. It also persists in both SFML1.6 and SFML2.0.
I'm feeling that I might have made some silly mistake since I'm pretty rusty at C++, but the code looks like it should be ok to me.
If you declare a static variable in your class, you must also define it on your source file:
foo.hpp:
class foo
{
static int bar;
};
foo.cpp:
int foo::bar;
Looks like you didn't do it.

Multiply Defined Symbols with Precompiled Header?

Well, I have been struggling with this for days now. I am writing a custom game DLL for CryENGINE from scratch, and I cannot even get the solution compile with one simple class (Game.cpp) and a precompiled header (StdAfx.h).
Both Game.cpp and StdAfx.cpp will compile perfectly on their own, but compiling the solution throw tons of multiply defined errors. The class is simple because the definitions are just placeholders.
Game.h
#if !defined __GAME__H__
#define __GAME__H__
#pragma once
class CGame : public IGame
{
public:
CGame();
VIRTUAL ~CGame();
//IMPLEMENT: IGame Interface, all methods declared.
};
#endif
Game.cpp
#include "StdAfx.h" //PreComp header
#include "Game.h"
//Define all methods, each one has a simple definition.
StdAfx.h
#if !defined __STDAFX__H__
#define __STDAFX__H__
#pragma once
//Various CryENGINE includes
#endif
Output
error LNK2005: "struct SSystemGlobalEnvironment * gEnv" (? gEnv##3PEAUSSystemGlobalEnvironment##EA) already defined in StdAfx.obj
error LNK2005: "public: static long volatile _CryMemoryManagerPoolHelper::allocatedMemory" (?allocatedMemory#_CryMemoryManagerPoolHelper##2JC) already defined in StdAfx.obj
error LNK2005: "public: static long volatile _CryMemoryManagerPoolHelper::freedMemory" (?freedMemory#_CryMemoryManagerPoolHelper##2JC) already defined in StdAfx.obj
error LNK2005: "public: static long volatile _CryMemoryManagerPoolHelper::requestedMemory" (?requestedMemory#_CryMemoryManagerPoolHelper##2JC) already defined in StdAfx.obj
error LNK2005: "public: static int volatile _CryMemoryManagerPoolHelper::numAllocations" (?numAllocations#_CryMemoryManagerPoolHelper##2HC) already defined in StdAfx.obj
The list goes on...
What really throws me off is that each one will compile just fine individually, so syntax and references are good. What could possibly cause multiply defined errors when the solution is compiled as a whole?
I really appreciate help on this frustrating issue, thank you.
I'm not sure the errors are caused by the precompiled header, but here is the correct way to set up the precompiled header:
Right-click on the project name in Solution Explorer, select Properties, go to Configuration Properties | C/C++ | Precompiled Headers and set the Precompiled Header setting to Use (/Yu). Leave the other two settings below it to the default.
Right-click on StdAfx.cpp, go to the same setting and set it to Create (/Yc).
Well, I figured it out. There is a clever complex include that does not belong in the precompiled header:
#include <Platform_Impl.h>
This was causing all my problems, and by moving it to Game.cpp, everything is fine.

error LNK: unresolved external symbol, resulting from virtual functions

Following are the errors when I compile my code. I am sorry that I cannot share my code here. FYI, I don't use any externals libs, so I am really not sure about these linker errors. They seem to come from the virtual functions I declared in DeviceManager class. Their respective implementations are in two other classes.
Could someone help me out with these errors?
1>device_manager.obj : error LNK2028: unresolved token (0A000397) "public: virtual void __clrcall DeviceManager::loadConfig(class System::IO::StreamReader ^)" (?DeviceManager#DeviceManager##$$FUAMXP$AAVStreamReader#IO#System###Z) referenced in function "void __clrcall `dynamic initializer for 'const DeviceManager::`vftable'''(void)" (???__E??_7DeviceManager##6B###YMXXZ#?A0xc2524ebc##$$FYMXXZ)
1>device_manager.obj : error LNK2019: unresolved external symbol "public: virtual void __clrcall DeviceManager::loadConfig(class System::IO::StreamReader ^)" (?loadConfig#DeviceManager##$$FUAMXP$AAVStreamReader#IO#System###Z) referenced in function "void __clrcall `dynamic initializer for 'const DeviceManager::`vftable'''(void)" (???__E??_7DeviceManager##6B###YMXXZ#?A0xc2524ebc##$$FYMXXZ)
1>device_manager.obj : error LNK2001: unresolved external symbol "public: virtual class Device * __thiscall DeviceManager::createDevice(void)" (?createDevice#DeviceManager##UAEPAVDevice##XZ)
1>device_manager.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall DeviceManager::initializeDevices(void)" (?initializeDevices#DeviceManager##UAEXXZ)
1>C:\Users\CH\Documents\Visual Studio 2010\Projects\Test Devices\Test Devices\Debug\Test Devices.exe : fatal error LNK1120: 4 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.50
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Update 1 (Possible Solution):
The problem seems to be because of the virtual function(s) declared in DeviceManagerclass not being pure virtual and not having definition(s) in the .cpp of DeviceManager class.
If the DeviceManagerclass does not have any non-virtual function(s), then making the virtual function(s) pure will be the solution. Something like this virtual void testFunction(); is not pure, so changing it to virtual void testFunction()=0; is the solution. For this, it is enough to have the definition(s) of this virtual function implemented in other classe(s) that inherit the DeviceManager class. In such a case, the DeviceManagerclass becomes abstract and therefore cannot be instantiated.
But in my case, the DeviceManagerclass has some non-virtual functions as well and also needs to be instantiated to call the non-virtual functions. Therefore, the solution is to implement the definitions of the virtual functions in the .cpp of the DeviceManagerclass apart from declarations and definitions in other classes that inherit the DeviceManager class.
Have you tried a full clean and rebuild?
Does your class have at least one non virtual member function?
I have seen that issue a few years ago.
Other than that I suggest copying the function prototype and full parameter list out into word pad or something and changing the font (to stop it looking so familiar that your eyes scan over it) and then comparing every call to every function to make sure that they match up(more likely to cause a complile problem but is worth checking).

Export native type from C++/CLI project

How do I export the methods of a native class defined in a C++/CLI project? Here's what I have:
The .h file:
#pragma once
#ifdef COMPILE_PRODUCER_LIB
#define PRODUCER_LIB_EXPORT __declspec(dllexport)
#else
#define PRODUCER_LIB_EXPORT __declspec(dllimport)
#endif
public class PRODUCER_LIB_EXPORT MySecondNativeClass {
public:
MySecondNativeClass(int val);
int getValue() const;
private:
int m_value;
};
The .cpp file:
#include "stdafx.h"
#include "MySecondNativeClass.h"
MySecondNativeClass::MySecondNativeClass(int val) {
this->m_value = val;
}
int MySecondNativeClass::getValue() const {
return this->m_value;
}
Using this class in the same project works fine, but using it in another (C++/CLI) project gives me "unresolved external symbol" linker errors. (COMPILE_PRODUCER_LIB is a preprocessor definition defined only in the project that contains the class.)
I've created a small solution (for Visual Studio 2010) that shows the problem (download at the end of this page). There, the native class MySecondNativeClass is defined in the project "ManagedProviderLib" and is being used in "ExternalTestClass.cpp" (in project "ManagedExternalLib").
Your DLL project creates a .lib file in the build directory, the DLL's import library. You need to specify that .lib file in the other project, Linker + Input, Additional Dependencies setting.
Note that Add Reference cannot work, the assembly metadata only contains definitions for managed classes. Your class is native C++, not managed. Also make sure that your .cpp file is compiled without /clr in effect. The C++/CLI compiler will happily translate native C++ classes to IL but that is not very efficient. You can control this within the source code file with #pragma managed.

Resources