K
Kaba
Hi,
Have a look at the following:
aFile.cpp
---------
#include <iostream>
#include <string>
namespace
{
void libraryAddFunction(
const std::string& name, void (*function)());
struct CallFunction
{
CallFunction(void (*callback)()) {callback();}
};
void aFunction()
{
std::cout << "aFunction" << std::endl;
}
void addFunction()
{
libraryAddFunction("aFunction", aFunction);
}
CallFunction call(addFunction);
}
register.cpp
------------
#include <string>
#include <map>
typedef std::map<std::string, void (*)()> FunctionMap;
FunctionMap& functionMap()
{
static FunctionMap theFunctionMap;
return theFunctionMap;
}
void libraryAddFunction(const std::string& name,
void (*function)())
{
functionMap().insert(
std::make_pair(name, function));
}
void runFunction(const std::string& name)
{
if (functionMap().count(name))
{
functionMap()[name]();
}
}
I then compile aFile.cpp and register.cpp into a static library, say
library.lib. Next I build the following trivial main along with the
library.lib:
main.cpp
--------
#include <string>
void runFunction(const std::string& name);
int main()
{
runFunction("aFunction");
return 0;
}
And the result is... That nothing is printed. The first question is: why
not? When all of the three files are built together, then the result is
the string "aFunction" printed on screen.
The idea here is that the aFile.cpp is kind of an independent component,
such that similar files could be added which register their
functionality automatically. The second question is: how to recover the
self-registration property in the library case?
Have a look at the following:
aFile.cpp
---------
#include <iostream>
#include <string>
namespace
{
void libraryAddFunction(
const std::string& name, void (*function)());
struct CallFunction
{
CallFunction(void (*callback)()) {callback();}
};
void aFunction()
{
std::cout << "aFunction" << std::endl;
}
void addFunction()
{
libraryAddFunction("aFunction", aFunction);
}
CallFunction call(addFunction);
}
register.cpp
------------
#include <string>
#include <map>
typedef std::map<std::string, void (*)()> FunctionMap;
FunctionMap& functionMap()
{
static FunctionMap theFunctionMap;
return theFunctionMap;
}
void libraryAddFunction(const std::string& name,
void (*function)())
{
functionMap().insert(
std::make_pair(name, function));
}
void runFunction(const std::string& name)
{
if (functionMap().count(name))
{
functionMap()[name]();
}
}
I then compile aFile.cpp and register.cpp into a static library, say
library.lib. Next I build the following trivial main along with the
library.lib:
main.cpp
--------
#include <string>
void runFunction(const std::string& name);
int main()
{
runFunction("aFunction");
return 0;
}
And the result is... That nothing is printed. The first question is: why
not? When all of the three files are built together, then the result is
the string "aFunction" printed on screen.
The idea here is that the aFile.cpp is kind of an independent component,
such that similar files could be added which register their
functionality automatically. The second question is: how to recover the
self-registration property in the library case?