unknown type name "Engine" - c

I am getting an error:
engine/renderer/render.h:8:19: error: unknown type name ‘Engine’
8 | void initRenderer(Engine* engine);
| ^~~~~~
Even though I have defined the type Engine and included it in the render.h file
app.h:
#pragma once
#include <defines.h>
#include <string.h>
#include <GLFW/glfw3.h>
#include <containers/vector.h>
typedef struct Engine {
uint16 w;
uint16 h;
GLFWwindow* window;
int backend;
Vector entities;
char* name;
void* (*Update)();
void* (*onStart)();
} Engine;
render.h:
#pragma once
#include <defines.h>
#include <renderer/backend/vulkan.h>
#include <app.h>
void initRenderer(Engine* engine);
I have included <app.h> in the include directory of the project with -I and it works in all other files

Related

Defining a typedef with the constructor in c

I'm writing a project. This project will be injected into another program and will enable that program to write CRUD operations on the files it wants. The constructor I wrote in my project will take a struct (data) from the injected program and define it as a type with typedef (like 'typedef struct Book_ Entity'). It will then do all of its operations over this type. However, the point where I stuck is this: After declaring an Entity with typedef, I wrote 'typedef type_t Entity;' in the Constructor macro I wrote. the line of code does not work or is valid only within the macro. You can better understand what the problem is from the program below.
NOTE: The data_num in data_constructor tells how many variables the data consists of. Required for Delete and Update operations.
Part of Constructors.h
#include <stdio.h>
#include <string.h>
#ifndef CONSTRUCTORS
#define CONSTRUCTORS
#define DATA_CONSTRUCTOR
typedef Entity;
#define data_constructor(type_t, number_of_data) typedef type_t Entity; int data_num = number_of_data;
#define FILE_CONSTRUCTOR
inline void file_constructor(const char* path)
{
char* g_file_path;
strcpy(g_file_path, path);
}
#endif
The constructor I wrote above is included. It defines the data structure (such as 'struct Book_') received from the user as a type. I got an error like expected 'Entity' {aka 'int'} but argument is of type 'struct Book_' when I ran the program while waiting for a structure like 'typedef struct Book_ Entity'.
Part of InMemoryDal.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Constructors.h"
#include "Defines.h"
#include "FileHelper.h"
#ifndef IN_MEMORY_H
#define IN_MEMORY_H
#endif
extern char *g_file_path;
extern int data_num;
FILE *g_file;
FILE *g_temp_file;
void Add(Entity entity);
void Delete(Entity entity);
void Update(Entity entity);
void Add(Entity entity)
{
F_OPEN(g_file, g_file_path, MODE_AB);
F_WRITE(&entity, sizeof(Entity), 1, g_file);
fclose(g_file);
}
This is where I write the functions of the program. Here, the Add () function takes an Entity type entity from the user and prints it to the file. Macros such as F_OPEN and F_WRITE used in it are defined in another file. It is just below in the file.
#include <stdio.h>
#include <stdlib.h>
#ifndef MODES
#define MODE_W "w"
#define MODE_R "r"
#define MODE_WB "wb"
#define MODE_RB "rb"
#define MODE_A "a"
#define MODE_AB "ab"
#endif
#define TEMP_FILE_PATH "temp_file.txt"
#define F_OPEN(fp, f_path, mode) fp=fopen(f_path, mode); if(fp == NULL) exit(1);
#define F_WRITE(buffer, element_size, element_count, fp) fwrite(buffer, element_size, element_count, fp); if(fwrite==0) exit(1);
Here is the code I tested the program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "InMemoryDal.h"
struct Book_
{
int id;
char title[50];
char author[50];
int print_number;
};
int main()
{
data_constructor(struct Book_, 4);
file_constructor("\\test123.txt");
struct Book_ b;
b.id=1;
strcpy(b.title, "testname");
strcpy(b.author, "testauthor");
b.print_number=34;
Add(b);
return 0;
}
I think I explained my problem well. I want your help to set up the constructor structure I want. Thanks!

