Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function declared static but never defined

Tags:

c

gcc-warning

I have a header file suppose abc.h, where i have function declaration as:

static int function1();

I have included this header file in abc.c and has defined the function and used it.

static int function1()
{
 < function definition>
}

After compiling I am getting warning:

warning: function1 declared static but never defined

How can I remove warning, without removing static. Thanks.

like image 523
pankanaj Avatar asked Mar 28 '13 12:03

pankanaj


People also ask

Is declared but never defined?

A "declared but not defined" error usually means you've declared a static function in a file but never actually defined the function. It usually isn't a linker error. The linker error would normally be 'undefined reference'.

Can objects of class type that is declared but not defined be created?

An incomplete class declaration is a class declaration that does not define any class members. You cannot declare any objects of the class type or refer to the members of a class until the declaration is complete.

What is a static function CPP?

A static function is a member function of a class that can be called even when an object of the class is not initialized. A static function cannot access any variable of its class except for static variables. The 'this' pointer points to the object that invokes the function.


2 Answers

A static function can be declared in a header file, but this would cause each source file that included the header file to have its own private copy of the function, which is probably not what was intended.

Are u sure u haven't included the abc.h file in any other .c files?

Because declaring a function as static, requires the function to be defined in all .c file(s) in which it is included.

like image 164
hazzelnuttie Avatar answered Oct 16 '22 09:10

hazzelnuttie


Good practice: Declare static functions in the source file they are defined in (please also provide prototype), since that's the only file they are visible in.

This way, the function is only visible to that file, such visibility issues can reduce possible code conflict! So, just provide the prototype and the static function definition in the .c file. Do not include the static function in the header file; the .h file is for external consumption.

Duplicate: Static functions in C

like image 18
Ehsan Avatar answered Oct 16 '22 09:10

Ehsan