How to include array of struct defined in another header in C? - arrays

I have the below files:
swaps.c:
#include "swaps.h"
#include "./poolutils.h"
double someFunction(struct Pool pools[]){
//do some stuff
}
int main(){
struct Pool pool1 = {"A","C",3.0,9000.0,0.0};
struct Pool pool2 = {"B","D",20.0,20000,0.0};
struct Pool pools[N];
pools[0] = pool1;
pools[1] = pool2;
double a = someFunction(pools);
}
swaps.h has signature of someFunction
poolutils.c has some functions
poolutils.h:
#ifndef _POOLUTILS_H
#define _POOLUTILS_H
struct Pool {
char *token1;
char *token2;
double reserve1;
double reserve2;
double fee;
};
//signatures of poolutils.c functions
#endif
When compiling (gcc -c swaps.c poolutils.c), I am getting the below error:
In file included from swaps.c:1:
swaps.h:4:44: error: array type has incomplete element type ‘struct Pool’
4 | double someFunction(struct Pool pools[]);
Now, I do include the header where the struct Pool is defined, so swaps.c should know about it but I understand that somehow swaps.h does not, how can I make it aware of the outside definitions ?
(gcc version 10.2.1 20210110 (Debian 10.2.1-6))

Simply add
#include "./poolutils.h"
to your swaps.h too, if swaps.h needs the Pool struct definition.
Since you've dutifully put include guards in poolutils.h, you can use the header multiple times in a single compilation unit.
Alternately, swap the order of your includes; the effect is the same here.
#include "./poolutils.h"
#include "swaps.h"

Related

struct declared in tools.h and defined in tools.c. Can't link them to main.c

