j123b567 / scpi-parser

Open Source SCPI device library
BSD 2-Clause "Simplified" License
447 stars 191 forks source link

C++ compilation error due to NULL being a long int #117

Closed ilanbiala closed 1 year ago

ilanbiala commented 3 years ago

When I try to compile this library in a C++ project, I get this error:

../libscpi/src/error.c: In function 'scpi_bool_t SCPI_ErrorAddInternal(scpi_t*, int16_t, char*, size_t)':
../libscpi/src/error.c:136:28: error: invalid conversion from 'long int' to 'char*' [-fpermissive]
  136 |     char * info_ptr = info ? SCPIDEFINE_strndup(&context->error_info_heap, info, info_len) : NULL;
      |                            ^
      |                            |
      |                            long int

I checked which macros are set, and SCPIDEFINE_strndup() is defined from https://github.com/j123b567/scpi-parser/blob/a765b9eadd3fe29de5f8d426549b69fc0a96793c/libscpi/inc/scpi/config.h#L233 for me. This causes a problem when compiling in C++ since NULL is interpreted as a long int, which cannot be implicitly converted to a char*.

j123b567 commented 3 years ago

Are you compiling C source files by C compiler? C and C++ are not compatible and C++ compiler can't compile C code. There are constructions which are allowed in C but prohibited in C++.

Problem is not the NULL itself, but that char * x = NULL is allowed in C++, but char * x = a ? NULL : NULL is not. This was allowed by the C++ standard but changed later and GCC6+ force this.

Feel free to provide PR which solve this.