简体   繁体   English

请任何人在编写 c 程序时向我解释这个警告(使用 extern 关键字)

[英]Please can anyone explain me this warning while writing a c program(use of extern keyword)

file name:- next.c文件名:- next.c

extern int global_var = 2;

file name:- try.c文件名:-try.c

#include <stdion.h>
#include "next.c"
void main()
{
printf("%d", global_var);
}

The waring that I am getting我得到的警告

In file include form try.c:2:0:
next.c.1.12: warning: 'globl_var' initialized and declared 'extern'
 extern in global_var = 22;
           ^~~~~~~~~

The warning itself tells you that your code is wrong.警告本身会告诉您您的代码是错误的。 The messages can be "translated" into something like: "Declaring a variable as extern and at the same time assigning a value, is wrong.这些消息可以“翻译”成类似这样的内容:“将变量声明为 extern同时分配一个值是错误的。

It seems you have misunderstood the normal way to use include files.您似乎误解了使用包含文件的正常方式。 You don't do:你不这样做:

#include "next.c"

What you do is你所做的是

#include "next.h"

That is... a c-file contains source code for a unit.那就是......一个c文件包含一个单元的源代码。 The corresponding h-file contains information about the unit that you want to share with other units (aka c-files).相应的 h 文件包含有关您要与其他单元共享的单元的信息(也称为 c 文件)。

Try this:尝试这个:

next.h:下一个.h:

extern int global_var;  // Tell other c-files that include next.h
                        // that a int-variable with name global_var
                        // exists

next.c:下一个。c:

int global_var = 2;     // Define and initialize global_var

and in try.c do:并在 try.c 中执行:

#include "next.h"       // Include next.h to know what the unit next.c
                        // makes available for use in try.c

The above is for a global variable defined by the unit next.c.以上是针对单元next.c定义的全局变量。 For functions is pretty much the same.对于功能几乎相同。

Assume that next.c implements a function foo that you want try.c to call... Then you do the same, ie you write the functions source code in next.c and use next.h to tell other units that the function is available. Assume that next.c implements a function foo that you want try.c to call... Then you do the same, ie you write the functions source code in next.c and use next.h to tell other units that the function is可用的。 Like:喜欢:

next.h:下一个.h:

extern int global_var;  // Tell other c-files that include next.h
                        // that a int-variable with name global_var
                        // exists

void foo(int a, int b);  // Tell other c-files that include next.h
                         // that a function with name foo
                         // exists

next.c:下一个。c:

int global_var = 2;     // Define and initialize global_var

void foo(int a, int b)   // Define foo
{
    ... source code ...
}

and in try.c use it like:并在 try.c 中使用它:

#include "next.h"       // Include next.h to know what the unit next.c
                        // makes available for use in try.c

#include "next.h"       // Include next.h to know what the unit next.c
                        // makes available for use in try.c

int bar()
{
    int x = 0;
    int y = 42;
    foo(x, y);    // Call function foo in unit next.c
    ....
}

extern is used to tell compiler that variable is declared out of scope. extern用于告诉编译器变量是从 scope 中声明的。

first.c一、c

#include <stdio.h>
#include "second.h"

int main(){

    extern const float pi;

    printf("pi : %f\n" , pi);

    return 0;
}

second.h第二个.h

const float pi = 3.148;

output output

pi: 3.148000圆周率:3.148000

why to use extern?为什么要使用外部?

to prevent local redeclaration of variable and make code more readable and understandable防止本地重新声明变量并使代码更具可读性和可理解性

example error示例错误
first.c一、c

#include <stdio.h>
#include "second.h"

int main(){

    extern const float pi;
    const float pi;

    printf("pi : %f\n" , pi);

    return 0;
}

output output

first.c: In function 'main':首先。c:在 function '主要':
first.c:7:17: error: redeclaration of 'pi' with no linkage first.c:7:17:错误:重新声明没有链接的“pi”
7 | 7 | const float pi;常量浮点数;
| | ^~ ^~
In file included from first.c:2:在 first.c:2 中包含的文件中:
second.h:1:13: note: previous definition of 'p second.h:1:13:注意:'p 的先前定义
' with type 'float' ' 类型为 'float'
1 | 1 | const float pi = 3.148;常量浮动 pi = 3.148;
| | ^~ ^~

extern tells the compiler that the extern declared symbol is defined in a different source file or rather translation unit. extern 告诉编译器 extern 声明的符号是在不同的源文件或翻译单元中定义的。

When you assign a value to an extern symbol, you basically say " the definition is somewhere else, but the definition is [x]"当您为外部符号赋值时,您基本上会说“定义在其他地方,但定义是 [x]”

To fix that, you should "define" the variable somewhere without "extern".要解决这个问题,您应该在没有“extern”的地方“定义”变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM