0

Now I am trying to access makefile variable in my user program in xv6. In other linux system, it can be easily achieved by doing that

  1. in makefile, define gcc -D MYVARIABLE=1 ...
  2. in my linux user program, by defining #include <stdio.h>, I can access MYVARIABLE.

but in xv6, there is no <stdio.h>. so I can't access MYVARIABLE.

How can I do for access MYVARIABLE??

moon
  • 23
  • 4
  • 2
    It is absolutely no true that you need to `#include ` to access `MYVARIABLE`. By adding `-DMYVARIABLE=1` to the command line you've created a _preprocessor_ variable named `MYVARIABLE` and it will be available anywhere in that source file, regardless of what headers you include or don't include. – MadScientist Apr 04 '22 at 13:52
  • @MadScientist oh.. I confused the usage.. thank you :) – moon Apr 05 '22 at 12:39

1 Answers1

0

Guessing you use this repo: https://github.com/mit-pdos/xv6-public, you can define MYVARIABLE in CFLAGS declaration, near line 83.

CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
# Your macro here
CFLAGS += -DMYVARIABLE=1

In that makefile, there is no %.o: %.c rule: implicit rules are used:

n.o is made automatically from n.c with a command of the form $(CC) -c $(CPPFLAGS) $(CFLAGS)

Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • really thank you for repply! do you happen to know how can I change the MYVARIABLE dynamically by using `make MYVARIABLE=something`?? I try this, but in my source file, it doesn't change.. :( – moon Apr 05 '22 at 12:50
  • @moon Warning, The `MYVARIABLE` in you source code is not a variable, but a macro, it will be replaced by the value you give. Anyway, you can have dynamic value, evaluated when `make` is called, see [that question](https://stackoverflow.com/q/10024279/1212012) – Mathieu Apr 05 '22 at 12:58