头文件和extern关键字

头文件和extern关键字

本文介绍了头文件和extern关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的外部变量和头文件有很多的问题。我经历的书籍阅读节并寻找小时网页,但我一直无法弄清楚。在了解这个问题的任何帮助将大大AP preciated。以下是code和错误,当我尝试编译

 的#include<&stdio.h中GT;
    #包括LT&;&stdlib.h中GT;
    #包括LT&;&string.h中GT;
    #包括sample.h    诠释的main()
      {
          INT I;
          INT GI = 0;
          双recv的;          我= 10;
          GI = I;          的recv = AnotherFunc();          返回0;
      }

而sample.h是以下

 的#ifndef样本
      #定义样本      EXTERN INT的gI;
      EXTERN双AnotherFunc();      #万一

和另一个是功能是

 的#include<&math.h中GT;
       #包括LT&;&stdio.h中GT;
       #包括LT&;&stdlib.h中GT;
       #包括LT&;&string.h中GT;
       #包括sample.h       双AnotherFunc()
        {
           双someVar;
           INT测试;           测试=的gI;           someVar = 10.0;
           返回someVar;
         }

当我编译下面的路上,我遇到下面的错误,我不明白为什么我得到这些错误。 sample.h有变量声明,它应该是在AnotherFunc可见。

 的gcc -Wall -ansi -pedantic -c MAIN.C AnotherFunc.c
          GCC main.o中AnotherFunc.o -o测试
          AnotherFunc.o:在功能`AnotherFunc:
          。AnotherFunc.c :(文字+为0x6):未定义引用'的gI
          collect2:劳工处返回1退出状态

我只加了INT GI = 0;因为我想定义它。如果我修改code以下面的方式我主要出现错误也。请参考下面。

 的#include<&stdio.h中GT;
           #包括LT&;&stdlib.h中GT;
           #包括LT&;&string.h中GT;
           #包括sample.h           INT主(INT ARGC,CHAR *的argv [])
            {
              INT I;
              双recv的;               我= 10;
              GI = I;              的recv = AnotherFunc();              返回0;
            }             GCC -Wall -Wstrict的原型-ansi -pedantic -c MAIN.C AnotherFunc.c
             GCC main.o中AnotherFunc.o -o测试
             main.o中:在函数'主':
             。MAIN.C :(文字+ 0x1b):未定义引用'的gI
             AnotherFunc.o:在功能`AnotherFunc:
             。AnotherFunc.c :(文字+为0x6):未定义引用'的gI
             collect2:劳工处返回1退出状态


解决方案

移动 INT GI = 0 之外的main()所以,它是全球可用:

  INT GI = 0;
诠释的main()
  {
      INT I;  ....
  }

I am having a lot of issue using extern variable and header files. I have read through sections of books and searched the web for hours but I haven't been able to figure out. Any help in understanding this problem will be greatly appreciated. The following is the code and the error when I try to compile

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "sample.h"

    int main()
      {
          int i;
          int gI = 0;
          double recv;

          i = 10;
          gI = i;

          recv = AnotherFunc();

          return 0;
      }

And the sample.h is the following

      #ifndef SAMPLE
      #define SAMPLE

      extern int gI;
      extern double AnotherFunc();

      #endif

And the other is function is

       #include <math.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <string.h>
       #include "sample.h"

       double AnotherFunc()
        {
           double someVar;
           int test;

           test = gI;

           someVar = 10.0;
           return someVar;
         }

When I compile the following way, I get the following errors which I don't understand why I am getting those errors. sample.h has the variable declaration and it should be visible in the AnotherFunc.

          gcc -Wall -ansi -pedantic -c Main.c AnotherFunc.c
          gcc Main.o AnotherFunc.o -o test
          AnotherFunc.o: In function `AnotherFunc':
          AnotherFunc.c:(.text+0x6): undefined reference to `gI'
          collect2: ld returned 1 exit status

I only added the int gI = 0; because I wanted to define it. If I modify the code in the following way I get error in main also. Please see below.

           #include <stdio.h>
           #include <stdlib.h>
           #include <string.h>
           #include "sample.h"

           int main(int argc, char *argv[])
            {
              int i;
              double recv;

               i = 10;
              gI = i;

              recv = AnotherFunc();

              return 0;
            }

             gcc -Wall -Wstrict-prototypes -ansi -pedantic -c Main.c AnotherFunc.c
             gcc Main.o AnotherFunc.o -o test
             Main.o: In function `main':
             Main.c:(.text+0x1b): undefined reference to `gI'
             AnotherFunc.o: In function `AnotherFunc':
             AnotherFunc.c:(.text+0x6): undefined reference to `gI'
             collect2: ld returned 1 exit status
解决方案

Move int gI = 0 outside main() so that it is globally available:

int gI = 0;
int main()
  {
      int i;

  ....
  }

这篇关于头文件和extern关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:35