What is wrong with this? C

I'm having trouble finding my error. Here is a definition in structures.h
typedef struct book {
bank_account_t **accounts;
transaction_t **transactions;
} book_t;
And here is in functions.c where I include header and try to use the type book_t
#include "structures.h"
void load_book(book_t *book) {
}
But I get this error
functions.c:10:16: error: unknown type name ‘book_t’
void load_book(book_t *book) {
^
Edits with more code below:
In my main file I order my .h files like so
#include "structures.h"
#include "functions.h"
structures.h
#ifndef STRUCTURES_H
# define STRUCTURES_H
typedef struct bank_account {
char *name;
int amount;
} bank_account_t;
typedef struct transaction {
char *name;
int amount;
} transaction_t;
typedef struct book {
bank_account_t **accounts;
transaction_t **transactions;
} book_t;
#endif
function.c
#include <stdio.h>
#include "functions.h"
#include "structures.h"
#include "bank_account.h"
#include "transaction.h"
void load_book(book_t *book) {
}
void init_book() {
}
bank_account.h
#ifndef BANK_ACCOUNT_H
# define BANK_ACCOUNT_H
void init_new_bank();
void deinit_new_bank();
#endif
transaction.h
#ifndef TRANSACTION_H
# define TRANSACTION_H
#endif
I think the problem must be in functions.h (which is not included in the original post).
functions.h
#ifndef FUNCTIONS_H
# define FUNCTIONS_H
/* [MarkU] required: include definition of book_t */
#include "structures.h"
void load_book(book_t *book);
void init_book();
#endif
Without the #include structures.h there is no definition of the boot_t type.
Built and verified with mingw32-gcc 4.7.2. Omitting the #include, I see the error message.
In functions.c change the order of those:
#include "functions.h"
#include "structures.h"
to be
#include "structures.h"
#include "functions.h"
The subtile thing is that the error message origins from functions.c not from functions.h.
Assuming the protoytpe to load_book(book_t *) in functions.h, it needs to know about book_t.
So the optimal solution to this would be to include structures.h into functions.h (as also already pointed out by MarkU's answer).
Lesson learned: Always (and only) include what you need and where you need it. Avoid (subtile) dependencies.

Error unkown filetype although it is declared in a header file and included

i have an error that I do not understand. I can get rid of the error if I just put the include in the mail.c file and not in the mail.h file. My code looks like this:
The mail.h file:
#ifndef MAIL_H
#define MAIL_H
//Normal needed headers
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//Custom headers
#include "pop3.h"
#include "pasw.h"
#include "rese.h"
#define NAME_SIZE 100 //Defines the length of e.g. the user name
#define PASSW_SIZE 100 //Defines the length of the password
#define ADD_SPACE 10 //Defines some additional space for arrays
#define HELPTEXT "help.txt" //Defines the name of the help.txt file
enum bool {true, false};
typedef enum bool bool;
#endif
The pop3.h file:
#ifndef POP3
#define POP3
// Open SSl headers
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
//Needed for the password input to not show the password
#include <termios.h>
//Normal needed headers
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//Custom headers
#include "mail.h"
#include "pasw.h"
#include "rese.h"
void pop3(unsigned char *, unsigned char *, unsigned char *);
int GetUserPassw(unsigned char *, unsigned char *);
int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);
int GetUsername(unsigned char *;);
int GetInput(unsigned char*, int);
int GetNumberOfMessages(BIO *);
void RetrieveEmail(BIO *);
#endif
In mail. c I have included mail.h and in pop3.c I have included pop3.h. The error I get is:
gcc -lcrypto -lssl -c mail.c
In file included from mail.h:10:0,
from mail.c:11:
pop3.h:24:69: error: unknown type name ‘bool’
int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);
^
pop3.h:24:75: error: unknown type name ‘bool’
int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);
^
Makefile:12: recipe for target 'mail.o' failed
make: *** [mail.o] Error 1
And my Makefile looks like this:
CC=gcc
CFLAGS=-lcrypto -lssl -o
OFLAGS=-lcrypto -lssl -c
RM=rm -i
all: mail
mail: mail.o pop3.o pasw.o rese.o
$(CC) $(CFLAGS) mail mail.o pop3.o pasw.o rese.o
mail.o: mail.c
$(CC) $(OFLAGS) mail.c
pop3.o: pop3.c
$(CC) $(OFLAGS) pop3.c
pasw.o: pasw.c
$(CC) $(OFLAGS) pasw.c
rese.o: rese.c
$(CC) $(OFLAGS) rese.c
clean:
rm *.o mail
Before I have changed the file structure a bit it has worked. But I can get rid of this problem by puting the include "commands" out of mail.h and into mail.c.
I am looking forward to hear from you.
King regards,
Greenality
First of all read my comment. Second of all, you define the type bool after you include the headers that depend on the type.
In pop3.h you use the type bool in the function declaration of
int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);
But when pop3.h is included into mail.h, bool is not defined at that point yet where pop3.h is included.
As you also include mail.h into pop3.h, you have a cyclic dependency between the header files, so you'd have to create its own file for the type bool and #include it into the pop3.h header. An alternative is to clean up your header dependencies properly as pop3.h has only a dependency on #include <openssl/bio.h> and the bool type.
Also instead of creating your own boolean type, use the bool from stdbool.h which is also properly convertible unlike yours and uses a real boolean type if the compiler provides it.
So your pop3.h would end up as
#ifndef POP3
#define POP3
#include <stdbool.h>
// Open SSl headers
#include <openssl/bio.h>
void pop3(unsigned char *, unsigned char *, unsigned char *);
int GetUserPassw(unsigned char *, unsigned char *);
int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);
int GetUsername(unsigned char *;);
int GetInput(unsigned char*, int);
int GetNumberOfMessages(BIO *);
void RetrieveEmail(BIO *);
#endif
while the missing header files required for the implementation will be directly included into the pop3.c file:
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "mail.h"
#include "pasw.h"
#include "rese.h"
I can't help you with mail.h as it's missing its interface. Everything that you have in your mail.h should actually be in mail.c as it's about implementation details.
Only what you want to provide to users of your mail.c implementation should be part of the interface in mail.h.
When compiling mail.c, the file first includes the content of mail.h, which in turn includes the content of pop.h, which attempts to include mail.h but ends up getting nothing from the file due to MAIL_H being already defined.
Since pop.h was included before enum bool was defined in mail.h, the content of pop.h has no knowledge of what bool is. Therefore the compiler error. Simply define enum bool before including pop.h in mail.h should solve the problem.

