LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   extern static variable (https://www.linuxquestions.org/questions/programming-9/extern-static-variable-360414/)

alaios 09-05-2005 10:08 AM

extern static variable
 
Hi i have a problem using the extern definition with a struct
The struct is defined as following

from file diffServLinux.h
struct diffstat{
unsigned long int ifindex;
unsigned long int ifdirection;
char name[20];
unsigned long int level;
unsigned long int handle_major;
unsigned long int handle_minor;
..........
};

File monitor.c /*Strores values in the variables */
#include "diffServLinux.h"
struct diffstat ds_stats[10];
ds_stats[1].ifindex=20;

File logical.c /*Wants to see the values in the ds_stats struct */
I have tried using the extern statement
extern struct diffstat ds_stats[10];

I compile it using
gcc -o programme monitor.c logical.c
but this returns the error
logical.c: At top level:
logical.c:2: error: storage size of `ds_stats' isn't known

The compiler says that perhaps cant understand the internal structure of the ds_stats
I have tried also to include the diffServLinux.h in the logical.c
but then the compiler starts complaining about a bunch of redefinitions

Dont know what to do now... Plz help

Hivemind 09-05-2005 10:25 AM

diffServLinux.h:
Code:

#ifndef DIFFSERVLINUX_H
#define DIFFSERVLINUX_H

struct diffstat
{
unsigned long int ifindex;
unsigned long int ifdirection;
char name[20];
unsigned long int level;
unsigned long int handle_major;
unsigned long int handle_minor;
};

#endif /* #ifndef DIFFSERVLINUX_H */

globals.h:
Code:

#ifndef GLOBALS_H
#define GLOBALS_H

#include "diffServLinux.h"

extern struct diffstat foo[10];

#endif /* #ifndef GLOBALS_H */

globals.c:
Code:

#include "globals.h"

struct diffstat foo[10];

main.c
Code:

#include <string.h> /* memset() */
#include <stdio.h>
#include "globals.h"

static
void bar(void)
{
  foo[1].level = 4711;
}

int
main(void)
{
  memset(foo, 0, sizeof(foo));
 
  bar();

  printf("%li\n", foo[1].level);

  return 0;
}

Makefile
Code:

CC = gcc
CFLAGS = -Wall -W -ansi -pedantic -g -O0 -c -o
LDFLAGS = -o $(EXEC)
EXEC = globalstructarraytest.exe
OBJECTS = globals.o main.o

all: $(OBJECTS)
        $(CC) $^ $(LDFLAGS)

%.o: %.c
        $(CC) $(CFLAGS) $@ $<

clean:
        rm -f $(OBJECTS) $(EXEC) *~ *.stackdump

Now there's a complete example on how you declare a global array of some struct where each struct (each element) can be changed during runtime and all translation units (read: files) that includes the header file globals.h will see the same array and not get its own instance of it.

alaios 09-05-2005 10:28 AM

Thx a lot cool :)
THHHXXX

alaios 09-05-2005 10:28 AM

1 more question.. Where have u learned all this stuff?

Hivemind 09-05-2005 10:54 AM

Reading a lot of books (not web tutorials, but good books), asking on/reading the programming groups (comp.lang.c, comp.lang.c++ mostly, but also groups dealing with a specific platform) on usenet. Later in life I've also taken courses in programming at the university. I started with C++ ten years ago, but the first five years or so I had long periods when I did no programming at all (unfortunately).

alaios 09-05-2005 11:43 AM

What do u mean by programming groups....
I am looking for a place that i can ask about c... Sometimes i think that this place isnt the most appropriate ... (linux forums)

Hivemind 09-05-2005 12:10 PM

Well, it seems you need to read up on what usenet is. If you google for usenet you should find a lot of information. Many ISP:s (Internet Service Providers) have a usenet server. You can configure most mail clients to allow you to read (and post on) usenet groups if your ISP has a news server. There are also programs that are exclusively for reading newsgroups. If your ISP doesn't have a news server, google provides a http-based access to it that's free. Before, google groups lagged behind several hours so it was inconvenient to use, but that's not the case today.

Head over to: http://groups.google.com/group/comp.lang.c


Keep in mind, though, that comp.lang.c only deals with the C language. Anything that's platform specific is off-topic for the group. But there are groups for developing on linux and other operating systems. If your question is pure C (like the one in this thread), ask in comp.lang.c, if it concerns libraries, api:s etc that's not part of the core language but provided by a specific operating system, ask in a group/forum dedicated to developing on that platform.

paulsm4 09-05-2005 01:30 PM

Hi -

In addition to the Stevens book, I'd also get a copy of Kernighan and Ritchie. It's the single best reference for these kinds of questions in the entire world. It's also one of the best "computer books" ever written. No kidding! And you can probably buy a copy on Amazon for around $20.00:

Kernighan & Ritchie: The C Programming Language:
http://www.amazon.com/exec/obidos/tg...=UTF8&v=glance

Unix Network Programming, 3rd Edition:
http://www.amazon.com/exec/obidos/tg...glance&s=books

alaios 09-10-2005 07:10 PM

Thx hivemind about your second reply... I have subscribed to google groups for c language.. but still i cant find any subgroup for linux c ...

Hivemind 09-11-2005 04:11 AM

Maybe comp.unix.programmer (many API:s and libraries work the same across unix and linux) and comp.os.linux.development.apps? I'm sure there are others. linuxquestions is fine for platform specific questions, but if you really need help from C experts comp.lang.c is the best.


All times are GMT -5. The time now is 08:09 PM.