如何使用“外部结构”来共享c编程中的变量,并与gcc一起编译?

时间:2022-09-07 21:04:10

I want some shared variables should be accessed among source files, main.c and second.c and my header file is all.h defined the shared data type,

我希望在源文件中访问一些共享变量,main。c和s。c和我的头文件都是。h定义了共享数据类型,

#ifndef ALL_H
#define ALL_H
struct foo {
    double v;
    int i;
};

struct bar {
    double x;
    double y;
};
#endif

main.c is given below

主要。c是下面

/* TEST*/
#include "all.h"
#include "second.h"

int main(int argc, char* argv[])
{
    struct foo fo; // should be accessed in second.c
    fo.v= 1.1;
    fo.i = 12;

    struct bar ba; // should be accessed in second.c
    ba.x= 2.1;
    ba.y= 2.2;

    sec(); // function defined in second.c

    return 0;
}

second.h is given below

第二。h是下面

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

int sec();

second.c is given below

第二。c是下面

#include "second.h"

extern struct foo fo;
extern struct bar ba;

int sec()
{
    printf("OK is %f\n", fo.v+ba.x);

    return 0;
}

I thought i have all the declaration and include the headers. But when i compile

我想我有所有的声明,包括标题。但是当我编译

    gcc -o main main.c second.c 

or 

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

It will give some error like

它会给出一些错误。

second.o: In function `sec':
second.c:(.text+0x8): undefined reference to `fo'
second.c:(.text+0xe): undefined reference to `ba'
collect2: ld returned 1 exit status

I think somewhere of the use of extern was wrong or i use the gcc incorrectly?

我认为在外部使用的某个地方是错误的,或者我错误地使用了gcc ?

3 个解决方案

#1


3  

The problem is with the scope. Your variables (fo & ba) have local scope as they are declared within main.So, their visibility is restricted to within main function. Please make them global variables and it should work.

问题在于范围。您的变量(fo & ba)具有本地范围,因为它们是在main中声明的。因此,它们的可见性被限制在主函数内。请让它们成为全局变量,它应该可以工作。

#2


2  

The error message indicates that the linker is unable to find fo and ba. With the extern declaration you have told the compiler that the variables will exist in some other translation unit, but they don't.

错误消息表明链接器无法找到fo和ba。在外部声明中,您告诉编译器变量将存在于其他的翻译单元中,但是它们没有。

You need to move the struct foo fo; and struct bar ba; outside of the main() function. Right now, they are function local variables. They need to be global variables for this to work.

你需要移动struct foo fo;英航struct酒吧;在main()函数之外。现在,它们是函数局部变量。它们需要全局变量才能工作。

#3


1  

//main.h

/ / main.h

typedef struct
{
    double v;
    int i;
 }foo;

//extern.h

/ / extern.h

extern foo fo;

//main.c

/ / c

#include "main.h"
#include "extern.h"
//use fo.v here

//second.c

/ / second.c

#include "second.h"
#include "main.h"
#include "extern.h"
foo fo;
//use fo.v here

Just include #include "main.h",#include "extern.h" in all .c files you want to use fo. Notice that foo fo is only in second.c,nowhere else

主要包括# include”。h”,# include“走读生。在所有的。c文件中,你想使用fo。请注意,foo fo仅在第二个位置。c,其他地方没有

#1


3  

The problem is with the scope. Your variables (fo & ba) have local scope as they are declared within main.So, their visibility is restricted to within main function. Please make them global variables and it should work.

问题在于范围。您的变量(fo & ba)具有本地范围,因为它们是在main中声明的。因此,它们的可见性被限制在主函数内。请让它们成为全局变量,它应该可以工作。

#2


2  

The error message indicates that the linker is unable to find fo and ba. With the extern declaration you have told the compiler that the variables will exist in some other translation unit, but they don't.

错误消息表明链接器无法找到fo和ba。在外部声明中,您告诉编译器变量将存在于其他的翻译单元中,但是它们没有。

You need to move the struct foo fo; and struct bar ba; outside of the main() function. Right now, they are function local variables. They need to be global variables for this to work.

你需要移动struct foo fo;英航struct酒吧;在main()函数之外。现在,它们是函数局部变量。它们需要全局变量才能工作。

#3


1  

//main.h

/ / main.h

typedef struct
{
    double v;
    int i;
 }foo;

//extern.h

/ / extern.h

extern foo fo;

//main.c

/ / c

#include "main.h"
#include "extern.h"
//use fo.v here

//second.c

/ / second.c

#include "second.h"
#include "main.h"
#include "extern.h"
foo fo;
//use fo.v here

Just include #include "main.h",#include "extern.h" in all .c files you want to use fo. Notice that foo fo is only in second.c,nowhere else

主要包括# include”。h”,# include“走读生。在所有的。c文件中,你想使用fo。请注意,foo fo仅在第二个位置。c,其他地方没有