Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

c - Can't use global variables with gcc

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...