1

I am writing a library which gets preloaded using LD_PRELOAD to different versions of other code (which I don't want/cannot change). In a former version, a class method A::foo() of class A exists and in a later version, it doesn't.

For global (C-like) function, I am using the attribute((weak)) mechanism to check whether a function is actually defined when running the code:

void foo() __attribute__(weak));
if (foo) { foo(); }

That doesn't seem to work for class members.

The compile-time error is:

no ‘int somenamespace::SomeClass::someFunction()’ member function declared in class ‘somenamespace::SomeClass’
int somenamespace::SomeClass::someFunction() __attribute__((weak));
                                                                                     ^
...
./file.cpp: In member function ‘virtual void somenamespace::OtherClass::initialize()’:
./file.cpp:397:32: error: ‘class somenamespace::SomeClass’ has no member named ‘someFunction’
         num = theinstance.someFunction();

Is there a way to check for existance which does not rely on the definition being known at compile time?

I cannot use templates as in Is it possible to write a template to check for a function's existence? because I templates are evaluated during compilation.

Also, How to Check if the function exists in C/C++ was of no help.

Community
  • 1
  • 1
steffen
  • 8,572
  • 11
  • 52
  • 90
  • 2
    All info about a function's existence is already known at compile-time; thus a compile-time solution is the best way to go. Keep in mind that C++ is not a dynamic language. – tenfour Jun 11 '14 at 10:32
  • I agree, how is it that you do not know if it exists? Unless you are loading a DLL at runtime and querying for functionality? – Dennis Jun 11 '14 at 10:49
  • @tenfour: As I wrote in the first sentence, I am writing a library which gets preloaded using LD_PRELOAD. That means, that at the time I am compiling the library, I do not know, to which other (version of) code it gets preloaded. – steffen Jun 11 '14 at 11:31

0 Answers0