0

I have a header file where i keep all my global variables without assigning a value and i include it in main code like this. I need to access a variable declared in Globals.h from Receiver.c, when i use any variable from Globals.h in Receiver.c, it Eclipse says Unknown Type Name

As per compiler, if i call Globals.h once, its in compiler memory right?

i dont include anything in Receiver.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "Globals.h"
#include "my_queue.h"
#include "Receiver.h"
#include "Receiver.c"

This is the contents of Globals.h

int stop_nw_global;
int                   datalen;
int cont[210];
struct in_addr        localInterface;
struct sockaddr_in    groupSock;
int                   sd;
char                  databuf[1024];


typedef struct UDP_Packet {
    unsigned short Year;          // year
    unsigned char  Month;         // months
    unsigned char  Day;           // day
    unsigned char  Hour;          // hour
    unsigned char  Minute;        // minute
    unsigned char  Seconds;       // seconds
    unsigned short Milliseconds;  // milliseconds
    unsigned char  SeqNo;         // packet sequence no
    unsigned short CommandCode;   // packet type
    unsigned char  DestSubSysID;  // Destination sub system id (0 to 255)
    unsigned char  DestNodeID;    // Destination node id
    unsigned char  SrcSubSysID;   // Source sub system id
    unsigned char  SrcNodeID;     // Source node id
    unsigned short DataSize;      // Data size in bytes
    unsigned char  AckSel;        // select acknowledgment option
    unsigned char  AckID;         // ID for ACK
    unsigned char  DataFlag;      // Flag indicating single part(0) or multipart data (1)
    unsigned char  MessageID;     // unique message ID
} UDP_Packet;

But when i try to add Globals.h in receive as well as main, it says, multiple definition of mostly all variable

src\udpexx1.o:/cygdrive/c/Eclipse Projects/udpexx1/Debug/C:\Eclipse Projects\udpexx1\SocketAPI/Globals.h:3: multiple definition of `stop_nw_global'; SocketAPI\Receiver.o:/cygdrive/c/Eclipse Projects/udpexx1/Debug/C:\Eclipse Projects\udpexx1\SocketAPI/Globals.h:3: first defined here

but both are pointing to Globals.h

  • You should give an example (at the very least) of the declaration of an error-causing variable (in Globals.h) and an example of the variable usage (in Receiver.c). You have given us very little to go on. – SGeorgiades Jun 01 '21 at 03:56
  • 1
    _if i call Globals.h once, its in compiler memory right?_ No. First of all, you're not "calling" anything. `#include` is a preprocessor directive. All that does is substitute the text of that file where the `#include` line is. If you're referencing a variable the compiler can't find in that [translation unit](https://en.wikipedia.org/wiki/Translation_unit_(programming)), then you'll get the error. Not sure what you're showing above, but if Receiver.c needs a global in Globals.h, then `#include "Globals.h"` in Receiver.c. Note, it's not common to `#include` source files. – yano Jun 01 '21 at 04:04
  • @SGeorgiades I have updated contents of Globals.h as edit –  Jun 01 '21 at 04:23
  • @yano im trying to do that, but it says multiply defined , and im not assigning any value to variable in a .h file –  Jun 01 '21 at 04:24
  • You should add [header include guards](https://en.wikipedia.org/wiki/Include_guard) to your .h files, that could be a likely source of your error (see [this question](https://stackoverflow.com/questions/27810115/what-exactly-do-c-include-guards-do) as well). For example, if my_queue.h and Receiver.h both `#include "Globals.h"`, then you have multiple definitions of `stop_mw_global` (and everything else in Globals.h). Include guards will prevent that. But without a [mre] I don't think there's enough information here to give you an exact answer. – yano Jun 01 '21 at 05:19

1 Answers1

0

It's nearly always a mistake to use global variables. You need some real good arguments for using global variables in your code. So reconsider your design.

If you really need globals, don't put variable definitions in a header file. They belong in C files. Then in the header file you do an extern declaration. Do it like this.

somefile.c

int stop_nw_global;  // Define the variable in a c-file

somefile.h

#ifndef SOMEFILE_H   // Include guard
#define SOMEFILE_H

extern int stop_nw_global; // Declare the variable, i.e. tell other c-files
                           // that the variable exists

#endif

otherfile.c

#include somefile.h

...
stop_nw_global = 42;   // Use the variable
...

yetanotherfile.c

#include somefile.h

...
stop_nw_global = -1;   // Use the variable
...

note It's fine to have the typedef in a header file. It's a type definition (not a variable definition) so that's ok

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63