Jordan
Jordan

Reputation: 9114

Calling static C functions externally

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++

Upvotes: 1

Views: 151

Answers (1)

ouah
ouah

Reputation: 145899

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.

Upvotes: 4

Related Questions