February 8th, 2014, 07:25 AM
Calling a function whose name is known
Suppose I get the name of a function as some input? How do I call it then?
I have seen this in openGL. You give the name of the function which openGL has to call for any keypress, mouse movement actions. How does it call?
cin >> function_name;
// Now I want to call function_name. How do I do it?
February 8th, 2014, 12:11 PM
It is not that simple. The name of a function (its "symbol") is know to the compiler and linker when the code is built, but the compiled code is resolved to an address and the symbol table discarded.
You have perhaps three options, all but one of which is non-portable and may not be possible. The first is theoretical, the second I have used in embedded systems to create a command shell, the third I have also used for a command shell both on an embedded system, Windows and Linux.
- Build your code with debug information included - this includes the symbol table in the executable, which includes function names and their addresses. The problem is you would need details of the how the debug data is stored in order to make use of it at runtime. It would be compiler specific. Another problem is that the entire symbol table would be exposed and the user could call any function with external linkage.
- Generate a map file for your build and from that generate your own format symbol lookup table as a data file used by your application at runtime to determine the address. This is less compiler dependent, but may be platform dependent. The addresses in the map file may be relocatable (the actual address may be determined at load time, rather than link time) - I have only ever done this in embedded systems with directly located code, I am not sure what Windows or Linux does.
- Create your own symbol table lookup to match command strings to function-pointers. This method requires maintaining the look-up table, but allows you to be completely selective about which functions are exposed, and the command name and the function name need not be the same. Also the functions you map need not have external linkage - they can be local to the module that defines the table.
Handling function parameters is perhaps the tricky part. The way I have handled that is to create a function pointer type that that references a function that returns an int and takes an arbitrarily large number of int parameters (I used 24). When you cast your function address to a pointer of this type, you can pass any number of real parameters up to the maximum allowed by the type, the compiler will generate a larger than necessary call stack, but the unused parameters will be safely ignored. So long as on the target the sizeof a pointer is the same as that of an int, you can even pass strings by casting to int. Similarly is the real function returns nothing, the returned int is simply undefined, and if it returns a pointer, you can cast the int back to a pointer.
Last edited by clifford; February 8th, 2014 at 12:15 PM.
February 8th, 2014, 12:22 PM
Are you sure that is the same thing? That sounds more like a callback to me. A callback is a method of allowing predefined code to call a user defined function. It is a kind of runtime linkage, but it cannot in any portable way be used to translate from a string to a function call.
Originally Posted by Avichal
Either way, both callbacks and what you are seeking to do involve function pointers, so you would do well to research that subject: start here
February 8th, 2014, 12:27 PM
Thanks, callbacks and function pointers is what I was looking for. Thanks for the link. I got it now
February 8th, 2014, 01:23 PM
Sounds like this is solved, but I think it's not so difficult.
You need to know the specific function interface, calling conventions on your operating system/compiler/linker. Supply the functions as a dynamic link library or shared object library, Use the dlopen family of functions.
[/code] are essential for python code and Makefiles!
February 8th, 2014, 03:47 PM
I think I made it more complicated perhaps than necessary. :o Not being a frequent desktop OS developer I forgot about such things. Of course shared and dynamic-link libraries necessarily include their symbol table and OSs that support them have an API for accessing functions by symbol exists.
Originally Posted by b49P23TIvg