Reputation: 510
Say we have a library that defines the following internally used function skip_comments
, that is used only by the translation unit it is defined in
void skip_comments(...)
{
}
void another_function()
{
skip_comments(...) //calls the above function --- (1)
}
Now this library is used by another process which also has a function with the same name skip_comments
.
This will cause a function name conflict between the library function vs the function defined in the process. So at the point (1)
I might expect it to call the process's function instead of the library function which clearly is a bug.
As a possible fix, I made the skip_comment
function static
and now the bug seems to be fixed. But I'm not sure if this is a proper fix
My question is, would defining the skip_comment
as static
ensure that at (1)
such a name conflict will not occur? ie.
static void skip_comments(...)
{
}
void another_function()
{
skip_comments(...) // will it always call the internal function?
}
Or the only solution is to make sure we define a unique name for functions such as adding prefixes?
Upvotes: 0
Views: 602
Reputation: 214495
Declaring a function as static
means that it will only be accessible by the translation unit where it is declared. (Translation unit meaning the .c file and all .h files included by that .c file.)
So yes, declaring it as static
will solve your name conflicts. Given that you do this consistently everywhere and not just in one file.
However, it is good practice to prefix all identifiers based on the module where they belong, so that is perhaps the best solution regardless.
Upvotes: 2