I am writing a kernel module in which i have 3 source files and one header file. When I compile it, it shows me the following error:
/home/karan/project/proc.o: In function `proc_read':
/home/karan/project/proc.c:23: multiple definition of `info'
/home/karan/project/main.o:/home/karan/project/main.c:23: first defined here
/home/karan/project/tx_pkt.o: In function `tx_packet':
/home/karan/project/tx_pkt.c:9: multiple definition of `info'
/home/karan/project/main.o:/home/karan/project/main.c:23: first defined here
I think the problem is the compiler is taking definition struct info more then once. But what is the solution? The struct info is declared in the header file . The header file is as follow:
int proc_write(struct file *filp,const char *buffer,unsigned long count,void *data);
int proc_read(char *buffer,char **buffer_location,off_t offset,int buffer_length,int *eof,void *data);
void tx_packet(void);
#ifndef MYDEFS_H
#define MYDEFS_H
struct inform
{
char tx_buffer[100];
struct iphdr *ip1;
};
extern struct inform info;
#endif
Use : header guards
In your header do this:
#ifndef MYDEFS_H
#define MYDEFS_H
struct info {
...
...
};
#endif
I think You added the main .c file in ($(USERAPPS): $(USERSP) $(CC) -o $# ) here,
U include all c file except main file here
Related
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.
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 .
So I made a program that uses four differrent processes to do some stuff. I works perfectly, but now I want to split it up so each of the processes has its own .c file. I tried to do this using a header file "processes.h" that has the function prototypes for all the processes. It looks like this:
#ifndef PROCESSES_H_
#define PROCESSES_H_
/*------------------------------------------------------------------------------
definitions (defines, typedefs, ...)
------------------------------------------------------------------------------*/
#define NR_OF_PROCESSES 4
enum errCode {
ERR_NONE = 0, ERR_SYNTAX, ERR_OPEN, ERR_TOKEN, ERR_ARG, ERR_END
};
typedef enum errCode ErrCode;
enum toktype {
NUMBER = 0, OPERATOR, ENDOFLINE, ENDOFFILE
};
typedef enum toktype Token;
/*------------------------------------------------------------------------------
function prototypes
------------------------------------------------------------------------------*/
void readProcess(int pfds[5][2]);
void tokenProcess(int pfds[5][2]);
void calculatorProcess(int pfds[5][2]);
void errorProcess(int pfds[5][2]);
/*------------------------------------------------------------------------------
global variable declarations
------------------------------------------------------------------------------*/
void (*functionTable[NR_OF_PROCESSES]) (int pfds[5][2]) = {
readProcess,
tokenProcess,
calculatorProcess,
errorProcess
};
#endif /*PROCESSES_H_*/
All the .c files which contain the implementation of the function #include "processes.h", and the main function (which uses the functionTable to set up the processes) also includes processes.h.
When I try to compile I get the error:
ld: duplicate symbol _functionTable in /var/folders/eH/eHF8LmdvHzSsNgT01V3jyk+++TI/-Tmp-//ccDgTW2X.o and /var/folders/eH/eHF8LmdvHzSsNgT01V3jyk+++TI/-Tmp-//ccp7zO9L.o
collect2: ld returned 1 exit status
Is this the right way of doing the splitting up into different files? Or do I need a separate .h file for each .c file?
Put this in your .h:
extern void *functionTable;
Then put this in exactly one file, e.g. the source file with "main()":
#include "processes.h"
...
void (*functionTable[NR_OF_PROCESSES]) (int pfds[5][2]) = {
readProcess,
tokenProcess,
calculatorProcess,
errorProcess
};
One way to do this is to put
#ifdef GENERATE_DEFINITIONS
#define EXTERN
#define INITIALIZE(x) = x
#else
#define EXTERN extern
#define INITIALIZE(x)
#endif
EXTERN void (*functionTable[NR_OF_PROCESSES]) (int pfds[5][2])
INITIALIZE({
readProcess,
tokenProcess,
calculatorProcess,
errorProcess
});
in processes.h and then put
#define GENERATE_DEFINITIONS
before the
#include "processes.h"
in just one of the source files (e.g., the one containing main()).
How do you access a struct say struct common, that is defined in a header file, in a .c file? We'll include the header file in the .c file and then can we directly use
struct common C;?
header file-new_algo.h
#ifndef NEW_ALGO_H_
#define NEW_ALGO_H_
#endif /* NEW_ALGO_H_ */
struct common{
float count;
//other variables
};
main.c
#include "new_algo.h"
int main()
{
typedef struct common myStruct;
myStruct* S;
S->count = 0;//when I do this segmentation fault occurs
//this is the error I get in eclipse
/* Thread [1] 0 (Suspended : Signal : SIGSEGV:Segmentation fault)
main() at E:/Namratha//trial/.settings/..\\src\\main.c:44 0x401443*/
}
Hey, the fault error is not resulted from access control.
Before use struct varible, you should malloc space for it like
myStrcut *s = (myStrcut *)malloc(sizeof(myStruct))
then assign:
s->count = 0
Please have a try.
Yes, it's correct. Just include the header in the source file and create the instance for the struct.
header.h
struct common
{
// ...
};
.c
#include "header.h"
struct common C ;
I am using Linux as my programming platform and C language as my programming language.
My problem is, I define a structure in my main source file( main.c):
struct test_st
{
int state;
int status;
};
So I want this structure to use in my other source file(e.g. othersrc.). Is it possible to use this structure in another source file without putting this structure in a header?
You can define the struct in each source file, then declare the instance variable once as a global, and once as an extern:
// File1.c
struct test_st
{
int state;
int status;
};
struct test_st g_test;
// File2.c
struct test_st
{
int state;
int status;
};
extern struct test_st g_test;
The linker will then do the magic, both source file will point to the same variable.
However, duplicating a definition in multiple source files is a bad coding practice, because in case of changes you have to manually change each definition.
The easy solution is to put the definition in an header file, and then include it in all the source file that use the structure. To access the same instance of the struct across the source files, you can still use the extern method.
// Definition.h
struct test_st
{
int state;
int status;
};
// File1.c
#include "Definition.h"
struct test_st g_test;
// File2.c
#include "Definition.h"
extern struct test_st g_test;
You can use pointers to it in othersrc.c without including it:
othersrc.c:
struct foo
{
struct test_st *p;
};
but otherwise you need to somehow include the structure definition. A good way is to define it in main.h, and include that in both .c files.
main.h:
struct test_st
{
int state;
int status;
};
main.c:
#include "main.h"
othersrc.c:
#include "main.h"
Of course, you can probably find a better name than main.h
// use a header file. It's the right thing to do. Why not learn correctly?
//in a "defines.h" file:
//----------------------
typedef struct
{
int state;
int status;
} TEST_ST;
//in your main.cpp file:
//----------------------
#include "defines.h"
TEST_ST test_st;
test_st.state = 1;
test_st.status = 2;
//in your other.ccp file:
#include "defines.h"
extern TEST_ST test_st;
printf ("Struct == %d, %d\n", test_st.state, test_st.status);
Putting it in a header file is the normal, correct way to declare types shared between source files.
Barring that, you can treat main.c as a header file and include it in the other file, then only compile the other file. Or you can declare the same struct in both files and leave a note to yourself to change it in both places.
C supports separate compilation.
Put the structure declaration in a header file and #include "..." it in the source files.
It is perfectly reasonable to be inclusive with structs by leaving them in the source file instead. This is encapsulation. However if you're going to redefine struct multiple times in multiple source files then you might as well define the struct once in a header file instead and include that file as necessary.
Header file /* include this header file in both file1.c and file2.c
struct a {
};
struct b {
};
so header file included the declaration of both structures .
file 1.c
struct a xyz[10]; --> struct a defined here
to use struct b here in this file
extern struct b abc[20];
/* now can use in this file */
file2.c
struct b abc[20]; /* defined here */
to use struct a defined in file1.c
use extern struct a xyz[10]