Declared static but never defined

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

I am using Atmel Studio and facing a compiling error.

 

I got 3 files : main.c / myLib.h / myLib.c

 

In myLib.h I declare a function like this:

 

static float constrain(float x, float a, float b);

 

In myLib.c I implement this function:

static float constrain(float x, float a, float b){
	if(x < a){
		return a;
	}
	else if(x > b){
		return b;
	}
	else{
		return x;
	}
}

I don't use this function in main.c, only in myLib.c. That is why I declared it static.

 

When compiling, I got this error: 'constrain' declared 'static' but never defined [-Wunused-function]

 

myLib.h is include in myLib.c and main.c because I use others function from it in main.c.

 

What did I do wrong?

 

Thanks

Last Edited: Sun. Mar 22, 2015 - 04:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Need to see more context, perhaps just zip all 3 files? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I upload the Atmel Studio solution. Here is the zip:

 

http://www.damien-monni.fr/files/static-issue.zip

 

Thanks for your help.

Last Edited: Sun. Mar 22, 2015 - 06:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's fairly obvious really. Here's the clue:

 

myLib.h is include in myLib.c and main.c because I use others function from it in main.c.

 

As the OP said: myLib.h contains a static function declaration for constrain, so when he compiles main.c the compiler sees the declaration of constrain() but of couse sees no definition.

 

The solution is to remove the declaration from myLib.h. (It shouldn't be there anyway because it's private to myLib.c.)

 

-- Nigel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed it works if I remove the declaration from the header. But I read everywhere that static function declaration should be done in header and source file...

So I don't really understand why sometimes I should declare it in header and sometimes not...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobby4078 wrote:
I read everywhere  that static  function declaration should be done in header and source file

No, I think you are misreading!

 

http://c-faq.com/decl/decldef.html

 

Post some examples.

 

Do you understand what the static keyword means in 'C' when applied to a function?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can read it here, for instance : https://www.avrfreaks.net/forum/t...

 

Note that the static keyword should be added to both the prototype in the header file, as well as to the function in the C source file.

 

To me, the static keyword applied to a function in C means that the function is "private" and cannot be used in another file.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobby4078 wrote:
To me, the static keyword applied to a function in C means that the function is "private" and cannot be used in another file.

Not just to you - that is the 'C' language meaning of static as applied to functions!

 

So, if the function is private - why would you ever put it in a header at all??

 

Quote:

I can read it here, for instance : https://www.avrfreaks.net/forum/tut-modularizing-c-code-managing-large-pr...

 

Note that the static keyword should be added to both the prototype in the header file, as well as to the function in the C source file.

That is just plain wrong - it is bound to lead to the very error you describe!

 

The exception to this would be if the file is inline - in which case the definition (not just a declaration aka "prototype") would need to be in the header, and not in the .c file.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that the error is mentioned in the discussion on the tutorial:

 

https://www.avrfreaks.net/comment...

https://www.avrfreaks.net/comment...

https://www.avrfreaks.net/comment...

 

And note the big red warning on the tutorial itself:

 

abcminiuser wrote:
For an updated version of this tutorial in PDF format, please see this page of my website.

And that version says,

Quote:
Note that the static keyword should be added to both the prototype, as well as to the function
in the C source le. However, there's now an additional important step: we need to move the
(now) static function prototype out of our header le and back into the C source le. This seems
counter-intuitive at rst; after all, header les are supposed to contain function prototypes. The
reason is due to the limited scope of static functions; since they can only be called from the C
source le where they are de ned, there's no need to have a global prototype in the header les
that other C source les could potentially include. In fact, if you do leave the static function
prototype in the header le, you will receive compiler warnings about unde ned functions if the
static function prototype is visible to other C source les.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For some reason, the text copied from Dean's PDF article seems to have lost all occurrences of "fi" at the start of a word!!

 

Try again:

Quote:
Note that the static keyword should be added to both the prototype, as well as to the function
in the C source file. However, there's now an additional important step: we need to move the
(now) static function prototype out of our header file and back into the C source file. This seems
counter-intuitive at first; after all, header files are supposed to contain function prototypes. The
reason is due to the limited scope of static functions; since they can only be called from the C
source file where they are defined, there's no need to have a global prototype in the header files
that other C source files could potentially include. In fact, if you do leave the static function
prototype in the header file, you will receive compiler warnings about undefined functions if the
static function prototype is visible to other C source files.

 

(I pasted it into Notepad, and Dean's PDF seems to have used some special code for the "fi" sequence)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Mar 23, 2015 - 09:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wrote:
Dean's PDF seems to have used some special code for the "fi" sequence

Google & Wikipedia to the rescue - it's a Typographic ligature:

 

http://en.wikipedia.org/wiki/Typ...

 

 

There - you learn something new every day!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...