-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
40 lines (29 loc) · 784 Bytes
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "compile.h"
#include "shared-library.h"
static const char * const libname = "/tmp/libfoo.so";
static const char * const funcname = "foo";
int main (int argc, char *argv[])
{
void * lib = NULL;
int (*fun) ();
struct compiler * compiler;
int rc = EXIT_FAILURE;
char prog[] = "int foo () { return 42; }";
compiler = compiler_get_gcc ();
if (comp_failed == compile_from_string (compiler, prog, libname))
goto out;
if (! (lib = load_library (libname)))
goto out;
fun = (int (*) ()) get_function (lib, funcname);
if (!fun)
goto out;
printf ("function '%s' from '%s' returned '%d'\n", funcname, libname, fun ());
out:
if (lib)
if (close_library (lib))
rc = EXIT_SUCCESS;
return rc;
}