1

This might seem like an odd question, but I was wondering if it were possible with any sort of hack to call static functions from another file without explicitly using extern or anything like that. Perhaps by calling the memory address of the function directly or something.

Basically what I want to do is create a test framework which can call into any function by specifying the function, file and function parameters.

So something like this structure:

component/
    component.c
         static int foo(int a){return a/2;}
         int bar(){ return 4;}

unit_tests/
    main.c
        int val = component.c::foo(4) * bar();

Even better would be if I could do this at runtime by injecting into the memory address of the function or something. I'm not entirely sure about if this would be executable on linux though, or if I'd hit security issues.

Perhaps something similar to this, and have a block of code in my component process to interpret runtime calls and translate to the correct function address: Calling a function through its address in memory in c / c++

Community
  • 1
  • 1
Jordan
  • 9,014
  • 8
  • 37
  • 47
  • No - you can not infer the c++ syntax, unless you know how to parse it (or having tools doing that) –  May 12 '15 at 20:49
  • you should remember that the 'static' modifier has the purpose of making a function 'invisible' to any other file. Why would you want to override that functionality? – user3629249 May 12 '15 at 21:07
  • Specifically for test frameworks without needing to expose each function as an interface. – Jordan May 14 '15 at 17:39

1 Answers1

4

You can use function pointers to static functions.

For test frameworks, note that some existing test frameworks in C use the trick to force you to use STATIC instead of the static specifier and STATIC is a macro defined (by the framework) to either nothing or static if you are in test mode or not to specify the correct linkage.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Yeah, I was almost hoping it do it directly in memory at runtime or something actually. Such as having a scriptable language directly call into the function at the given memory address. I'd also prefer not to have to use a macro for static if at all possible, since I'm not sure if that would be acceptable based on our coding standards. Thanks – Jordan May 12 '15 at 20:44