|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
c programming - Modules
I'm in the process of planning an application which i intend on marketing and I'd like to implement modules that can be loaded dynamically into the application.
For example, someone buys the core system and wants to add ... say MP3 support, so they'd buy this module and install it. How would something like this actually work ? Would this be achieved using 'dlopen' ?
__________________
"No, that's wrong, Cartman. But don't worry, there are no stupid answers, just stupid people." -- Mr. Garrison Forum Netiquette Last edited by corey_james; 26th October 2008 at 09:36 AM. |
|
|||
Well, i have had a play and i think using dlopen and dlsym is the best way to go about this. Here's some examples if anyone is interested:
libtest.c Code:
#include <stdio.h> void testfunc() { printf("Test plugin\n"); } gcc -fPIC -g -c -Wall libtest.c main.c Code:
#include <stdio.h> #include <dlfcn.h> int main () { void *handle = NULL; void (*testlib)(void) = NULL; handle = dlopen("./libtest.so" ,RTLD_LAZY); testlib = dlsym(handle, "testfunc"); if ( testlib == NULL ) { printf("Error: %s \n", dlerror()); } else { testlib(); } } making the library into a shared library ( required ) Code:
gcc -shared -Wl,-soname,libtest.so.1 -o libtest.so libtest.o -lc
__________________
"No, that's wrong, Cartman. But don't worry, there are no stupid answers, just stupid people." -- Mr. Garrison Forum Netiquette |
|
||||
Yes, and on windows it would be achieved with LoadLibrary, GetProcAddress, and FreeLibrary.
(Since it seems you plan to market this application I can only assume Windows is a target for you too) The basic implementation is: 1) open the library (dlopen/LoadLibrary) 2) get the pointer to the function you want to get from the module, IE "filetype_handler" (dlsym/GetProcAddress) 3) use the function as needed 4) at shutdown or when the module is no longer needed, close the module (dlclose/FreeLibrary). |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
NetBSD New kernel modules in NetBSD-CURRENT (instead LKM) | vermaden | News | 1 | 17th May 2015 11:06 PM |
Problem setting up perl modules | badguy | OpenBSD Installation and Upgrading | 4 | 10th August 2009 09:46 PM |
kernel modules | Mr-Biscuit | FreeBSD General | 0 | 2nd March 2009 06:18 AM |
What's the difference between these two memory modules? | Carpetsmoker | General Hardware | 2 | 25th November 2008 03:13 PM |
Are certain kernel modules permanent? | davidgurvich | FreeBSD General | 3 | 6th June 2008 06:14 PM |