I'm experimenting around with dll loading on runtime and I have a problem: I have a little helper class which is istanciated in the main program. The pointer to this object is handed to the loaded dll. To test it, i want to call a function from the class (printLine). But I can't compile the dll because I get a:
Utility.o: In function `ZN7Utility6onInitEv':
D:\Benutzer\Jan\Desktop\Programmierprojekte\Game Engine 5.0\Utilities\Debug/../Utility.cpp:7: undefined reference to `ModuleHelper::printLine(std::string)'
collect2.exe: error: ld returned 1 exit status
These two files are the only content of the dll:
Utility.h:
#ifndef UTILITY_H_
#define UTILITY_H_
#include <iostream>
#include <Definitions.h>
#include <ModuleHelper.h>
class Utility
{
public:
Utility();
~Utility();
static void onInit();
static void onUpdate();
static char* getName();
static char** getDependencies();
static int getCountDependencies();
static char* getServeAs();
static void setModuleHelper(ModuleHelper* helper);
private:
static constexpr char* name = "Utility";
static constexpr char** dependencies = nullptr;
static constexpr int countDependencies = 0;
static constexpr char* serveAs = "";
static ModuleHelper* moduleHelper;
};
extern "C" //GAME_API is a dllexport macro
{
char* GAME_API getName()
{
return Utility::getName();
}
char** GAME_API getDependencies()
{
return Utility::getDependencies();
}
int GAME_API getCountDependencies()
{
return Utility::getCountDependencies();
}
char* GAME_API getServeAs()
{
return Utility::getServeAs();
}
noargfunc GAME_API onInit()
{
return Utility::onInit;
}
noargfunc GAME_API onUpdate()
{
return Utility::onUpdate;
}
void GAME_API setModuleHelper(ModuleHelper* moduleHelper)
{
Utility::setModuleHelper(moduleHelper);
}
}
#endif /* UTILITY_H_ */
Utility.cpp:
#include "Utility.h"
ModuleHelper* Utility::moduleHelper; //with "= nullptr" or "= NULL" it didn't work either
void Utility::onInit()
{
moduleHelper->printLine("Hello from Utilities"); //wrapper for std::cout
}
void Utility::onUpdate()
{
}
char* Utility::getName()
{
return name;
}
char** Utility::getDependencies()
{
return dependencies;
}
int Utility::getCountDependencies()
{
return countDependencies;
}
char* Utility::getServeAs()
{
return serveAs;
}
void Utility::setModuleHelper(ModuleHelper* helper)
{
moduleHelper = helper;
}
An undefined reference means that the IMPLEMENTATION is not found.
it looks like you just included the header file in order to use the library.
The main problem (the linker error - not runtime!) is that you may forgot to link the library. (Project References in Visual Studio)
Anyway you will find your next error if you CAN compile and link your code.
ModuleHelper* Utility::moduleHelper; //with "= nullptr" or "= NULL" it didn't work either
if you initialize moduleHelper with NULL and then dereference the pointer with "->" trying to do sth you will get a null-pointer exception. You have to initialize it... (could be = new ModuleHelper). Since I don't know the used library you have to read the documentation yourself.