0

I declared 2 termios structs in my header file aba.h:

extern struct termios cookedInput, rawInput;

And then in a function I tried to changed the values in stdin_prep.c like so:

tcgetattr(STDIN_FILENO, &cookedInput);
rawInput = cookedInput;
cfmakeraw(&rawInput);

gcc -Wall -Werror -Wextra *.c gives me the following errors:

In function stdin_change.c
stdin_change.c:(.text+0x26): undefined reference to 'rawInput'
stdin_change.c:(.text+0x55): undefined reference to 'cookedInput'

Those functions stdin_prep(); and stdin_change("raw"); are called in my main.c.

I've tried a few solutions from: Undefined reference to global variable during linking and C: undefined reference to a variable when using extern but got a bunch of different errors.

I've included a picture of my terminal. WSL-Ubuntu-18.04-Screenshot

ntruter42
  • 184
  • 1
  • 11

2 Answers2

3

Declaring an object doesn't cause it to exist. You need to actually define it. Put

struct termios rawInput;

optionally with an initializer, at top level (not inside any function) in exactly one of your .c files.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • How would I initialize them in my main.c? `rawInput = NULL;` and `rawInput = { 0 };` doesn't work. – ntruter42 Apr 21 '20 at 18:25
  • @ntruter42: Are you asking how to initialize them at compile time, or at run time? They'll automatically be initialized to zeros at compile time, even without specifying any initializer. If for some reason you need to zero them again at run time, you can use compound literals: `rawInput = (struct termios){0};` – Nate Eldredge Apr 21 '20 at 18:38
  • Compile time. The gcc -Werror flag reports a warning `unused variable 'rawInput'` and I would like to keep the flag. I am using the variable in a function `stdin_prep();` and another function `stdin_change("raw");`, both called in the main but -Werror flag does not recognize this. I define them in the main because they have to be globally available to all functions. Got any idea how I can use them just to silence the warning? – ntruter42 Apr 21 '20 at 19:22
  • I don't quite understand what you're doing. Can you post the complete code? Note that the definition `struct termios rawInput;`, like the definition of any other global variable, is supposed to be at top level (not inside any function definition, not even `main`). You should not get an unused variable warning for that – Nate Eldredge Apr 21 '20 at 19:24
  • I put the definition just on top of `int main(void) {...` and that solved the problem! Thanks. – ntruter42 Apr 21 '20 at 19:37
0

These

extern struct termios cookedInput, rawInput;

are forward declarations of two objects of the type struct termios but not their definitions.

You have to define the objects in some module.

For example you could define in the module with main.

struct termios cookedInput, rawInput;

If you will not specify explicitly initializers for the objects then they will be initialized like

struct termios cookedInput = { 0 }, rawInput = { 0 };
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I tried to define them in the main but `gcc -Werror` reports it as an unused variable even though I'm using it in a function `stdin_prep();` inside the main. How do I specify initializers explicitly or even some kind of NULL value? – ntruter42 Apr 21 '20 at 18:32
  • @ntruter42 Define them in modules where the function are defined.:) – Vlad from Moscow Apr 21 '20 at 18:33