Hi,

I am trying to do meet these 2 requirements:
1. create a global file containing global variables that can be accessed from multiple places. Need this for multithreading. something like global.c/h to contain global data in particular the mutexes.

2. some list modules that is required from different other modules. something like list.c. there are some global list included in the global.c.

So I did this:

global.c
#include "list.c"

util.c
#include "global.c"
// need to access the global data structures defined in global.c
// also methods from list.c to perform insert/delete on global lists //defined in global.c

client_handler.c
#include "global.c"
#include "util.c"
// need to access global data structures
// need also access functions defined in list.c to perform insert/delete
// need also access utility functions defined in util.c

Now I have tried to do this. Hoping that util.c includes global.h and list.c. So including util.c in client_handler.c would solve the "redefinition" (with which the compiler is badgering for a long time) would be gone. But nothing good happens except for the shouting from the compiler.

global.c
#include "list.c"

util.c
#include "global.c"
// hoping that list.c is already included in global.c

client_handler.c
#util.c

Besides is there any elegant way to do this one place? Because managing header files seem to be a big pain in the a$$.

Thanks.

The header contains declarations and is included in multiple places because declarations can be duplicated. Then you have one implementation file that has the definitions:

/* globals.h */
extern int glob;
extern int foo ( void );
/* globals.c */
#include "globals.h"

int glob = 0;

int foo ( void )
{
  return 1;
}
/* main.c */
#include <stdio.h>
#include "globals.h"

int main ( void )
{
  printf ( "glob: %d\nfoo: %d\n", glob, foo() );
  return 0;
}

It's not recommended that you include *.c files because they usually contain definitions as well as declarations, and you'll likely get multiple definition errors.

I dont know how it worked but I did this and (like a magic) it worked

globals.h

#ifndef _global_h_included_
  5 #define _global_h_included_
  6 
  7 #define NUM_INPUTS 1000
  8 #define INPUT_MAX 500
  9 
 10 list request_queue;
 11 pthread_mutex_t mutex_request_queue;
 12 pthread_cond_t cond_request_queue_full;
 13 pthread_cond_t cond_request_queue_empty;
 14 pthread_mutex_t mutex_global_data;
 15 int sum_client_speed;
 16 list input_list;
 17 int global_average;
 18 
 19 #endif

Now I can include the same header file as many times as I want. But I am not sure how is it working correctly. I would appreciate greatly if some one knowledgeable can explain this to me.

Thanks.

The first time the compiler processes the header, _global_h_included_ is undefined so the header is processed. During this processing, _global_h_included_ is defined.

Each subsequent time the header is processed, _global_h_included_ is already defined, so the rest of the header is skipped thanks to the #ifndef

The first time the compiler processes the header, _global_h_included_ is undefined so the header is processed. During this processing, _global_h_included_ is defined.

Each subsequent time the header is processed, _global_h_included_ is already defined, so the rest of the header is skipped thanks to the #ifndef

thanks for ur explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.