Detect undefined characters in C header file

Suposse I coded the C library, which provides a bunch of "public" functions declared in the mylib.h header file. These functions are supposedly implemented in the (say) mylib.c file, which is compiled into (say) the static lib mylib.c -> mylib.o -> mylib.a .

Is there any way to discover that I forgot to provide an implementation of some declared function in mylib.h ? (Yes, I know about unit testing, best practices, etc. - and, yes, I understand the meaning of declaring a simple function in C).

Suppose mylib.h declares a void func1(); , and this function was not encoded in the provided library. This will only cause an error if the linker should use this function. Otherwise, it will compile normally and even without warning - AFAIK. Is there a way (possibly dependent on a compiler) to trigger a warning for declared but not implemented functions, or is there any other way to deal with this problem?

BTW: nm -u does not list all undefined declared functions, but only those "used" by the library, i.e. these functions which will cause an error in the binding phase if it is not announced somewhere. (Which makes sense, the library object file does not know about header files, of course.)

+4
source share
4 answers

In principle, the most reliable way is to have a program (or perhaps a series of programs) that formally perform each of the functions. If one of the programs could not connect due to a missing character, you are faced.

I assume that you could try to do something by editing a copy of the header into the source file (like at the end of the .c file), converting function declarations to definitions of dummy functions:

Original:

 extern int somefunc(void); 

Corrected version:

 extern int somefunc(void){} 

Then compile the modified source with minimal warnings and ignore anything that is related to a function that should return a value, not ". Then compare the specific characters in the object file from the revised source with the specific characters in the library (using nm -g on Unix- similar systems.) Everything that is present in the object file that is not in the library is absent and should be provided.

Note. If your header contains other custom headers that define functions, you need to handle all of them. If your header contains standard headers such as <stdio.h> , then obviously you will not define functions like fopen() or printf() in the normal course of events. So, carefully select the headers that you process into source code.

+2
source

There is no easy way.

For example, you can analyze the output of clang -Xclang -ast-print-xml or gcc-xml and filter ads without implementations for a given .h file.

+2
source

You can grep to sign the exported function in both .h and .c and compare the lists. Use wc -l to count matches. Both numbers must be equal.

Another thought just occurred to me. This cannot be handled by the compiler. this is not always the case, this function is declared in mylib.h, implemented in mylib.c

0
source

Is there any way to discover that I forgot to provide an implementation of some declared function in mylib.h?

Write an implementation first, and then think about the contents of the header - because that way it can be tagged.

0
source

Source: https://habr.com/ru/post/1332781/


All Articles