#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2

    Calling a function whose name is known


    Suppose I get the name of a function as some input? How do I call it then?

    e.g.:
    Code:
    cin >> function_name;
    // Now I want to call function_name. How do I do it?
    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?
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,116
    Rep Power
    1803
    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.

    1. 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.
    2. 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.
    3. 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 11:15 AM.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,116
    Rep Power
    1803
    Originally Posted by Avichal
    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?
    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.

    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    Thanks, callbacks and function pointers is what I was looking for. Thanks for the link. I got it now
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    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]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,116
    Rep Power
    1803
    Originally Posted by b49P23TIvg
    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.
    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.

IMN logo majestic logo threadwatch logo seochat tools logo