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.
Related
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.
I am new to c++ programming. I am trying few things
I tried using directive in following way as shown in below program just for trail it is throwing error
IDE used:VS 2015
language :VC++
Project type: library
Error occured is Error: Expected an identifier
This is in Stdafx.h
#define MANAGED_PUBLIC public ref
This is in trail.h
using namespace System;
namespace trail {
MANAGED_PUBLIC class Class1
{
// TODO: Add your methods for this class here.
};
}
I wanted to use MANAGED_PUBLIC instead of using public ref each and every time in whole project
You cannot do that. public ref is a context sensitive keyword. You cannot bury it down under a macro. C++/CLI compiler would process it differently than a regular compiler hence the macro outcome won't be public ref. You must type it everywhere.
You can use two macros:
#define MANAGED_PUB public
#define MANAGED_REF ref
MANAGED_PUB MANAGED_REF class Class1
{
// TODO: Add your methods for this class here.
};
You might try a compiler flag: -DMANAGED_PUBLIC="public ref" for your legacy code (quotes are stripped according to msdn).
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.
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.
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).