Here we document style rules for C usage in the gRPC Core library.
Public header files (those in the include/grpc tree) should compile as pedantic C89.
Public header files should be includable from C++ programs. That is, they should include the following:
#ifdef __cplusplus extern "C" { # endif /* ... body of file ... */ #ifdef __cplusplus } # endif
Header files should be self-contained and end in .h.
All header files should have a #define guard to prevent multiple inclusion. To guarantee uniqueness they should be based on the file's path.
For public headers: include/grpc/grpc.h
→ GRPC_GRPC_H
For private headers: src/core/channel/channel_stack.h
→ GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_STACK_H
When declaring a (non-static) pointer variable, always initialize it to NULL
. Even in the case of static pointer variables, it's recommended to explicitly initialize them to NULL
.
Within public header files, only /* */
comments are allowed.
Within implementation files and private headers, either single line //
or multi line /* */
comments are allowed. Only one comment style per file is allowed however (i.e. if single line comments are used anywhere within a file, ALL comments within that file must be single line comments).
grpc_
grpc_
#define
names must be uppercase. All other values must be lowercase.variable_name
.atexit()
is in forbidden in libgrpc.