Enum declaration not found when including containing header - c

I have an enumerator type that is declared in a header file. I would like to use this in a c file that includes this header file. However when compiling I get an error that the enumerator type is not defined. I've tried copying the enumerator declaration to my source file but I get an error: "nested redefinition of ‘enum command_type’"
Could someone explain how to use the enumerator type in my file please?
Thank you!
The enumerator:
//command-internals.h
enum command_type
{
AND_COMMAND, // A && B
SEQUENCE_COMMAND, // A ; B
OR_COMMAND, // A || B
PIPE_COMMAND, // A | B
SIMPLE_COMMAND, // a simple command
SUBSHELL_COMMAND, // ( A )
};
The usage:
//#include "command-internals.h"
command_type scan(char *buffer)
...
The error: error: unknown type name ‘command_type’

Your prototype should read:
enum command_type scan(char *buffer);
Since you didn't put a typedef on the enum declaration.

With your code compiler cannot understand what is the the type of command_type.
One will typically use a typedef:
//Header file
typedef enum {....} command_type;
//C File
command_type scan(char *buffer)
With this you don't have to remember to keep adding an enum everywhere again.

Related

Including enums defined in header

I have been fiddling with enums for a while and wanted to try to use them in a project. The project structure is as follows:
// protocol.h
#ifndef PROTOCOL_H
#define PROTOCOL_H
enum C_C {P_NORTH = 0,
P_WEST = 1,
P_SOUTH = 2,
P_EAST = 3};
#endif
// other.h
#include "protocol.h"
struct cmd {
enum C_C code : 4;
};
void make_cmd(struct cmd*, enum C_C);
This file triggers the following errors:
field 'code' has incomplete type
'enum C_C' declared inside parameter list will not be visible outside of this definition or declaration
// other.c
#include "other.h"
void make_cmd(struct cmd* cmd, enum C_C code) {
cmd->code = code;
}
This throws the following errors:
conflicting types for 'make_cmd'
I have tried changing the enum to a type using typedef with no luck. This happens also with function definitions which rely on this type of parameters.
Will throw the following error:
type of formal parameter 2 is incomplete
Thanks for your help.
This only happens when using the defined enum in another header, either for structs or for functions prototypes.
I do believe there must be some issue with the compilation order. I have tested in Xilinx SDK and Vitis with the same result.
protocol.h holds all the definitions of the enums and the structures to be used throughout the project. I was hoping by just including this one in the other headers the definitions would be available to build the other.h and other.c on top of that one.
Update:
I have moved the definition of the structure inside the protocol.h and it lets me add a member using the enum without issues. I guess the problem is when importing protocol.h into another header and trying to use the enum there the compiler has all of the headers in the
This code compiles:
#include "other.h"
void make_cmd(struct cmd* cmd, enum C_C code) {
cmd->code = code;
}
int main(int argc, char *argv[])
{
struct cmd cmd;
make_cmd(&cmd, P_WEST);
}
If you #include "protocol.h" as well you'll get an error (type redefinition) because it is already included in other.h.

C - Headers including each other

I have a file game.h which has this declaration
typedef struct Enemy {
...
}Enemy;
And a function
void moveEnemy(Level* l, Enemy* enemy){
...
}
Level is declared on levels.h, so in game.h I have:
#include "levels.h"
Everything was perfect, until I had to use Enemy in levels.h.
So, in levels.h I added:
#include "game.h"
And now I get a compilation error:
game.h:34:2: error: unknown type name ‘Level’
Level* level;
^
I have include guards on both .h
I don't know why I can't have on file including another.
What can I do?
Just add a forward declaration, like this in game.h before the function,
typedef struct Level Level;
since it's just a pointer to Level this will do it.

libwebsockets: Dereferencing pointer to incomplete type error

I'm using libwebsockets and I can't compile a demo code implemented by myself.
I created the context:
struct libwebsocket_context *context;
...
context = libwebsocket_create_context(&info);
and when I try to access the members of the struct libwebsocket_context, defined in private-libwebsockets.h:
struct libwebsocket_context {
struct pollfd *fds;
struct libwebsocket **lws_lookup; /* fd to wsi */
int fds_count;
int max_fds;
int listen_port;
...
};
For example,
printf("%d\n", context->listen_port);
The compiler returns,
error: dereferencing pointer to incomplete type
Thanks!
It seems that "struct libwebsocket_context" is not known for gcc - that's why this error occures. Are you sure that definition of this structure is included from .h file? I'd suggest you to insert for example #warning or #error with some message near definition of this struct (in .h file) and try to recompile your program. Your #error or #warning message should appear while compilation. If not - it means that gcc will not also see this struct.
The fact that the struct definition is in private-libwebsockets.h suggests that you are not supposed to use the struct members directly. You can #include that header to get access to the private implementation details of the library but you probably should not do it.

yacc - field has incomplete type

yacc doesn't seem to like when my tokens are of a type that I defined.
At the top of my grammar (.y) file in a %{ ... %} block, I include a header file that defines the following structure:
typedef struct _spim_register {
spim_register_type type; /* This is a simple enumeration, already defined */
int number;
} spim_register;
Before my list of rules, I have:
%token AREG
...
%union {
struct _spim_register reg;
}
...
%type <reg> register AREG
I get
error: field ‘reg’ has incomplete type
at the line in the %union clause while trying to compile the code produced by bison. In my %union statement, trying to declare reg by writing spim_register reg; gives the error:
unknown type name ‘spim_register’
It seems like there's something special about %union { ... }, because I'm able to use the data structures from my header file in the actions for the rules.
It would help if my #includes were in the right order...
The answer was, as user786653 hinted, here. I needed to include the header file that defines my custom structure before including the .tab.h file in the .l file.
I met the same problem. Because my *.l file like this:
include "y.tab.h"
include "FP.h"
then, I rewrote it like this:
include "FP.h"
include "y.tab.h"
It works. Thank you very much. #ArIck

C compile error

What does the following error mean when compiling.
Tilemap.h:21: error: conflicting types for ‘ThreeDWorld’
Tilemap.h:21: error: previous declaration of ‘ThreeDWorld’ was here
Tilemap.h:29: error: conflicting types for ‘CGPoint’
Tilemap.h:29: error: previous declaration of ‘CGPoint’ was here
Tilemap.h:31: error: conflicting types for ‘tileForCoordinates’
Tilemap.h:31: error: previous declaration of ‘tileForCoordinates’ was here
Why is it giving an error for what was there?My source file has one instance of it as such
typedef struct
{
int xPosition;
int yPosition;
}
CGPoint;
Are you including the header file from more than one place? Use a guard in the header file, if so.
For example, in Tilemap.h:
#ifndef TILEMAP_H
#define TILEMAP_H
// header file contents
#endif /* TILEMAP_H */
Stick some inclusion guards on your headers.
Your type definition is appearing more than once in your compilation unit.
You included the header file twice.
In my own code, I wrapped all header files with
#ifndef HEADER_FILE_NAME
#define HEADER_FILE_NAME
#endif
to avoid such errors.

Resources