Compiling multiple C files having underlying functions from a common file - c

I went through all StackOverflow threads regarding this but could not find one that matched my problem.
I am trying to implement different data structures (implemented in stack.c, queue.c) which get basic functions from another file storage.c. Here are their header files -
storage.h
typedef struct _circular_list
{
int *array, head, tail;
size_t size, count;
} circular_list;
typedef circular_list *clist;
clist Initialize(size_t);
clist WriteAtTail(clist, int);
int RemoveAtHead(clist);
int RemoveAtTail(clist);
clist WriteAtHead(clist, int)
stack.h
#include "storage.h"
#define stack clist
#define push(s, i) WriteAtTail(s, i)
int pop(stack);
int popKey(stack, int);
queue.h
#define queue clist
#define EnQueue(q,i) WriteAtTail(q,i)
#define DeQueue(q) RemoveAtHead(q)
I include them in the main C file
driver.c
#include <stdio.h>
#include "stack.h"
#include "queue.h"
Now if i try to compile them using
gcc -o driver driver.c storage.c stack.c queue.c
I get an error unknown type name 'clist' in queue.h.
If I try to include storage.h in queue.h as well, I get an error that I have multiple declarations for the structure.
How do I go about compiling this?

If you don't include storage.h in stack.h or queue.h you have to include it in your driver.c file before including one of the other files. Otherwise the compiler does not know about the declarations of storage.h.
A better solution is to include storage.h directly in stack.h and queue.h. To avoid getting compiler errors due to multiple declarations you have to change your storage.h like this:
#if !defined(INC_STORAGE_H)
#define INC_STORAGE_H
/* original storage.h contents go here */
#endif /* INC_STORAGE_H */
Or simply write #pragma once at the beginning of the storage.h if your compiler supports it. (If you're writing a library for other people to use I recommend the first solution)

Related

how to use a struct defined in a certain header file from another header in C?

I have two headers files, stack.h
#ifndef __STACK_H__
#define __STACK_H__
typedef struct StackElement_
{
int value;
struct StackElement_ *next;
} StackElement, *Stack;
/*----------------------Prototypes----------------------*/
Stack new_stack();
void push_to_stack(Stack *, int);
int pop_stack(Stack *);
int peek_stack(Stack);
int stack_length(Stack);
void print_stack(Stack);
Bool is_empty_stack(Stack);
void clear_stack(Stack);
/*------------------------------------------------------*/
#endif
and utils.h
#ifndef __UTILS_H__
#define __UTILS_H__
#define INT_MIN -2147483648
/*------------------------Typedef-----------------------*/
typedef enum Boolean_ {
FALSE,
TRUE
} Bool;
/*------------------------------------------------------*/
#endif
In stack.h, I need to know Bool but when I include utils.h in stack.c, the structure is still not known in stack.h. How to do this without having to define it directly in stack.h?
You need to include utils.h in stack .h (not stack.c). The #include statements are part of the C macro language, which is a simple pre-processor. It literally takes the file given as the filename (#include filename) and inserts it into your program (during the pre-processor stage of the compile).
A source file or include file should include all files necessary.
So if stack.h is dependent on declarations in util.h, then you should #include "util.h" in stack.h. That way, any module that includes stack.h doesn't need to worry about also including util.h and putting it in the right order.
If a module decides to include both that should be fine as well. The include guards you've added should address that.
You didn't show the source of stack.c, but you most likely included stack.h before util.h. That would explain your issue since the definitions needed by stack.h appear after them instead of before.

Proper way to declare and use structures in C project?

