相关疑难解决方法(0)

如何使用extern在源文件之间共享变量?

我知道C中的全局变量有时会有extern关键字.什么是extern变量?宣言是什么样的?它的范围是什么?

这与跨源文件共享变量有关,但这是如何工作的?我在哪里用extern

c global-variables extern

942
推荐指数
13
解决办法
67万
查看次数

头文件中的全局变量

我有一个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

50
推荐指数
4
解决办法
12万
查看次数

在C++头文件中声明和定义静态变量?

许多 其他 问题涉及如何通过在头文件中声明变量并在.cpp文件中定义(分配)来分配变量.

我想要做的是不为我的类使用任何.cpp文件,并将所有函数定义为内联(在头文件中).我遇到的问题是如何定义静态成员变量,以便即使.h文件包含在多个编译单元中,我也不会得到"此处首次定义"链接器错误.

如果它完成了工作,我会对预处理器黑客等开放.我只是想避免任何.cpp文件.

如果重要我正在使用GCC.

c++ variables static gcc

12
推荐指数
1
解决办法
2万
查看次数

C中的声明和定义混淆

这个答案困惑了我.

如果我们在同一个.c文件中有两行:

extern int c;
int c;
Run Code Online (Sandbox Code Playgroud)
  • 第一行代码如何成为声明,第二行如何定义?
  • 这两个声明都不是吗?
  • 这两行有何不同?

c external public extern

6
推荐指数
1
解决办法
145
查看次数

C++标头或.cpp中的类变量声明?

到目前为止,我一直在使用以下方式的类:

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吗?

c++ header-files variable-declaration

5
推荐指数
2
解决办法
2万
查看次数

在头文件C++中初始化变量

编辑:正确的函数名称,并添加#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被初始化两次.

当我这样做时,我的标题编译了两次吗?

提前致谢!

c++ initialization header

4
推荐指数
2
解决办法
3796
查看次数

您是否在C库中定义全局变量?

目前我有我的上面定义的子程序和全局变量main().我正在尝试用C创建一个库.我可以在头文件中声明全局变量吗?

c global-variables static-libraries

2
推荐指数
1
解决办法
4421
查看次数

不知怎的,我违反了一个定义规则

我收到链接器的错误,例如:

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)

linker gcc

0
推荐指数
1
解决办法
391
查看次数