Hello all,
I am using CW6.3 for HCS08 devices.
My project utilises the following two files (in addition to some other files):
// File: SCI1.c#include "SCI.h"byte Rx_get_fifo( void); // Fetch byte from receive buffer
// File: SCI2.c#include "SCI.h"byte Rx_get_fifo( void); // Fetch byte from receive buffer
The function Rx_get_fifo() is utilised from within each respective file only. There is no declaration for Rx_get_fifo() within the header file.
My incorrect assumption was that the scope of the function would be limited to its own file. However, the linker was unable to resolve, and indicated a naming conflict.
Solution 1: Obviously, to use different function names.
Solution 2: To declare each function as "static". This seems unorthodox usage of the static keyword. Most C textbooks refer this as a modifier for variables, but do not mention anything with respect to functions.
Perhaps someone will be able to explain why the function name did actually extend beyond file scope, without any extern declarations. Additionally, are there any other side effects with using a "static" function?
Regards,
Mac
> Most C textbooks refer this as a modifier for variables, but do not mention anything with respect to functions.
Sadly, most C textbooks are utter crap. Including K&R which only mentions static functions very briefly.
Using the static keyword for private functions is considered very good programming practice. Static functions are used in C to achieve the OO program design concept called "private encapsulation!", which is a 100% good thing. There are no "side-effects" of static functions, they are perfectly safe. (As opposed to the static keyword for variables, which makes functions non-reentrant.)
The widely-recognized industry standard MISRA-C does not only encourage the use of static but also enforces it (MISRA-C:2004 rules 8.10 and 8.11).
I would also recommend reading embedded guru Nigel Jones' blog:
http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/
Hello Kef and Rocco,
Thank you for your response. Presumably it is the function name only that defaults to global scope. Is it then true that the extern function prototype declaration is required only to identify the parameter list and return type of the particular function name?
I had previously assumed that the declaration was also required to "globalise" the function name, as well.
Regards,
Mac