Who needs a scripting language? When compiled C will do.
Posted: Fri Apr 09, 2010 10:14 am
Okay, well maybe thats a little miss leading, and yes, perhaps there are things this following method can't do that you can in some scripting language.
This method however does allow you to dynamically use compiled C code, WITHOUT changing the codebase.
What we are going to be using is called dynamic linking, dlfcn.h is the header that will give us all the goodies to do this.
NOTE : This will require you to do some planning because you have to have a alias to get to the code in in the library you will dynamically linked.
lets look at this code.
dynamic.h
dynamic.cpp
Okay, so thats pretty straight forward, we compile this code into a dynamic library, that would be dll/so/dylib for window/linux/osx, respectively.
i called it dynamic and my particular extension was dylib.(OSX)
Now for the main code.
main.cpp
the most important line is, calc = (int(*)(int)) dlsym(handle, "calc");
here it looks for a function called calc in the library that we loaded, (well the extern C bit, but yanno).
and well the rest is pretty straight forward.
on a side note i have been trying to do it where you have a single base class with a few smart virtual functions, then the libraries would have inherited that, so that you only use say cNPC in the main program, but you can extend the code base dynamically by making class cDragon : public cNPC or something like that. but im having a little issue where it will load fine but its crashing once i call functions on the NPC class that are virtual. anyways.
if you spend some time and make a few functions that will stay consistent you can really use this to your advantage.
you can change entire render systems and so on, make expansions like this, levels, weapons, the possibilities really are endless. and the best part of all is that its compiler, so its fast, and you dont have to change your codebase.
This method however does allow you to dynamically use compiled C code, WITHOUT changing the codebase.
What we are going to be using is called dynamic linking, dlfcn.h is the header that will give us all the goodies to do this.
NOTE : This will require you to do some planning because you have to have a alias to get to the code in in the library you will dynamically linked.
lets look at this code.
dynamic.h
Code: Select all
#ifndef _dynamic_h
#define _dynamic_h
extern "C" int calc(int a);
#endif
Code: Select all
#include <stdio.h>
#include "dynamic.h"
int calc(int a) {
return a*a;
}
i called it dynamic and my particular extension was dylib.(OSX)
Now for the main code.
main.cpp
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(void)
{
void *handle;
int (*calc)(int);
handle = dlopen("dynamic.dylib", RTLD_NOW);
if(handle == NULL) {
printf("%s\n", dlerror());
exit(1);
}
calc = (int(*)(int)) dlsym(handle, "calc");
if(dlerror() != NULL)
{
printf("%s\n", dlerror());
exit(-1);
}
for(int a = 1;a <= 5;a++)
printf("%d squared = %d\n", a, calc(a));
dlclose(handle);
return 0;
}
here it looks for a function called calc in the library that we loaded, (well the extern C bit, but yanno).
and well the rest is pretty straight forward.
on a side note i have been trying to do it where you have a single base class with a few smart virtual functions, then the libraries would have inherited that, so that you only use say cNPC in the main program, but you can extend the code base dynamically by making class cDragon : public cNPC or something like that. but im having a little issue where it will load fine but its crashing once i call functions on the NPC class that are virtual. anyways.
if you spend some time and make a few functions that will stay consistent you can really use this to your advantage.
you can change entire render systems and so on, make expansions like this, levels, weapons, the possibilities really are endless. and the best part of all is that its compiler, so its fast, and you dont have to change your codebase.