C preprocessing defines are hard to guarantee its ‘portability’? I think its my skills issue.
In vector.h
, I use a token VECTOR_H_i_TYPE
, which is NOT explicitly defined in vector.h
. It is to represent any type that is going be decided by the client.
In other words, the intention is to have the client first define the macro, then include the header file; e.g.
#define VECTOR_H_i_TYPE uint16_t
#include "vector.h"
This is so that token pasting will allow it to be defined by the client and after the #include
the type will be defined.
In vector.h
, I also have the following macros.
#define PRINT_ANY(type) PRINT_FUNCTION_TYPE(type)
#define PRINT_FUNCTION_TYPE(type) print_ ##type
The intention is to let client define how to print VECTOR_H_i_TYPE
by defining print_<VECTOR_H_i_TYPE>
like so:
In client.c
:
void print_uint16_t(uint16_t elem) { ... };
In vector.h
, PRINT_ANY
is used like so:
void vector_print(Vector* self) {
... // for loop on each elem in self
PRINT_ANY(VECTOR_H_i_TYPE)(elem); // this should expand to `print_uint16_t(elem);`
};
On my local machine it works as intended!
On the CI with github actions it doesn’t! it says print_uint16_t
is defined twice!
Here is the full error trace:
/home/runner/work/yet-another-c-template/yet-another-c-template/test/source/test_vector.c:10:6: error: conflicting types for 'print_uint16_t'
void print_uint16_t(uint16_t elem)
^
/home/runner/work/yet-another-c-template/yet-another-c-template/include/vector/vector.h:154:5: note: previous implicit declaration is here
PRINT_ANY(VECTOR_H_i_TYPE)
^
/home/runner/work/yet-another-c-template/yet-another-c-template/include/vector/vector.h:70:27: note: expanded from macro 'PRINT_ANY'
# define PRINT_ANY(TYPE) PRINT_FUNCTION_NAME(TYPE)
^
/home/runner/work/yet-another-c-template/yet-another-c-template/include/vector/vector.h:78:37: note: expanded from macro 'PRINT_FUNCTION_NAME'
# define PRINT_FUNCTION_NAME(TYPE) print_##TYPE
^
<scratch space>:33:1: note: expanded from here
print_uint16_t
The trace seems to suggest that the expansion of PRINT_FUNCTION_NAME(TYPE
macro conflicts with the definition of void print_uint16_t(uint_16 *elem)
in client.c
.
The actual repository where these codes reside are in here: https://github.com/jymchng/yet-another-c-template
9
it says
print_uint16_t
is defined twice!
No, it doesn’t. At least, the diagnostics presented in the question do not say that. They say that there are conflicting declarations, one of them an implicit one. That there is a call relying on an implicit declaration of the function at all is a problem: that hasn’t been well-defined since C99, though many compilers continue to allow it for backwards compatibility. You can find all sorts of relevant information here on SO and elsewhere by searching for “implicit function declaration”.
If you are using any kind of decent compiler locally, then you can probably get it to emit analogous errors. With GCC, for example, you should (always) compile with -Wall
, and you can convert any resulting warnings into errors by adding -Werror
, too. That should get you the same error you present in the question.
It looks like the implicit declaration is coming from the call in the body of function void vector_print
, defined in the header. As I already remarked in comments, external functions should not be defined in headers. That you do so is a contributing factor to the error, but the essential issue is that there is no declaration of print_uint16_t
in scope at the point of that call. If you want to persist with this ill-considered approach, then you need to declare the function prior to the first call to it (preferably with a prototype).