Reputation: 2132
Is there a way in C programming language to check if function prototypes from a header files corresponds to actual function definition in compile time. For example, if I made header file, and then change signature of some function described in that header file, can I check in compile time if there is a wrong prototype in header file? Is that a job of the compiler or some other tool before compilation? Thanks.
Upvotes: 2
Views: 2520
Reputation: 3134
If you use the function, the compiler will give you a linker error, if an implementation for a prototype does not exists. But if you never use the function (for example when you build a library) the linker will not complain.
This is one of the reasons you should make sure you have good code coverage in your tests - if you have for example some unit tests which also gets compiled, the linker will complain. If you have some functions you can not test and will not get called from within your code, you can just write a dummy executable (which does not have to work) which will call all this functions.
The last solution would be to use the clang libraries to write your own code checkers.
Upvotes: 0
Reputation: 272772
If you declare the same function name with two different prototypes, the compiler should catch this, i.e.:
int foo(int a, int b);
...
int foo(int a, float b) { ... }
Of course, if you actually rename the function, then the compiler cannot catch it, i.e.:
int foo(int a, int b);
...
int fee(int a, int b) { ... }
Unless, of course, you attempt to call foo
from elsewhere. Then the linker will complain.
Upvotes: 2
Reputation: 206636
That is the job of the compiler and in my experience it does it quite well:)
If your function prototype in header file does not match it's definition in the source file, then you cannot use that function in other source file because it is not declared and the compiler will inform you so, by giving an error.
Upvotes: 1