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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

How would I initialize them in my main.c? rawInput = NULL; and rawInput = { 0 }; doesn't work.
@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};
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?
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
I put the definition just on top of int main(void) {... and that solved the problem! Thanks.
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 };

2 Comments

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 Define them in modules where the function are defined.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.