C语言 在 C 中创建外部字符数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7670816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:50:29  来源:igfitidea点击:

Create extern char array in C

ccharextern

提问by SMUsamaShah

How to create an external character array in C?

如何在C中创建外部字符数组?

I have tried various ways to define char cmdval[128]but it always says undefined reference to 'cmdval'

我尝试了各种方法来定义char cmdval[128]但它总是说undefined reference to 'cmdval'

I want to put a string in cmdval in first.cfile and use it in other second.cfile. I tried adding a global.hfile with extern char cmdval[128]but no luck.

我想在first.c文件中的 cmdval 中放置一个字符串并在其他second.c文件中使用它。我尝试添加一个global.h文件,extern char cmdval[128]但没有运气。

UPDATE:

更新:

global.h

全局文件

extern char cmdval[128];

first.c

第一个.c

#include "global.h"

char cmdval[128];

function(){
   strcpy(cmdval, "a string");
}

second.c

第二个.c

#include "global.h"

function(){
   printf("%s \n",cmdval); //error
}

FAIL :( "undefined reference to `cmdval'"

失败 :(“未定义对‘cmdval’的引用”

EDIT: I am working in linux (editing a mini OS xv6 then compiling and running it in qemu), I don't know if it is a barrier

编辑:我在 linux 中工作(编辑一个 mini OS xv6 然后在 qemu 中编译和运行它),我不知道这是否是一个障碍

回答by Geoffrey

You need to declare it in the .h file

您需要在 .h 文件中声明它

extern char cmdval[128];

And then define the value in first.c;

然后在first.c中定义值;

char cmdval[128];

Then anything that includes your .h file, provided it is linked with first.o will have access to it.

然后任何包含您的 .h 文件的内容,只要它与 first.o 链接,都可以访问它。

To elaborate, "extern" is saying, there is an external variable that this will reference... if you dont then declare cmdval somewhere, cmdval will never exist, and the extern reference will never reference anything.

详细说明,“extern”是说,有一个外部变量将引用……如果您不在某处声明 cmdval,则 cmdval 将永远不存在,并且 extern 引用将永远不会引用任何内容。

Example:

例子:

global.h:

全球.h:

extern char cmdval[128];

first.c:

首先.c:

#include "global.h"
char cmdval[128];

int main() {
  strcpy(cmdval, "testing");
  test();
}

second.c:

第二个.c:

#include "global.h"

void test() {
  printf("%s\n", cmdval);
}

You can compile this using:

您可以使用以下方法编译它:

gcc first.c second.c -o main

Or make the .o files first and link them

或者先制作 .o 文件并链接它们

gcc -c first.c -o first.o
gcc -c second.c -o second.o
gcc first.o second.o -o main

回答by lostyzd

Externdoesn't mean find it somewhere, it changes the variable linkageto external, which means no matter how many time you declare a variable, it references to the same thing.

Extern并不意味着在某处找到它,它将变量更改linkageexternal,这意味着无论您声明变量多少次,它都引用相同的内容。

e.g. These references the same thing in c(not c++), a global variable's linkage by default is external.

例如,这些在 c(不是 c++)中引用了相同的东西,默认情况下全局变量的链接是external.

external char cmdval[128];
char cmdval[];
char cmdval[128];

The problem is that you shoud first compile them into .o, then link those .o together.

问题是您应该首先将它们编译成 .o,然后将这些 .o 链接在一起。

gcc -c first.c second.c
gcc -o first.o second.o

or

或者

gcc first.c second.c

回答by lostyzd

You should have a compilation unit in which you definethe cmdvalvariable. The externkeyword only declaresthat the variable exists, it does not define it.

你应该在你编译单元定义cmdval变量。该extern关键字仅声明变量存在,并没有定义它。

Put the following line in first.c, second.cor in an other C file of your choice:

将以下行放入first.csecond.c或您选择的其他 C 文件中:

char cmdval[128];

回答by Pete Houston

In second.c

second.c

#include "global.h"

define extern char cmdval[128]in global.has well.

也定义extern char cmdval[128]global.h