简体   繁体   中英

Structures not working over multiple C source files

I've been searching for how to do this correctly but still none of the solutions or explanations I found worked. I have my main function in rsort.c:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "records.h"
#define NAMESIZE 20
#define BLOCK 2
#define LINESIZE 512

typedef struct name{
char last[NAMESIZE];
char first[NAMESIZE];
} name;

typedef struct record{
name name;
int score;
} record;

typedef struct record_list{
record *data;   
size_t nalloc;  
size_t nused;   
} record_list;

int main(void) {
record_list a;
record *ab = malloc(sizeof(record));
}

And a records.h header file:

#ifndef RECORDS_H
#define RECORDS_H
#include "records.c"

struct name name;
struct record record;
struct record_list record_list;

int list_compare(const void *left, const void *right);
#endif

And finally records.c where all my errors occur:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "records.h"

struct name name;
struct record record;
struct record_list record_list;

int list_compare(const void * left, const void * right) {
const record * a = (const record *) left;
const record * b = (const record *) right;
}

With this I get about 5 screens worth of errors on the bash shell. I'm using GCC. I've tried a rearranging the structs in a ton of different ways but still get the same "type name not found error" in the first few lines of record.c. I don't really understand where the forward declarations are supposed to go, and I had the structs in the .h file before moving them to rsort.c with no real change. At this point I just want to know what lines/blocks go where, written how, as a vague description of what to change won't really help me. I've tried taking vague advice for other peoples code from posts on this site but it hasn't got me very far.

To me it makes the most sense to have the structs in the header file, but I couldn't get that working. The problem might well be isolated to records.c as the errors appear to only come from there, although I can hardly tell as it produces more errors than I can count. The first errors are always complaining about the types being used from my structs its not finding. This code does work when it's all in one file, but I am required to use multiple source files so that's what I'm doing. I also gutted the functionality to preserve my work, by emptying the functions that were necessary to show and deleting the ones that have nothing to do with the problem. Thanks for reading.

I don't think records.h should #include "records.c" . I haven't tried it myself, but that should fix it.

Generally you only #include .h files.

The full definition of the structs need to be available in the header file. Also please never include c files.

rsort.c

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "records.h"

int main(void) {
    record_list a;
    record *ab = malloc(sizeof(record));
}

records.h

#ifndef RECORDS_H
#define RECORDS_H
#include <stdlib.h>

#define NAMESIZE 20
#define BLOCK 2
#define LINESIZE 512

typedef struct name {
    char last[NAMESIZE];
    char first[NAMESIZE];
} name;

typedef struct record {
    name name;
    int score;
} record;

typedef struct record_list {
    record *data;   
    size_t nalloc;  
    size_t nused;   
} record_list;

int list_compare(const void *left, const void *right);
#endif

records.c

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "records.h"

int list_compare(const void * left, const void * right) {
    const record * a = (const record *) left;
    const record * b = (const record *) right;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM