Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can GCC ignore static declaration of a function?

In my application I have a build config for pseudo unit testing (this is more a kind of manual debugging a dedicated function).

In those unit tests I'd like to access functions declared as static in their translation unit.

Is there an option of GCC that would allow me to call static functions from anywhere?

I'd like to avoid:

#if UNIT_TEST_MODE
void myfunction(void)
#else
static void myfunction(void)
#end
{
    // body
}

Everywhere!

Thanks for your help :).

like image 530
Plouff Avatar asked May 27 '16 12:05

Plouff


People also ask

What is static and non static declaration in C?

Static is a keyword used in C programming language. It can be used with both variables and functions, i.e., we can declare a static variable and static function as well. An ordinary variable is limited to the scope in which it is defined, while the scope of the static variable is throughout the program.

Is main function static in C++?

Thus main can NEVER be static, because it would not be able to serve as the primary entry point for the program.

What does static function mean in C?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.


1 Answers

There is not need to be verbose. Use a prefix define for every static function:

#if UNIT_TEST_MODE
#define UNIT_TEST_STATIC
#else
#define UNIT_TEST_STATIC static
#end

UNIT_TEST_STATIC void myfunction(void)
{
    // body
}

Another option is to move all static function from that .c file to a separate header. That header is included only in that .c file, but it can be included in the unit test .c file if needed. The functions will remain invisible in other files, unless the header is manually included.
(They will have to be defined as static inline. )

like image 186
2501 Avatar answered Sep 30 '22 03:09

2501