error: expected identifier or '(' before 'typedef'|

i'm working since a few weeks on the same project and never had this kind or error.
I got it now without touching to the concerned file, which is down there:
#ifndef DIJKSTRA_H_INCLUDED
#define DIJKSTRA_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INFINI 100000.0
typedef struct dij
{
int ordre;
float** adjacencePoids;
float* l;
int* pred;
}t_dij;
int choix_action();
int choix_sommet_depart();
int choix_sommet_arrivee();
t_dij* allouer_dijkstra();
t_dij* allouer_dijktra_durees();
t_dij* dijkstra();
void afficher_resultat();
void sauver_resultat();
void detruire_struc();
#endif // DIJKSTRA_H_INCLUDED
This code gives me this error (Compiler is MinGW)
Line 11 error: expected identifier or '(' before 'typedef'|
Thanks!
I suspect the error lies in the file the header shown is included from, or in a file included just before this one.

C Error C2371 redefinition

Error 2 error C2371: 'QixC': redefinition; different basic types line 5
Error 5 error C2371: 'QixC': redefinition; different basic types line 5
Error 13 error C2371: 'QixC': redefinition; different basic types line 5
This is the part of the file Game.h:
#include "Graphics_Console.h"
#include <stdio.h>
#include <conio.h>
typedef struct
{
int X;
int Y;
}QixC;
typedef struct
{
int X;
int Y;
}CCursor;
And I use them in Game.c :
#include "Game.h"
#include <stdio.h>
#include <conio.h>
#include <time.h>
int QIX(int nivell)
{
QixC Qix;
CCursor Cursor;
HANDLE hScreen;
int DirQixX,DirQixY;
int IniciX,IniciY;
int FiX, FiY;
int DirStix=0;
int Area=0,AreaTotal=0,AreaObjectiu=(FI_X-INICI_X)*(FI_Y-INICI_Y)*0.75;
int Pantalla=1;
int Puntuacio=0;
int tecla=0;
int viu=1;
int NCops=0, Velocitat=1000/(nivell*0.70);
int XocStix=0;
char continuar[1];
hScreen = GetStdHandle(STD_OUTPUT_HANDLE);
InitScreen(hScreen);
do
{
system("CLS");
IniciX=INICI_X,IniciY=INICI_Y;
FiX=FI_X, FiY=FI_Y;
Cursor.X=INICI_X+(FiX-INICI_Y)/2,Cursor.Y=FI_Y;
DibuixarRectangle(IniciX,IniciY,FI_X,FI_Y,hScreen);
InfoPuntsPartida(hScreen, Puntuacio);
InfoPantallaPartida(hScreen, Pantalla);
Qix.X=Aleatori(IniciX+1,FiX-1),Qix.Y=Aleatori(IniciY+1,FiY-1);
InicialitzarDirQix(&DirQixX,&DirQixY);
PintarQix(Qix,hScreen);
PintarCursor(Cursor.X,Cursor.Y,hScreen);
do{
if(_kbhit())
{
tecla=LlegirEvent();
TractarEvent(tecla,Cursor,&IniciX,&IniciY,&FiX,&FiY,Qix,&DirStix,&Area,hScreen);
if(Area)
{
Puntuacio=Puntuacio+Area;
AreaTotal+=Area;
Area=0;
InfoPuntsPartida(hScreen,Puntuacio);
}
}
NCops++;
if(NCops==Velocitat)
{
if(DirStix!=0)
XocStix=QiXXocStiX(Qix,Cursor,DirStix);
if(!XocStix)
MoureQix(Qix,&DirQixX,&DirQixY,IniciX,IniciY,FiX,FiY,hScreen);
else
viu=0;
NCops=0;
}
}while((tecla!=TECLA_q)&&(tecla!=TECLA_Q)&&viu &&(AreaTotal<AreaObjectiu));
GameOver(hScreen);
TextColor(LIGHTGREY,BLACK,hScreen);
GotoXY(0,FI_Y+1,hScreen);
return Puntuacio;
system ("PAUSE");
printf("Continuar?(s/n)");
scanf("%c",&continuar);
}while(continuar!="s");
}
Sorry for the foreign words and names, english is not my first language.
I can't see where is redefined. They don't appear anywhere else,but are passed as parameters to some functions. Any help, please?
Try guarding your header against multiple inclusion.
#ifndef GAME_H
#define GAME_H
#include "Graphics_Console.h"
#include <stdio.h>
#include <conio.h>
typedef struct
{
int X;
int Y;
}QixC;
typedef struct
{
int X;
int Y;
}CCursor;
#endif /* GAME_H */
By default, if you include multiple headers which directly or indirectly include your header, you'll get multiple competing (if identical) versions of its structures and functions. You can avoid this by starting your header
#ifndef SOMETHING_UNIQUE_TO_YOUR_HEADER
#define SOMETHING_UNIQUE_TO_YOUR_HEADER
and ending it
#endif /* SOMETHING_UNIQUE_TO_YOUR_HEADER */
which guarantees that any source file will include at most a single copy of your structures.

Resources