I have 3 *.c files (file1.c, file2.c and file3.c) and 1 *.h file (file3.h) in a project (Visual Studio).
/*******************************
file3.h
********************************/
#ifndef FILE3_H
#define FILE3_H
int gintVariable = 400;
#endif
/*******************************
file1.c
********************************/
#include "file3.h"
#include <stdio.h>
#include <conio.h>
int modifyGlobalVariable(void);
void printGlobalVariable(void);
int main(void)
{
modifyGlobalVariable();
printGlobalVariable();
printf("Global variable: %d\n", gintVariable++);
getch();
return 0;
}
/*******************************
file2.c
********************************/
#include "file3.h"
int modifyGlobalVariable(void)
{
return gintVariable++;
}
/*******************************
file3.c
********************************/
#include "file3.h"
#include <stdio.h>
void printGlobalVariable(void)
{
printf("Global: %d\n", gintVariable++);
}
When I build the solution in VS, it is giving error as "_gintVariable already defined in file1.obj".
I did check in the pre-processor output, the gintVariable is included in all the *.c files even though I have included include guards.
What mistake I am doing?
You should use 'extern' while declaring a global variable in header file.
Define it in any one of *.c file.
This should fix the issue.
For more on header files, read
How do I use extern to share variables between source files?
Including guards prevents multiple inclusion (or, more precisely, multiple compilation of the .h file content) in a single translation unit.
It is useful against this problem:
/* glob.h */
#ifndef H_GLOB
#define H_GLOB
struct s { int i; };
#endif
/* f.h */
#ifndef H_F
#define H_F
#include "glob.h"
struct s f(void);
#endif
/* g.h */
#ifndef H_G
#define H_G
#include "glob.h"
struct s g(void);
#endif
/* c.c */
#include "f.h" /* includes "glob.h" */
#include "g.h" /* includes "glob.h" */
void c(void) {
struct s s1 = f();
struct s s2 = g();
}
The inclusions is like a diamond:
glob.h
/ \
f.h g.h
\ /
c.c
Related
I'm trying to do this code, and I split it up into .c files (lets say file1.c and file2.c) and file1.h file. I'm not allowed to change which parameters I can send to the function, so I need to find another way to "send"/access another variable. I tried to make the variable static in the header file file1.h, and include it in the file2.c. The function in file1.c look something like this:
int function(int *array, int a, int b){
...
...
if(global_variable == 1){
point = array[(a+b)/2];
}else if(global_variable == 0){
point = array[b];
}
and in the file2.c I have a function something like this:
double function2(t_sort_funcp fun, const case_t c, int array_length, result_t *buf, t_generate_array_funcp g_array){
int array[array_length];
switch (c)
{
case first:
global_variable = 1;
g_array(array, array_length);
return debugg(fun, array, array_length);
break;
case second:// Wors case is an inverted sorted array.
global_variable = 0;
g_array(array, array_length);
return debugg(fun, array, array_length);
break;
case third:
global_variable = 1;
g_array(array, array_length);
return debugg(fun, array, array_length);
break;
}
return 0;
}
In the file1.h I have:
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <stdbool.h> // bool
static int global_variable;
#endif
as you can see, I'm trying to change the global_variable variable in file2.c and use it in file1.c but that does not work, the if-statement in file1.c always executes the code in the else-statement, even if I changed the variable to 1.
NOTE: file2.c always executes before file1.c
Do it the opposite way
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <stdbool.h> // bool
extern int global_variable;
#endif
In one of the .c files
int global_variable;
Include the .h file in all files which require access to this variable.
static in global scope makes the variable only available in one compilation unit (file).
You can use extern with a cpp conditional.
Below are samples files.
FILE: file1.h
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <stdbool.h> // bool
#ifdef FILE1_C
int global_variable;
#else
extern int global_variable;
#endif
#endif
FILE: file1.c
// I am file1.c
#define FILE1_C
#include "file1.h"
FILE: file2.c
// I am file2.c
#include "file1.h"
UPDATE:
definitely wrong way. Why this weird ifdef complication –
0___________
No, it's not the wrong way. I do this all the time. The idea is that you have both definitions in the same place/file. If you put the global in a .c file, it's harder to see if you change the type.
Here's a more expanded case:
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <stdbool.h> // bool
#ifdef FILE1_C
#define EXTRN_FILE1 /**/
#else
#define EXTRN_FILE1 extern
#endif
EXTRN_FILE1 int global_variable;
EXTRN_FILE1 double global_variable_2;
#ifdef FILE1_C
int global_variable_3 = 37;
#else
extern int global_variable_3;
#endif
#endif
And, we may want to put the globals in a different .c file. If we rename FILE1_C to (e.g.) DEFINE_GLOBALS, we can put #define DEFINE_GLOBALS in any .c. If we change our minds later, it's trivial to move the #define from (e.g.) file1.c to file2.c.
Can anyone explain how to create a header file in C with a simple example from beginning to end.
foo.h
#ifndef FOO_H_ /* Include guard */
#define FOO_H_
int foo(int x); /* An example function declaration */
#endif // FOO_H_
foo.c
#include "foo.h" /* Include the header (not strictly necessary here) */
int foo(int x) /* Function definition */
{
return x + 5;
}
main.c
#include <stdio.h>
#include "foo.h" /* Include the header here, to obtain the function declaration */
int main(void)
{
int y = foo(3); /* Use the function here */
printf("%d\n", y);
return 0;
}
To compile using GCC
gcc -o my_app main.c foo.c
#ifndef MY_HEADER_H
# define MY_HEADER_H
//put your function headers here
#endif
MY_HEADER_H serves as a double-inclusion guard.
For the function declaration, you only need to define the signature, that is, without parameter names, like this:
int foo(char*);
If you really want to, you can also include the parameter's identifier, but it's not necessary because the identifier would only be used in a function's body (implementation), which in case of a header (parameter signature), it's missing.
This declares the function foo which accepts a char* and returns an int.
In your source file, you would have:
#include "my_header.h"
int foo(char* name) {
//do stuff
return 0;
}
myfile.h
#ifndef _myfile_h
#define _myfile_h
void function();
#endif
myfile.c
#include "myfile.h"
void function() {
}
header files contain prototypes for functions you define in a .c or .cpp/.cxx file (depending if you're using c or c++). You want to place #ifndef/#defines around your .h code so that if you include the same .h twice in different parts of your programs, the prototypes are only included once.
client.h
#ifndef CLIENT_H
#define CLIENT_H
short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize);
#endif /** CLIENT_H */
Then you'd implement the .h in a .c file like so:
client.c
#include "client.h"
short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize) {
short ret = -1;
//some implementation here
return ret;
}
I have the following source named lcd.c.
#include <stdio.h>
#include "lcd.h"
void print_mode(void)
{
printf("%d\n",LCD_MODE);
}
The header lcd.h contains the definition for LCD_MODE as follows.
#ifndef LCD_H
#define LCD_H
#include "util.h"
#ifndef LCD_MODE
#define LCD_MODE LCD_MODE_8BIT
#endif
void print_mode(void);
#endif /* LCD_H */
The file util.h contains
#ifndef UTIL_H
#define UTIL_H
#define LCD_MODE_8BIT 1
#define LCD_MODE_4BIT 0
#endif /* UTIL_H */
lcd.c will be compiled separately as part of some library. I want to use it with an application main.c as follows.
#include "util.h"
#define LCD_MODE LCD_MODE_4BIT
#include "lcd.h"
int main(void)
{
print_mode();
return 0;
}
The desired outcome is to print 0 as per the definition of LCD_MODE_4BIT in main.c. However, 1 is printed because the header file sees that LCD_MODE is not defined during the preprocessing for lcd.c. How should I go about passing the LCD_MODE option to print_mode() through the main application?
if you cannot recompile lcd.c you cannot use a macro in another source file, because lcd.o already has the value hardcoded.
You could create a static variable (which defaults to LCD_MODE) that you can change using a setter:
#include <stdio.h>
#include "lcd.h"
static int the_mode = LCD_MODE;
void print_mode(void)
{
printf("%d\n",the_mode);
}
void set_mode(int new_mode)
{
the_mode = new_mode;
}
lcd.h should contain the prototype for the new configuration function BTW:
void set_mode(int new_mode);
then in your main, you can:
set_mode(LCD_MODE);
(or drop that LCD_MODE macro everywhere because it solves nothing and adds to the confusion)
display.h
#ifndef PRO_DISPLAY_H
#define PRO_DISPLAY_H
/** Initializes the display **/
int pro_display_init(void);
#endif /* PRO_DISPLAY_H */
display.c
#include "main.h"
static int height_ = 300;
static int width_ = 300;
static int bpp_ = 16;
static SDL_Surface* screen_ = NULL;
int pro_display_init(void)
{
screen_ = SDL_SetVideoMode(width_, height_, bpp_, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen_)
{
pro_sdl_error("Video initialization failed.");
return 0;
}
return 1;
}
main.h
#ifndef PRO_MAIN_H
#define PRO_MAIN_H
// standard headers
#include <stdlib.h>
#include <stdlib.h>
// conditional headers
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#endif
// our own headers
#include "scripter.h"
#include "ttf_util.h"
#include "events.h"
#include "display.h"
// some macros
#define pro_error(...) fprintf(stderr, __VA_ARGS__)
#define pro_sdl_error(x) fprintf(stderr, "%s. \n=> %s\n", x, SDL_GetError())
#define pro_ttf_error(x) fprintf(stderr, "%s. \n=> %s\n", x, TTF_GetError())
#endif /* PRO_MAIN_H */
** main.c**
#include "main.h"
int main(int argc, char* argv[])
{
pro_display_init();
return 0;
}
The Error:
main.c|5|undefined reference to `pro_display_init()'|
Checked the build process. Made sure I was adding "display.c" to gcc's input files. I'm at my wit's end. Why the error?
display.c and main.c are compiled into their own "translation unit". What happens is that when trying to resolve symbols name (i.e. looking for pro_display_init), the C compiler thinks it's compiling a standalone .c unit. The proper way to go is to compile them separately and then link them, e.g.
gcc -c display.c # creates display.o
gcc main.c display.o # compiles main.o and then link with display.o
Of course, you'll be creating/reusing a Makefile soon that lets you define rules for all this.
I think, #include "main.h" or #include "display.h" (in main.h) "finds" the wrong include file. Check you include_path variable.
Can anyone explain how to create a header file in C with a simple example from beginning to end.
foo.h
#ifndef FOO_H_ /* Include guard */
#define FOO_H_
int foo(int x); /* An example function declaration */
#endif // FOO_H_
foo.c
#include "foo.h" /* Include the header (not strictly necessary here) */
int foo(int x) /* Function definition */
{
return x + 5;
}
main.c
#include <stdio.h>
#include "foo.h" /* Include the header here, to obtain the function declaration */
int main(void)
{
int y = foo(3); /* Use the function here */
printf("%d\n", y);
return 0;
}
To compile using GCC
gcc -o my_app main.c foo.c
#ifndef MY_HEADER_H
# define MY_HEADER_H
//put your function headers here
#endif
MY_HEADER_H serves as a double-inclusion guard.
For the function declaration, you only need to define the signature, that is, without parameter names, like this:
int foo(char*);
If you really want to, you can also include the parameter's identifier, but it's not necessary because the identifier would only be used in a function's body (implementation), which in case of a header (parameter signature), it's missing.
This declares the function foo which accepts a char* and returns an int.
In your source file, you would have:
#include "my_header.h"
int foo(char* name) {
//do stuff
return 0;
}
myfile.h
#ifndef _myfile_h
#define _myfile_h
void function();
#endif
myfile.c
#include "myfile.h"
void function() {
}
header files contain prototypes for functions you define in a .c or .cpp/.cxx file (depending if you're using c or c++). You want to place #ifndef/#defines around your .h code so that if you include the same .h twice in different parts of your programs, the prototypes are only included once.
client.h
#ifndef CLIENT_H
#define CLIENT_H
short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize);
#endif /** CLIENT_H */
Then you'd implement the .h in a .c file like so:
client.c
#include "client.h"
short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize) {
short ret = -1;
//some implementation here
return ret;
}