In the general case, you cannot do what you want in portable C11 (read n1570 ), as it is explained by others: function names are compilation time and are unknown at runtime. The C programming language does not have reflection and / or introspection . BTW, a function may even “disappear” during optimized compilation (so that “practically” does not exist at run time, but your program behaves as-if this function exists), in the sense that it was inlined or removed from executable . However, C has function pointers (the type of which describes the signature of an indirectly called function); practically speaking, they point to machine code .
But if you encoded an application program on an x86-64 computer, such as Linux, sometimes you can use some workarounds using some dirty tricks specific to this operating system and instruction set architecture :
for some string of type "functionOne" (practically, of type const char* ), you can get a pointer to it (for example, functionOne , if it has an extern linkage ) - through dynamic means of communication - using dlopen (3) with NULL , then dlsym (3) . BTW reverse mapping (from address to name) is available through dladdr (3) .
a function pointer is provided (or virtually any real address in a virtual address space that points inside some executable code segment ), you could call it indirectly if the signature of this function is known at compile time (indicated in the type of this function pointer).
if you want to call an arbitrary function with an arbitrary signature and arbitrary arguments known only at runtime, you can use libffi . He knows the ABI of your system.
a possible trick is also to emit some temporary file /tmp/emittedcode.c containing C code (at runtime), fork the compilation process into a temporary plugin (e.g. gcc -Wall -O -shared -fPIC/tmp/emittedcode.c -o /tmp/emittedplugin.so ) and dynamically load this temporary plugin /tmp/emittedplugin.so with dlopen (3) . Be sure to clear the clutter (for example, delete all temporary files, possibly using atexit (3) ) at the end of the program.
perhaps you want to generate some machine code at runtime; then consider also some JIT compilation , such as GCCJIT , LLVM , libjit , asmjit .
If your computer is not running Linux, you can find equivalent information for your OS and computer. Read Operating Systems: Three Simple Pieces to learn more about the OS as a whole. Read the documentation for your specific OS (for Linux, first read ALP or the new Linux programming book, then intro (2) , syscalls (2) , elf (5) and related pages).
By the way, if you just want to call functions from your program (from the names in some input file), you can instead construct a hash table (or map , possibly a red-black tree ), connecting function names to function pointers, instead as suggested in this other answer.
Perhaps you need some kind of homoiconic programming language with some eval primitive . Take a look at generic Lisp. Remember SBCL , it compiles a maximum REPL into dynamically generated machine code.
Perhaps you are writing an interpreter (which is often more complicated and time consuming than you think, read the Dragon Book ). Consider embedding and using an existing one, such as Guile or Lua .
source share