我知道C中的全局变量有时会有extern关键字.什么是extern变量?宣言是什么样的?它的范围是什么?
这与跨源文件共享变量有关,但这是如何工作的?我在哪里用extern?
我有一个2模块(.c文件)和一个.h头文件:
file1.c中:
#include <stdio.h>
#include "global.h"
int main()
{
i = 100;
printf("%d\n",i);
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
file2.c中
#include <stdio.h>
#include "global.h"
void foo()
{
i = 10;
printf("%d\n",i);
}
Run Code Online (Sandbox Code Playgroud)
global.h
int i;
extern void foo()
Run Code Online (Sandbox Code Playgroud)
当我做gcc file1.c file2.c一切正常,我得到了预期的输出.现在,当我将头文件中的变量'i'初始化为0并再次编译时,我得到一个链接器错误:
/tmp/cc0oj7yA.o:(.bss+0x0): multiple definition of `i'
/tmp/cckd7TTI.o:(.bss+0x0): first defined here
Run Code Online (Sandbox Code Playgroud)
如果我只是通过头文件中的初始化(即gcc file1.c)编译file1.c(删除对foo()的调用),一切正常.到底是怎么回事?
这个答案困惑了我.
如果我们在同一个.c文件中有两行:
extern int c;
int c;
Run Code Online (Sandbox Code Playgroud)
到目前为止,我一直在使用以下方式的类:
GameEngine.h声明该类如下
class GameEngine {
public:
// Declaration of constructor and public methods
private:
InputManager inputManager;
int a, b, c;
// Declaration of private methods
};
Run Code Online (Sandbox Code Playgroud)
我的GameEngine.cpp文件然后只是实现方法
#include "____.h"
GameEngine::GameEngine() {
}
void GameEngine::run() {
// stuff
}
Run Code Online (Sandbox Code Playgroud)
但是,我最近读到变量声明不应该在头文件中.在上面的例子中,这将是一个inputManager和a,b,c.
现在,我一直在寻找放置变量声明的位置,我找到的最接近的答案是:头文件中的变量声明
但是,我不确定extern的使用是否有意义; 我只是声明私有变量,它们只会在类本身的实例中使用.我的头文件中的变量声明是否正常?或者我应该把它们放在别处吗?如果我把它们放在cpp文件中,它们会直接进入#include吗?
编辑:正确的函数名称,并添加#pragma一次
这是我的问题的一个非常强大的简化,但如果我这样做:
啊
#pragma once
static int testNumber = 10;
void changeTestNumber();
Run Code Online (Sandbox Code Playgroud)
A.cpp
#pragma once
#include "A.h"
void changeTestNumber()
{
testNumber = 15;
}
Run Code Online (Sandbox Code Playgroud)
BH
#pragma once
#include "A.h"
// some other stuff
Run Code Online (Sandbox Code Playgroud)
B.cpp
#pragma once
#include "B.h"
// some other stuff
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#pragma once
#include "B.h"
#include <iostream>
int main(){
changeTestNumber();
std::cout<<testNumber<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么我在呼叫时没有得到testNumber = 15?当我使用包含在我的包含标题的标题中的函数时,会发生什么?如果我删除静在从INT testNumber,我会得到一些错误,我的testNumber被初始化两次.
当我这样做时,我的标题编译了两次吗?
提前致谢!
目前我有我的上面定义的子程序和全局变量main().我正在尝试用C创建一个库.我可以在头文件中声明全局变量吗?
我收到链接器的错误,例如:
osd.o(.ndata+0x514):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\osd.c: multiple definition of `video_buff_vis_num'
main.o(.ndata+0x0):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\main.c: first defined here
osd.o(.ndata+0x515):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\osd.c: multiple definition of `video_buff_draw_num'
main.o(.ndata+0x1):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\main.c: first defined here
osd.o(.ndata+0x516):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\osd.c: multiple definition of `vid_format'
main.o(.ndata+0x2):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\main.c: first defined here
osd.o(.ndata+0x518):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\osd.c: multiple definition of `vid_line'
main.o(.ndata+0x4):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\main.c: first defined here
Run Code Online (Sandbox Code Playgroud)
这让我感到烦恼,因为在源代码中,我已经包含了这些定义可能来自唯一地方的警卫.
#ifndef OSD_H …Run Code Online (Sandbox Code Playgroud)