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.
Related
I declared a struct in main.c file and i have functions in func1.c func2.c func1.h func2.h files.
main.c:
#include <stdio.h>
#include "func1.h"
#include "func2.h"
struct mystruct{
someting
};
main(){
struct mystruct var ={...};
myfunc(var);
}
func1.h:
void myfunc(struct mystruct);
And definition is in func1.c. Similar for func2 files. I got compilation errors, it is obvious that problem is header file dont have declaration of mystruct and so cant use it.
So what is the way to overcome that problem? Adding a new header file for struct or using extern keyword what i read. What is approprite choice i couldnt figure out at that point.
struct mystruct{
//something
};
Should be defined in a header file which all your c files that need it has access to.
mytypes.h
#ifndef mytypes_h
#define mytypes_h
struct mystruct{
int something;
};
#endif
func1.h
#ifndef func1_h
#define func1_h
#include "mytypes.h"
void myfunc(struct mystruct);
#endif
func1.c
#include "func1.h"
void myfunc(struct mystruct){
//do soemthing with struct
}
Then your main can include func1.h which already mytypes.h
main.c
#include "func1.h"
int main(){
}
I have a program which is processing list in c, it is working perfectly as long as I have it in one source file, when I try to separate it and compile it got this error “ delete_functions.c:15:13: error: unknown type name ‘nodetype’ ” same error goes for functionality_functions.c and insert_functions.c here is the code
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "delete_functions.h"
#include "insert_functions.h"
#include "functionality_functions.h"
int main(){
//i did not upload all the main function code because it is way to long
}
types.h
typedef char AirportCode[4];
typedef struct nodetype{
char Airport[4];
struct nodetype *next;
} nodetype;
delete_functions.h
void Delete(nodetype *list,char node[4]);
void DeleteLast(nodetype *list);
functionality_functions.h
void print(nodetype *head);
nodetype *search(nodetype *list,char item[4]);
nodetype *create();
insert_functions.h
void *InsertLast(nodetype *list,char item[4]);
void *InsertAfter(nodetype *list,char item[4],char node[4]);
According to the GCC error message, there error is in the delete_functions.c file.
Presumably, it looks like this at the beginning:
#include "delete_functions.h"
Since delete_functions.h does not itself include types.h, you need to include it first:
#include "types.h"
#include "delete_functions.h"
Alternatively, you can add include guards to your headers so, that they can safely be included multiple times, like this for types.h:
#ifndef TYPES_H
#define TYPES_H
typedef char AirportCode[4];
typedef struct nodetype{
char Airport[4];
struct nodetype *next;
} nodetype;
#endif
And for delete_functions.h:
#ifndef DELETE_FUNCTIONS_H
#define DELETE_FUNCTIONS_H
void Delete(nodetype *list,char node[4]);
void DeleteLast(nodetype *list);
#endif
The *_H include guard macros are necessary because otherwise, main.c would not compile anymore: each type in types.h can only be defined once per translation unit, and without the guards, every *.h would bring in another definition, leading to compiler errors.
My compilation order is :
core1.c
top.c
core1.c contents :
#include "header1.h"
#include "header2.h"
void function1() {
---- }
void function2() {
---- }
header1.c contents function declarations, enums, includes :
#include comdef.h
void function1();
void function2();
top.c contents :
#include "header1.h"
#include "header2.h"
void main() {
function1();
function2();
}
I will add more headers and more core C files into my project. Each core.c file needs the same header files. How to get this all working, without the need to put #include header1/2.h in each core1.c, core2.c etc, and include these headers only in main.c ?
You can use a global header including all files
/* glob.h */
#ifndef GLOB_H
#define GLOB_H
#include "header1.h"
#include "header2.h"
#endif /* GLOB_H */
and in your main file
#include "glob.h"
Even if this is considered bad style, there are several projects using this approach, i.e. gtk
Use one header for each source file:
core1.h:
#ifndef _CORE1
#define _CORE1
#include comdef.h
void function1();
void function2();
#endif
core1.c:
#include "core1.h"
void function1() {
---- }
void function2() {
---- }
top.c:
#include "core1.h"
void main() {
function1();
function2();
}
I'm currently working on a c project and to keep my code organized I use include files. One file includes.h has a typedef and I wan't to access this in a different included file util.h/util.c. I included them both in main.c in the following order: includes.h util.h.
main.c:
#include <stdio.h>
#include <stdlib.h>
//similar includes
#include "includes.h"
#include "util.h"
int main()
{
MyStructType somename;
somename.number = 5;
util_foo(somename);
return 0;
}
includes.h:
#pragma once
struct structname
{
int number;
};
typedef struct structname MyStructType;
util.h:
#pragma once
#include "includes.h" //added line (>accepted answer)
int util_foo(MyStructType blabla);
util.c:
#include <stdio.h>
#include "util.h"
int util_foo(MyStructType blabla)
{
printf("%d\n", blabla.number);
return 0;
}
I compile with this command: gcc -o main *.c
But this doesn't compile, do you have any idea why this doesn't work? Or how to fix it without changing my projects' structure completely?
Edit:
It is recommended to replace #pragma once with:
#ifndef STH_SIMILAR_TO_FILENAME
#define STH_SIMILAR_TO_FILENAME
//code
#endif
You need to add
#include "includes.h"
in util.h in order to make your code compile.
This is because you're relying on transitive #include directives to get the MyStructType typedef into util.h from main.c, but you're not doing the same in util.c.
The best solution (for maintainability purposes) is to minimize relying on transitive inclusions: you should include includes.h wherever it's needed (in this case, in util.h).
I have 3 *.c files (file1.c, file2.c and file3.c) and 1 *.h file (file3.h) in a project (Visual Studio).
/*******************************
file3.h
********************************/
#ifndef FILE3_H
#define FILE3_H
int gintVariable = 400;
#endif
/*******************************
file1.c
********************************/
#include "file3.h"
#include <stdio.h>
#include <conio.h>
int modifyGlobalVariable(void);
void printGlobalVariable(void);
int main(void)
{
modifyGlobalVariable();
printGlobalVariable();
printf("Global variable: %d\n", gintVariable++);
getch();
return 0;
}
/*******************************
file2.c
********************************/
#include "file3.h"
int modifyGlobalVariable(void)
{
return gintVariable++;
}
/*******************************
file3.c
********************************/
#include "file3.h"
#include <stdio.h>
void printGlobalVariable(void)
{
printf("Global: %d\n", gintVariable++);
}
When I build the solution in VS, it is giving error as "_gintVariable already defined in file1.obj".
I did check in the pre-processor output, the gintVariable is included in all the *.c files even though I have included include guards.
What mistake I am doing?
You should use 'extern' while declaring a global variable in header file.
Define it in any one of *.c file.
This should fix the issue.
For more on header files, read
How do I use extern to share variables between source files?
Including guards prevents multiple inclusion (or, more precisely, multiple compilation of the .h file content) in a single translation unit.
It is useful against this problem:
/* glob.h */
#ifndef H_GLOB
#define H_GLOB
struct s { int i; };
#endif
/* f.h */
#ifndef H_F
#define H_F
#include "glob.h"
struct s f(void);
#endif
/* g.h */
#ifndef H_G
#define H_G
#include "glob.h"
struct s g(void);
#endif
/* c.c */
#include "f.h" /* includes "glob.h" */
#include "g.h" /* includes "glob.h" */
void c(void) {
struct s s1 = f();
struct s s2 = g();
}
The inclusions is like a diamond:
glob.h
/ \
f.h g.h
\ /
c.c