Question: I have a file like this:

```
char* vidmem = (char*)0xb8000;

int main()
{
    vidmem[0] = 'x';
    vidmem[1] = 0x0f;
}

```

But when i compile it with gcc it behave like vidmem does not even exist. I have this problem with all variable declared outside of function. Maby this one can be declare inside main but other variables in other files linked to this can't be.
This how I compile it:

```
gcc -c main.c -o main.obj -ffreestanding -fno-exceptions -m64

```

And this is how i assemble all the files:

```
gcc -m64 -Wl,--build-id=none -static -fno-asynchronous-unwind-tables -nostdlib -nodefaultlibs -lgcc main.obj [..] -T linker.ld -o out.bin

```

Why this happen? And how can i solve it?

**EDIT:**

I am making an OS and this is the code from the C kernel. I am using linux subsystem for windows to compile it and qemu for testing

Here is a link to all the code: <http://www.mediafire.com/file/7x21lh4dnc93dz9/OS.7z/file>

Answer: 
You can declare global variables but you have to initialize inside main like this:
char\* vidmem;

```
char* vidmem;

int main()
{
    vidmem = (char*)0xb8000;

    vidmem[0] = 'x';
    vidmem[1] = 0x0f;
}

```
I don't understand why there are so many down votes to this question. This is a totally valid and interesting question and I just ran into the same problem 1 hour ago.

The problem is that in a normal scenario when the linker is generating an executable to run on Linux, for example, the global variables are stored in the .data section, and their values are part of the executable. Now, in your scenario, you are generating raw binary data and directly booting it. In this case, you need to specify to the linker where the global variables should be stored in the image. You can do it via `-Tdata 0x6000` to store them starting at address 0x6000, for example. Then, when loading your OS, be sure to load this part of the image to memory, before accessing the global variable.