12

Other than boost (Bind & Function), how can I dynamically call a function in C++?

PHP has:

$obj = new MyObject();
$function = 'doSomething';
$obj->$function();

Objective-C has:

MyObject *obj = [[MyObject alloc] init];
SEL function = NSSelectorFromString(@"doSomething");
[obj performSelector: function];
joels
  • 7,249
  • 11
  • 53
  • 94
  • http://stackoverflow.com/questions/11016078/is-it-possible-to-create-a-function-dynamically-during-runtime-in-c – dtech Aug 25 '13 at 16:12
  • Related search keywords: In C++ look for "virtual functions" and "late binding or dynamic binding". In C "pointer to functions". – lepe Jun 19 '15 at 02:57

5 Answers5

10

You can export necessary functions (e.g by marking them with __dllexport) and use GetProcAddress or dlsym (depending on your platform) for getting their address:


void *handle = dlsym(0, RTLD_LOCAL | RTLD_LAZY);
FunctionType *fptr = (FunctionType *)dlsym(handle, "doSomething");
fptr();


HANDLE handle = GetCurrentProcess();
FunctionType *fptr = (FunctionType *)GetProcAddress(handle, "doSomething");
fptr();

All of this is platform-specific though and there is no standard way in C++ for doing this.

Konstantin Oznobihin
  • 5,234
  • 24
  • 31
  • 3
    Can't complain, that is a good answer. However I'll point out it's great for dynamic calls to C functions, and not going to work for polymorphic C++ method calls. – Graham Perks Jan 19 '12 at 14:51
  • @GrahamPerks: well, it will just be even more platform-specific with such things like mangling, adjusting object pointers and like, but not entirely impossible. Although, I doubt I will ever do such thing in production environment. – Konstantin Oznobihin Jan 19 '12 at 15:36
  • 1
    You can also mark the functions you want to reference with extern "C" in the declaration to prevent the names from being mangling during compilation. extern "C" void hello() { std::cout << "Hello\n"; } This will allow you to call your function by the name. void *handle = dlsym(0, RTLD_LOCAL | RTLD_LAZY); FunctionType *fptr = (FunctionType *)dlsym(handle, "hello"); fptr(); Here is what extern "C" does under the hood: http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c – deadbabykitten Apr 25 '15 at 21:06
4

Expanding on Konstantin Oznobihin's answer to the question, you can mark the c++ functions you are referencing with extern "C" in the declaration to prevent the compiler from mangling the names during compilation.

extern "C" void hello() {
    std::cout << "Hello\n";
}

This will allow you to call your object/function by the name you initially gave it. In this case it is 'hello'.

void *handle = dlsym(0, RTLD_LOCAL | RTLD_LAZY);
FunctionType *fptr = (FunctionType *)dlsym(handle, "hello");
fptr();

There are a bunch of things extern "C" does under the hood, so here's a short list: In C++ source, what is the effect of extern "C"?

Community
  • 1
  • 1
deadbabykitten
  • 159
  • 3
  • 14
  • Please edit and add this to @KonstantinOznobihin 's answer. Anyway, thanks for good tip. – Jet Apr 24 '15 at 15:25
4

If I understood your question properly, you can make use of function pointer (or pointer to member) in C++. You can dynamically decide which function call (you may need a prototype of the same) and call it dynamically. See this link

https://isocpp.org/wiki/faq/pointers-to-members

lepe
  • 24,677
  • 9
  • 99
  • 108
sarat
  • 10,512
  • 7
  • 43
  • 74
2

If those functions you are interested in are of same type, you could create map of strings and function pointers.

stralep
  • 858
  • 8
  • 16
2

The simple answer is, you can't. C++ doesn't do method look up by name.

Graham Perks
  • 23,007
  • 8
  • 61
  • 83
  • 1
    Wrong, you can lookup DLL functions by name. – dtech Aug 25 '13 at 15:56
  • 4
    @ddriver DLLs aren't part of C++, and DLL functions aren't C++ methods. – Roddy Aug 25 '13 at 16:23
  • @Roddy - you can compile C++ to DLL and get a pointer to the function by a name lookup. DLL on windows, so on linux, whatever, you CAN lookup functions in dynamic libraries by name, that is a fact. – dtech Aug 25 '13 at 16:30
  • My "simple answer" still stands. There are more nuanced alternatives. Your DLL solution is sound except that every function you want to call dynamically has to be part of a DLL and also be exported, ideally in some non-mangled form. It gets harder when the function is a class member. Hardly a simple, ideal, or general purpose solution like the original poster's PHP and Obj-C examples. – Graham Perks Aug 26 '13 at 18:04