wohoo, reviving an old thread
i've been using the live coding quite a lot, still the style where i only change a small part of the application but leave the main ofApp untouched.
i used to do (roughly this):
- compile app in xcode, launch it
- if runtimeCPP detects a change then create a shared lib that statically links to: openFrameworks.a, rtAudio.a, etc.
- load shared lib
and here's the new way:
- compile app with "-rdynamic" to export all symbols (like ofDrawLine) and launch
- when a change is detected compile the shared (runtime) part of the application without (!) linking to anything. this gives a ton of linker errors. disable them with clang's "-undefined dynamic_lookup" flag
- load the shared library. upon loading all the symbols will resolve into the main application thats already running
the biggest advantages are:
- a bit faster and smaller shared library
- library calls (like ofGetMouseX() finally work)
so this help won't change much if you want to have the entire ofApp reload. but it's quite neat for having small bits of changeable code (e.g. i'm using it for audio processing).
here is the basic principle in a stripped down C example:
////// main.c
#include <stdio.h>
#include <dlfcn.h>
void exported_callback(){
printf("Hello from callback!\n");
}
typedef void (*lib_func)();
int main(int argc, const char *argv[]){
printf("Hello from main!\n");
void *handle = dlopen("./libprog.dylib", RTLD_NOW | RTLD_GLOBAL);
lib_func func = dlsym(handle, "library_function");
func();
return 0;
}
////// lib.c
#include <stdio.h>
void exported_callback();
void library_function()
{
printf("Hello from library!\n");
exported_callback();
}
////// COMPILE
clang -rdynamic main.c -o prog -ldl
clang -undefined dynamic_lookup --shared -fPIC lib.c -o libprog.dylib
/////// OUTPUT
./prog
Hello from main!
Hello from library!
Hello from callback!
the code comes quite directly from here: http://stackoverflow.com/a/17083153/347508