-1

I have some functionality which is highlighted in a separate file functionality.c. The code in this file reads thresholds which are in functionality.h:

unsigned int thresold1[2];
unsigned int thresold2[2];
void *watcher(void *);

Also I have main.c file, where I'm trying to configure these thresholds:

#include "functionality.h"

int main(void) {
    /* ... */
    int ret;
    pthread_t watcher_thread;
    thresold1[0] = 10;
    thresold1[1] = 50;
    ret = pthread_create(&watcher_thread, NULL, watcher, NULL);
    if (ret) {
        /* ... */
    }
    /* ... */
}

But when I'm trying to access these thresholds in watcher() from functionality.c all these array values are zeroed, i.e. undefined. Where am I wrong?

P.S. functionality.h is also included to functionality.c

UPD: I compile it such a way:

gcc -pthread main.c functionality.c -o main
NK-cell
  • 1,145
  • 6
  • 19
  • Does this answer your question? [How do I use extern to share variables between source files?](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files) – red0ct Oct 02 '21 at 20:30

1 Answers1

4

Variable should be declared in the header file as:

extern unsigned int thresold1[2];
extern unsigned int thresold2[2];

and defined in a unique point (.c file) as:

unsigned int thresold1[2];
unsigned int thresold2[2];
Carlo Banfi
  • 186
  • 1
  • 7