kfifo icon indicating copy to clipboard operation
kfifo copied to clipboard

如何集成到C++项目?

Open chwyu opened this issue 1 year ago • 2 comments

C++编译有很多报错

chwyu avatar Jun 28 '24 05:06 chwyu

我试了一下用g++编译的确会有一堆编译错误,具体错误如下:

g++    -c -o main.o main.cc
In file included from main.cc:4:
../kfifo.h:64:18: error: ‘kfifo::<unnamed union>::kfifo’ has the same name as the class in which it is declared
   64 |   struct __kfifo kfifo; \
      |                  ^~~~~
../kfifo.h:83:2: note: in expansion of macro ‘__STRUCT_KFIFO_COMMON’
   83 |  __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
      |  ^~~~~~~~~~~~~~~~~~~~~
../kfifo.h:93:14: note: in expansion of macro ‘__STRUCT_KFIFO_PTR’
   93 | struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
      |              ^~~~~~~~~~~~~~~~~~
main.cc: In function ‘int main()’:
../kfifo.h:82:1: error: types may not be defined in ‘sizeof’ expressions
   82 | { \
      | ^
../kfifo.h:88:9: note: in expansion of macro ‘__STRUCT_KFIFO_PTR’
   88 |  struct __STRUCT_KFIFO_PTR(type, 0, type)
      |         ^~~~~~~~~~~~~~~~~~
../kfifo.h:113:27: note: in expansion of macro ‘STRUCT_KFIFO_PTR’
  113 |  (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
      |                           ^~~~~~~~~~~~~~~~
../kfifo.h:140:18: note: in expansion of macro ‘__is_kfifo_ptr’
  140 |  __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
      |                  ^~~~~~~~~~~~~~
main.cc:37:5: note: in expansion of macro ‘INIT_KFIFO’
   37 |     INIT_KFIFO(g_fifobuf);
      |     ^~~~~~~~~~
../kfifo.h:63:8: error: types may not be defined in ‘sizeof’ expressions
   63 |  union { \
      |        ^
../kfifo.h:83:2: note: in expansion of macro ‘__STRUCT_KFIFO_COMMON’
   83 |  __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
      |  ^~~~~~~~~~~~~~~~~~~~~
../kfifo.h:88:9: note: in expansion of macro ‘__STRUCT_KFIFO_PTR’
   88 |  struct __STRUCT_KFIFO_PTR(type, 0, type)
      |         ^~~~~~~~~~~~~~~~~~
../kfifo.h:113:27: note: in expansion of macro ‘STRUCT_KFIFO_PTR’
  113 |  (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
      |                           ^~~~~~~~~~~~~~~~
../kfifo.h:140:18: note: in expansion of macro ‘__is_kfifo_ptr’
  140 |  __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
      |                  ^~~~~~~~~~~~~~
main.cc:37:5: note: in expansion of macro ‘INIT_KFIFO’
   37 |     INIT_KFIFO(g_fifobuf);
      |     ^~~~~~~~~~
../kfifo.h:82:1: error: types may not be defined in ‘sizeof’ expressions
   82 | { \
      | ^
../kfifo.h:88:9: note: in expansion of macro ‘__STRUCT_KFIFO_PTR’
   88 |  struct __STRUCT_KFIFO_PTR(type, 0, type)
      |         ^~~~~~~~~~~~~~~~~~
../kfifo.h:113:27: note: in expansion of macro ‘STRUCT_KFIFO_PTR’
  113 |  (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
      |                           ^~~~~~~~~~~~~~~~
../kfifo.h:142:18: note: in expansion of macro ‘__is_kfifo_ptr’
  142 |  __kfifo->data = __is_kfifo_ptr(__tmp) ?  NULL : __tmp->buf; \
      |                  ^~~~~~~~~~~~~~
main.cc:37:5: note: in expansion of macro ‘INIT_KFIFO’
   37 |     INIT_KFIFO(g_fifobuf);
      |     ^~~~~~~~~~
../kfifo.h:63:8: error: types may not be defined in ‘sizeof’ expressions
   63 |  union { \
      |        ^
../kfifo.h:83:2: note: in expansion of macro ‘__STRUCT_KFIFO_COMMON’
   83 |  __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
      |  ^~~~~~~~~~~~~~~~~~~~~
../kfifo.h:88:9: note: in expansion of macro ‘__STRUCT_KFIFO_PTR’
   88 |  struct __STRUCT_KFIFO_PTR(type, 0, type)
      |         ^~~~~~~~~~~~~~~~~~
../kfifo.h:113:27: note: in expansion of macro ‘STRUCT_KFIFO_PTR’
  113 |  (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
      |                           ^~~~~~~~~~~~~~~~
../kfifo.h:142:18: note: in expansion of macro ‘__is_kfifo_ptr’
  142 |  __kfifo->data = __is_kfifo_ptr(__tmp) ?  NULL : __tmp->buf; \
      |                  ^~~~~~~~~~~~~~
main.cc:37:5: note: in expansion of macro ‘INIT_KFIFO’
   37 |     INIT_KFIFO(g_fifobuf);
      |     ^~~~~~~~~~
make: *** [<builtin>: main.o] Error 1

liigo avatar Jul 01 '24 05:07 liigo

https://unix.stackexchange.com/questions/449158/error-types-may-not-be-defined-in-sizeof-expressions 貌似Linux内核里的C代码并不考虑与C++的兼容性。所以如何集成到C++项目的确是一个难办的问题。

liigo avatar Jul 01 '24 05:07 liigo

https://github.com/ubrabbit/kfifo 我删减了一些内容,可以在c++11 && gcc8.5/clang16 环境编译通过。

ubrabbit avatar Apr 28 '25 09:04 ubrabbit