I am building a project that I am trying to organize as follows:
main.c
globals.h
structures.h
FunctionSet1.c, FunctionSet1.h
FunctionSet2.c, FunctionSet2.h
etc.
I thought I could define a structure type in structures.h:
struct type_struct1 {int a,b;}; // define type 'struct type_struct1'
then declare a function1() returning a structure of type type_struct1 in FunctionSet1.h:
#include "structures.h"
struct type_struct1 function1(); // declare function1() that returns a type 'struct type_struct1'
then write function1() in FunctionSet1.c:
#include "FunctionSet1.h"
struct type_struct1 function1() {
struct type_struct1 struct1; // declare struct1 as type 'struct type_struct1'
struct1.a=1;
struct1.b=2;
return struct1;
}
Edit: with the corrected code above, the compiler returns
306 'struct' tag redefined 'type_struct1' structures.h
Is the file set good practice ?
What is the good practice to manage the structures ?
In your example, you declare a structure named type_struct in structure.h, then in FunctionSet1.h the structure that you are returning is type_struct, and in the .c it is called struct1.
So i think that the problem is that struct1 and type_struct are not recognized because they have never been defined ...
However, the organization of your files is fine.
Your general structure looks good. One thing you need to do, as zenith mentioned, is to put include guards into your header files. What that is is a set of #define's that make sure that the contents of the header are not included more that once in a given file. For example:
structures.h:
#ifndef STRUCTURES_H
#define STRUCTURES_H
struct type_struct1{
int a,b;
};
...
// more structs
...
#endif
FunctionSet1.h:
#ifndef FUNCTION_SET_1_H
#define FUNCTION_SET_1_H
#include "structures.h"
struct type_struct1 function1();
...
// more functions in FucntionSet1.c
...
#endif
main.c:
#inlcude <stdio.h>
#include "structures.h"
#include "FunctionSet1.h"
int main(void)
{
struct type_struct1 struct1;
struct1 = function1();
return 0;
}
Here, main.c includes structures.h and FunctionSet1.h, but FunctionSet1.h also includes structures.h. Without the include guards, the contents of structures.h would appear twice in the resulting file after the preprocesser is done. This is probably why you're getting the "tag redefined" error.
The include guards prevent these type of errors from happening. Then you don't have to worry about whether or not a particular header file was included or not. This is particularly important if you're writing a library, where other users may not know the relationship between your header files.
First of all, you have to declare the structure in your file.h (you can use typedef to create an alias)
typedef struct Books
{
char title[50];
int book_id;
} books;
then, you have to include your file.h in your file.c and declare your variable like this
#include "file.h"
int main()
{
books book1;
book1.title = "Harry Potter";
book1.book_id = 54;
}
or like this if you didn't use typedef
#include "file.h"
int main()
{
struct Books book1;
book1.title = "Harry Potter";
book1.book_id = 54;
}
Thank you all.
I read again what you said and found that the code above is now correct.
The error I report is with testing the following main.c
#include "structures.h"
#include "FunctionSet1.h"
void main() {
struct type_struct1 struct2;
struct2=function1();
}
in which structures.h is included again, thus causing the error. Removing the include eliminates the error.
I will now look into header guards to avoid such problems.
Thanks again.

Why does my C project compilation cannot find my structure?

I had a project based on a single C file that I try to rearrange for further development in several .c and .h files.
My main is organised as follow:
// General includes
typedef struct
{
} MyStruct;
#include "MyInclude.h"
// Rest of the code
My file "MyInclude.c" is organised as follow:
#include "MyInclude.h"
// Defines
// Functions that need to know MyStruct
There is something I don't understant about the compilation process of GCC. In fact, I got the error "MyStruct undeclared (first use in this function)" and I don't why as I put my include after the typedef declaration of my structure.
Does someone knows why it happens?
The question is a bit unclear.
The file "MyInclude.c" can access only to your H file.
While your struct is written in another C file.
You can solve it by:
Define the struct on the H file "MyInclude.h". It will work, but methodologically it's wrong.
Define setters and getters to access your struct
Cheers
Your file.h :
// file.h
#include <stdio.h> //Juste for printf
typedef struct s_data
{
char c;
} t_data;
Your file.c :
#include "file.h"
int main()
{
t_data data;
data.c = 'a';
printf("%c", data.c);
return (0);
}
Compil (if your file .c and .h are in the same directory):
gcc file.c -o my_app -I .

External Structs and Multiple Headers

I have a confusion about external structs. I have to define a global instance of the struct in a file other than which the struct is defined (as a RAM variable which I do not know what it is exactly).
Below is an example that GCC can compile and it runs correct while Code Composer Studio gives compile-time errors.
I want to learn where the problem is, how GCC can compile, and where/how I should use the extern declaration.
Any comment would be appreciated.
person.h
#ifndef PERSON_H
#define PERSON_H
struct person {
int age;
};
typedef struct person PERSON;
void modifyPerson();
#endif // PERSON_H
personRam.h
#ifndef PERSONRAM_H
#define PERSONRAM_H
#include "person.h"
PERSON p1;
#endif // PERSONRAM_H
modifyPerson.c
#include "person.h"
#include "personRam.h"
void modifyPerson() {
p1.age = 10;
}
main.c
#include <stdio.h>
#include "person.h"
#include "personRam.h"
int main() {
modifyPerson();
printf("%d", p1.age);
return 0;
}
You should not get a compiler error, but a linker error, saying that p1 is defined multiple times. At least that's what I guess is the problem.
The reason is that you define the variable in a header file, which you then include in multiple source files. This means that the definition is in both source files (The preprocessor #include directive literally puts the contents of the header file in place of the include "statement").
If you declare the variable as extern in the header file, and define it in one source file it should work.
So in personRam.h
extern PERSON p1;
And in one of the source files:
PERSON p1;
On which operating system are you compiling, and for which target system?
For what it is worth, Linux (& Unix-es) and Windows have different linking semantics.
Read Levine's "Linkers & Loaders" book for details.

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