0

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))

1
  • do not place data instances in header files. Because those data elements will be generated in each .c file that includes that header file. It will fail during the linking and/or running steps due to duplicate definitions of the data, Commented May 19, 2022 at 15:07

1 Answer 1

3

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"
Sign up to request clarification or add additional context in comments.

3 Comments

Which option is the cleanest/recommended?
Depending on the size of your program, I'd probably split type/structure definitions into a separate header file you can include wherever. Beyond that, the first option; anything that depends on you having to know the correct order for your includes is bound to be brittle.
Alternatively, if all functions/types don't use struct Pool but rather a pointer to it like struct Pool* but not struct Pool [] then you could just place a forward declaration: struct Pool;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.