0

Possible Duplicate:
Dynamically creating functions in c

Here is an example of what I'd like to do:

void  attribute((constructor)) someFunction() {
    // Would be nice to define C function "someFunction2" somehow here.
}

I know class_addMethod allows adding C functions to Objective-C classes during runtime.

Is it possible to add C function to C main space?

Please, don't tell me I'm wrong if I'm thinking about this way of doing things - I am interested in it rather for educational purposes.

Community
  • 1
  • 1
Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
  • http://stackoverflow.com/questions/1839965/dynamically-creating-functions-in-c – A.B. Dec 30 '12 at 06:59
  • you could use existing source as a template, add the function, call a compiler using system and then invoke the new executable – PeterJ Dec 30 '12 at 07:04
  • 1
    You should really elaborate on your intension as the question does not make much sense in this bare form. How should your newly created functions be called? Do you want to create the implementation as well? What do you want to achieve? – Nikolai Ruhe Dec 30 '12 at 07:21
  • @Nikolai Ruhe, my similar question: http://stackoverflow.com/questions/14062042/how-to-dynamically-auto-register-c-function-so-it-could-be-available-through-the. – Stanislav Pankevich Dec 30 '12 at 07:43
  • 1
    If you really want help with these questions you should more broadly describe what's the goal of all of this. The way you ask just sounds like "I know C can't do it but how can it be done anyway?" – Nikolai Ruhe Dec 30 '12 at 08:11

1 Answers1

4

No. A C function consists of a name and a body. The compiler transforms the body to a binary piece of executable code that will be mapped to some address when a process is created from the executable. The name is used by the static and dynamic linkers as an alias to this address.

At runtime both concepts aren't really of much interest. The executable is loaded and names are resolved, so there's little use in creating them dynamically.

On iOS it would even be impossible to create new function implementations as the kernel disallows to make memory executable.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200