file tools.h:
//some macro definitions
struct name;
//other function prototypes
file tools.c:
#include "tools.h"
struct name
{
FILE *src;
int value;
}
//definitions of functions declared in tools.h
file main.c:
#include <stdio.h>
#include "tools.h"
int main()
{
struct name *ptr;
ptr = malloc(sizeof(struct name));
ptr->FILE = fopen(filename, "r");
ptr->value = 12;
...
}
At first, I built tools.o by using:
$ gcc tools.c -c
Without any error or warning, tools.o was built in the current directory.
Now, I tried to build executable by using:
$ gcc main.c tools.o -o exe
and I got errors of same type (all the errors were of same type, caused due to accessing the struct element). Here's the sample of that error I got:
main.c: In function ‘main’:
main.c:17:22: error: invalid use of undefined type ‘struct name’
17 | buffer = malloc(ptr->value+1);
Please explain why this happened and what wrong I did while linking or in my code.
tools.h file
#ifndef TOOLS_H
#define TOOLS_H
#include <stdio.h>
struct name
{
FILE *src;
int value;
};
int foo(struct name*);
struct name *bar(double, FILE*, const char *); //prototypes of functions defined in tools.c
#endif
tools.c
#include "tools.h"
int foo(struct name*)
{
/* ... */
}
struct name *bar(double, FILE*, const char *)
{
/* ... */
}
Looks like you have some confusion with regards to forward declaration of a structure and a structure declaration (which defines a type).
From Forward declaration:
a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.
In tools.h, this
struct name;
is forward declaration of structure struct name. Note that, it declares an incomplete type because the specification [list defining the content] of struct name is unknown at this time.
You have included the tools.h in main.c and when compiling main.c compiler does not find the specification of struct name and hence, throwing error on the statements using it.
In tools.c, you are declaring the struct name (which defines a type):
struct name
{
FILE *src;
int value;
};
when compiling tools.c, compiler knows about the specification of structure struct name and hence, it's compilation is successful.
The other post [by #0___________] gives the appropriate way to solve this problem.

Forward declaration issue, two compilers

I've been developing in C using eclipse as my IDE in my virtual machine with ubuntu, I've made some progress and I wanted to test them in the real product which is an embedded system using powerpc.
In order to compile that program for our product I use Code::Blocks in Windows but the compiler is a powerpc version of the gcc.
The same code is giving me an error in the powerpc version that doesn't appear in the ubuntu version.
I have two header files gral.h and module_hand.h as follows:
The gral.h file:
#ifndef HEADERS_GRAL_H_
#define HEADERS_GRAL_H_
#include "module_hand.h"
typedef struct PROFILE
{
module_t mod; // this one comes from module_hand.h
int var1; // some other random variables
} profile_t;
#endif /* HEADERS_GRAL_H_ */
The module_hand.h is defined as follows
#ifndef HEADERS_MODULE_HAND_H_
#define HEADERS_MODULE_HAND_H_
#include <stdint.h>
#include "gral.h"
typedef struct PROFILE profile_t;
typedef struct module
{
char name[30]; // name of module
char rev[30]; // module revision
char mfr[30]; // manufacturer
} module_t;
int Mod_Init(profile_t *profile);
/* some other random functions */
#endif /* HEADERS_MODULE_HAND_H_*/
As you'll see, I don't use the PROFILE struct in the module struct, But I declare it forward to use it in the declaration of the Mod_Init function
This gives me a Error: redefinition of typedef 'profile_t'
and error: previous declaration of 'profile_t' was here
If I remove the forward declaration the error is Error: parse error before '*' token
where the line number is the line of the function declaration.
My doubt is what am I missing, and why gcc in Ubuntu does compile it with no problem.
Your powerpc compiler is enforcing the C99 rule that
If an identifier has no linkage, there shall be no more than one
declaration of the identifier (in a declarator or type specifier) with
the same scope and in the same name space, except for tags as
specified in 6.7.2.3.
(C99 6.7/3)
Your Linux compiler is observing the relaxed version of that rule that was introduced in C11:
If an identifier has no linkage, there shall be no more than one
declaration of the identifier (in a declarator or type specifier) with
the same scope and in the same name space, except that:
a typedef name may be redefined to denote the same type as it currently does, provided that type is not a variably modified type;
tags may be redeclared as specified in 6.7.2.3.
(C11 6.7/3; also C17 6.7/3)
Supposing that the compilation options are the same, the behavior difference surely arises from using different versions of GCC. More recent versions default to more recent versions of the language.
You could try adding -std=gnu11 or -std=c11 to the command-line options (for both targets) to try to get consistency. If your powerpc version of GCC is too old to accept those then you really need to update to a newer version.
Note also, however, that you don't need to have this problem in the first place. Given that module_hand.h includes gral.h, the former has no need whatever to redefine a typedef that the latter already defines.
Moreover, the fact that these two headers each include the other is a strong suggestion that they ought to be combined into one. Multiple-inclusion guards prevent an actual loop, but they are not an adequate solution.
In the gral.h header file, you define profile_t using typedef, then you redefine profile_t with another typedef in module_hand.h. You should just define the struct PROFILE in gral_h and include gral.h in module_hand.h.
gral.h:
#ifndef HEADERS_GRAL_H_
#define HEADERS_GRAL_H_
#include "module_hand.h"
typedef struct PROFILE {
module_t mod; // this one comes from module_hand.h
int var1; // some other random variables
} profile_t;
#endif /* HEADERS_GRAL_H_ */:
module_hand.h:
#ifndef HEADERS_MODULE_HAND_H_
#define HEADERS_MODULE_HAND_H_
#include <stdint.h>
typedef struct module
{
char name[30]; // name of module
char rev[30]; // module revision
char mfr[30]; // manufacturer
} module_t;
int Mod_Init(struct PROFILE *profile);
/* some other random functions */
#endif /* HEADERS_MODULE_HAND_H_*/
Well I read your answers and comments and decided to try another approach.
As some of you said, I had some kind of recursion, I wanted to keep every structure within its respective header file, but now, I dropped the idea and merged the structures in one file only.
My new approach:
Gral.h
#ifndef HEADERS_GRAL_H_
#define HEADERS_GRAL_H_
typedef struct module
{
char name[30]; // name of module
char rev[30]; // module revision
char mfr[30]; // manufacturer
} module_t;
typedef struct PROFILE {
module_t mod; // this one comes from module_hand.h
int var1; // some other variables
} profile_t;
#endif /* HEADERS_GRAL_H_ */:
Module.h
#ifndef HEADERS_MODULE_HAND_H_
#define HEADERS_MODULE_HAND_H_
#include <Gral.h>
int Mod_Init(profile_t *profile);
/* some other functions */
#endif /* HEADERS_MODULE_HAND_H_*/
And when any other structure comes up, I'll declare it in Gral.h and include the header file.
Regarding the compilers, they aren't the same version. The powerpc is quite old now. That would explain the powerpc compilation errors.
Thank you again.

include guard does not allow me to declare a type

I encountered such an interesting problem, because of the include guard trigger, the type declared in the header file is not declared.
I have a mutex file.h which describes the mutex_t type, it depends on the thread_t type that is declared in the thread file.h, however, thread_t depends on mutex_t, and the main.c file uses both of these types.
Problem:
when compiling main.c, the thread file is included.h, which includes mutex.h, however, because of the thread.h was already connected, then it rolls out include guard and does not allow declaring the thread_t type in mutex.h
thread.h
#ifndef _THREAD_H_
#define _THREAD_H_
#include "mutex.h"
typedef struct thread
{
int thread_data;
} thread_t;
extern mutex_t* get_mutex();
#endif
mutex.h
#ifndef _MUTEX_H_
#define _MUTEX_H_
#include "thread.h"
#include "stdint.h"
typedef struct mutex {
uint8_t lock_status; //+0
thread_t* waiting_thread_queue[1024];
} mutex_t;
extern thread_t* get_thread();
#endif
main.c
#include "stdlib.h"
#include "stddef.h"
#include "thread.h"
int main(void) {
get_thread();
get_mutex();
return 0;
}
This is the error I get:
emilia#emilia-pc:~/test$ gcc main.c
In file included from thread.h:4,
from main.c:3:
mutex.h:9:5: error: unknown type name ‘thread_t’
9 | thread_t* waiting_thread_queue[1024];
     | ^~~~~~~~
mutex.h:12:8: error: unknown type name ‘thread_t’
12 | extern thread_t* get_thread();
     | ^~~~~~~~
You have a circular dependency between two headers. The thread header needs mutex and vice versa.
The way you have done it doesn't make sense. The thread header declares a function called get_mutex, and vice versa, the mutex header declares a function called get_thread.
Probably, what this points to is that the headers are so closely related, they should be one header.
Anyway, the way we can break a circular dependency is by using a forward declaration in one of the headers.
// child.h
struct parent; // incomplete "forward" declaration
struct child {
struct parent *parent;
};
// parent.h
#include "child.h"
#define MAX_CHILDREN 42
struct parent {
struct child *children[MAX_CHILDREN];
};
The "forward" declaration struct parent doesn't require the parent.h header. The parent.h header picks up that declaration, but it does not interfere with its own declaration, because this is allowed:
struct parent;
struct parent { ... };
Note that _THREAD_H_ intrudes into a reserved implementation namespace. Names beginning with an underscore followed by either a capital letter or another underscore are reserved. Violating this rule is actually undefined behavior, which can have any consequences whatsoever. The implementation can stop translating your program with a diagnostic message. Or it can predefine a macro called _THREAD_H_, so that your own header fails to produce its expected contents.

Warning: declaration of '...' will not be visible outside this function [-Wvisibility]

First of all, I've googled the error and read these answers:
I don't understand why compiler is giving me error with this code
C : Warning about visibility of a struct
But none of them helped me, so here we are.
The problem resides somewhere in between these 2 structures, prx_data_s which store generic data and prx_ops_s that defines pointers to functions that will use that data.
I'll simplify the sources for the example:
prx_data.h
#ifndef PRX_EXAMPLE_DATA_H
#define PRX_EXAMPLE_DATA_H
#include "prx_ops.h"
struct prx_data_s {
enum prx_op_t op;
char *keyquery;
};
char *get_query(struct prx_data_s *dt);
#endif
prx_data.c
#include "prx_data.h"
char *get_query(struct prx_data_s *dt)
{
return dt->keyquery;
}
prx_ops.h
#ifndef PRX_EXAMPLE_OPS_H
#define PRX_EXAMPLE_OPS_H
#include "prx_data.h"
enum prx_op_t {
PRX_EXAMPLE_OP = 2
};
struct prx_ops_s {
int (*dec) (struct prx_data_s *);
};
#endif
I'm trying to compile the object from the above example with:
clang -c prx_data.c -o prx_data.o -std=c11 -g -Wall
And this is the output error:
In file included from prx_data.c:1:
In file included from ./prx_data.h:4:
./prx_ops.h:11:24: warning: declaration of 'struct prx_data_s' will not be visible outside of this function [-Wvisibility]
int (*dec) (struct prx_data_s *);
^
All help is welcome, thanks :)
You have a problem with circular dependencies in your header:
prx_data.h:
#include "prx_ops.h" <<< Here we do not yet see the struct definition
prx_ops.h:
#include "prx_data.h" <<<< Nothing will be included due to inclusion guards.
struct prx_ops_s {
int (*dec) (struct prx_data_s *); <<<< Here a new struct type is declared.
};
later back in prx_data.h:
struct prx_data_s {
enum prx_op_t op;
char *keyquery;
};
When reading prx_ops.h, you dont 't have prx_data.h included because the compiler is reading prx_ops.h from the include at the beginning of prx_data.h. You thus have to forward declare it.
Try adding
struct prx_data_s;
at the beginning of prx_ops.h`
Hope that helps ~~

Clear tutorial explaining modular programming in C?

I'm just getting started with modular programming in C. I think I'm doing something wrong with the inclusions, because I'm getting a lot of conflicting types for 'functionName' and previous declaration of 'functionName' was here errors. I did put inclusion guards in place.
Do you know a clear tutorial that explains modular programming in C, especially how the inclusions work?
Update: I have tried to isolate my issue. Here's some code, as requested.
Update 2: updated code is below. The errors have been updated, too.
/*
* main.c
*/
#include <stdio.h>
#include "aStruct.h"
int main() {
aStruct asTest = createStruct();
return 0;
}
/*
* aStruct.h
*/
#ifndef ASTRUCT_H_
#define ASTRUCT_H_
struct aStruct {
int value1;
int value2;
struct smallerStruct ssTest;
};
typedef struct aStruct aStruct;
aStruct createStruct();
#endif /* ASTRUCT_H_ */
/*
* smallerStruct.h
*/
#ifndef SMALLERSTRUCT_H_
#define SMALLERSTRUCT_H_
struct smallerStruct {
int value3;
};
typedef struct smallerStruct smallerStruct;
smallerStruct createSmallerStruct();
#endif /* SMALLERSTRUCT_H_ */
/*
* aStruct.c
*/
#include <stdio.h>
#include "smallerStruct.h"
#include "aStruct.h"
aStruct createStruct() {
aStruct asOutput;
printf("This makes sure that this code depends on stdio.h, just to make sure I know where the inclusion directive should go (main.c or aStruct.c).\n");
asOutput.value1 = 5;
asOutput.value2 = 5;
asOutput.ssTest = createSmallerStruct();
return asOutput;
}
/*
* smallerStruct.c
*/
#include <stdio.h>
#include "smallerStruct.h"
smallerStruct createSmallerStruct() {
smallerStruct ssOutput;
ssOutput.value3 = 41;
return ssOutput;
}
This generates the following error messages:
At aStruct.h:10
field 'ssTest' has incomplete type
At main.c:8
unused variable `asTest' (this one makes sense)
The base of inclusion is to make sure that your headers are included only once. This is usually performed with a sequence like this one:
/* header.h */
#ifndef header_h_
#define header_h_
/* Your code here ... */
#endif /* header_h_ */
The second point is to take care of possible name conflicts by handling manually pseudo namespaces with prefixes.
Then put in your headers only function declarations of public API. This may imply to add typedefs and enums. Avoid as much as possible to include constant and variable declarations: prefer accessor functions.
Another rule is to never include .c files, only .h. This is the very point of modularity: a given module dependant of another module needs only to know its interface, not its implementation.
A for your specific problem, aStruct.h uses struct smallerStruct but knows nothing about it, in particular its size for being able to allocate an aStruct variable. aStruct.h needs to include smallerStruct.h. Including smallerStruct.h before aStruct.h in main.c doesn't solve the issue when compiling aStruct.c.
The multiple definition problem is most likely coming from the way you're including the code. You are using #include "aStruct.c" as opposed to #include "aStruct.h". I suspect you are also compiling the .c files into your project in addition to the #include. This causes the compiler to become confused due to the multiple definitions of the same function.
If you change the #include to #include "aStruct.h" and make sure the three source files are compiled and linked together, the error should go away.
Such errors mean that function declaration (return type or parameter count/types) differs from other function declarations or function definition.
previous declaration message points you to the conflicting declaration.

Resources