libcoap
libcoap copied to clipboard
Headers contain non-prefixed/namespaced macros and declarations
coap/debug.h
defines the macros info
, warn
, debug
. This is bad practice and worse is that these are some pretty common words which may already be in use in other applications and libraries. Since they are preprocessor macros and not regular functions, they don't care about scope and will mess up headers from other libraries.
There are some more non-prefixed macros in include/coap
:
-
debug.h
-info
,warn
,debug
macros -
prng.h
-prng
,prng_init
macros -
options.h
-options_next
,PCHAR
macros,options_start
function -
encode.h
-Nn
,HIBIT
,EMASK
,MMASK
,MAX_VALUE
,ENCODE_HEADER_SIZE
macros -
resource.h
-RESOURCES_xxx
macros
coap.h
includes all of these files and therefore it's not easy to avoid them.
The specific collision that I received was when mixing Eigen 3.2 with libcoap. Eigen is a header-only library and declares some classes with methods named info()
, which are messed up by the preprocessor because of the non-prefixed macros in debug.h
.
A work around is to reorder the include lines in the application code, but it is difficult to realize for someone who is not familiar with the specific libraries involved or less experienced in C.
The best solution in my opinion would be to prefix all public macros with COAP or LIBCOAP and change them to uppercase, and rename options_start to coap_options_start.
... not to forget uthash
that is shipped with the include files.
The macros debug()
, info()
, and warn()
are present only for backwards compatibility and have been replaced by the preferred form coap_log()
with their appropriate log-level attributes (which in turn have been named after the syslog attributes on purpose).
The clean way would be to remove debug()
, info()
and warn()
once and for all, causing a significant API change. (To some degree, this is true for the other symbols mentioned but they are more for private use and thus should not occur in application code too often.)