A R
A R

Reputation: 2803

Is there any way to construct a function pointer by reading a file in c++

I have a function prototype in a file. Is there any way to create a function pointer by reading the file at run time in c++?

Upvotes: 0

Views: 147

Answers (3)

user1129665
user1129665

Reputation:

You are properly looking for libffi, from Wikipedia:

libffi is a foreign function interface library. It provides a C programming language interface for calling natively compiled functions given information about the target function at run time instead of compile time. It also implements the opposite functionality: libffi can produce a pointer to a function that can accept and decode any combination of arguments defined at run time.

Upvotes: 2

Roberto
Roberto

Reputation: 3083

You could use a system call (using the system function of the standard library) to invoke a compiler which creates a dynamic library from your function file. You can then load the library with dlopen or LoadLibrary, depending on the platform.

EDIT: Of course this will not work if you really have only a prototype, and not the definition. In that case I am sorry I misunderstood your question.

Upvotes: 0

user149341
user149341

Reputation:

No. A function prototype is just a statement of how a function is used; it cannot be executed on its own.

If you have the name of a function and you want to turn that into a function pointer, you may be able to do so using dlsym(RTLD_DEFAULT, "fn"). Not recommended, though.

Upvotes: 0

Related Questions