无法在gcc中使用全局变量 [英] 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;
}
但是当我用gcc编译它时,它的行为就像vidmem甚至不存在.我在函数外部声明的所有变量都遇到了这个问题. Maby可以在main内部声明此变量,但是不能在链接到此文件的其他文件中声明其他变量. 这是我的编译方式:
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?
我正在制作一个操作系统,这是来自C内核的代码.我正在使用Windows的Linux子系统对其进行编译,并使用qemu进行了测试
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
此处是所有代码的链接: http://www. mediafire.com/file/7x21lh4dnc93dz9/OS.7z/file
Here is a link to all the code: http://www.mediafire.com/file/7x21lh4dnc93dz9/OS.7z/file
推荐答案
您可以声明全局变量,但必须在main内部进行如下初始化: char * vidmem;
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;
}
这篇关于无法在gcc中使用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!