【问题标题】:Visual Studio Code: Error include external header in CVisual Studio Code:错误在 C 中包含外部标头
【发布时间】:2020-06-07 04:08:39
【问题描述】:

编程新手,实际上是在学习 C 的开放课堂教程,我发现自己被困在包括外部标题部分在内的内容上。我使用 IDE,我在 Visual Studio Code 上,我可以切换到 IDE,但在简单的文本编辑器中应该是可能的并且不难做到,所以我想了解如何。

程序运行时的错误代码:

main.c:6:10: fatal error: 'level.h' file not found
#include "level.h"
         ^~~~~~~~~
1 error generated.

这是我的main.c 代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#include "level.h"
#include "menu.h"

int level();
int menu();
int main()
{
    int nombreMystere, guess, round = 10;
    char answer;
    bool exit = true;
    while (exit)
    {
        menu();
        {       
            while(guess != nombreMystere)
            {      
                printf("%d"                          , nombreMystere);
                printf("\nIl vous restes %d round.\n", round        );
                printf("Trouver le nombre magique: "                );
                scanf ("%d"                          , &guess       );
                if(guess < nombreMystere) printf("Trop bas\n"); 
                else if(guess > nombreMystere) printf("Trop Haut\n"); 

                round --;
                if (round == 0)
                {
                    printf("You Lose.. Dumbass");
                    break;
                }
                if (guess == nombreMystere)  printf("\nGood job !\n");
            }

            printf("Play again?\n (yes/no): ");

            scanf("%s", &answer);
            if (answer == 110 || answer == 78) {exit=0;}
        }
    }
    return 0;
}

这里是level.c

static int selec;

int level(int x)
{
    int max, difficulty = selec;

    if      (difficulty==1){max    = 100                                ;}
    else if (difficulty==2){max    = 1000                               ;}
    else if (difficulty==3){max    = 10000                              ;}
    return max;
}

level.h:

int level(int x);

menu.c:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "menu.h"
int level();
int menu()
{
    srand(time(NULL));
    int  max, nombreMystere, selec;
    const int MIN = 1;
    while (selec < 1 || selec > 3)

            {
                printf("\n\nSelect Difficulty level:\n");
                printf("1: Level 1\n");
                printf("2: Level 2\n");
                printf("3: Level 3\n");
                scanf("%d", &selec);
                level(selec);
                printf("\n\n%d\n\n", max);
                //if      (difficulty==1){max    = 100                                ;}
                //else if (difficulty==2){max    = 1000                               ;}
                //else if (difficulty==3){max    = 10000                              ;}
                //else                   {printf("Wrong selection, please try again.");}
                nombreMystere = (rand() % (max - MIN + 1)) + MIN;
            }
    return nombreMystere;
}

menu.h:

int menu();

我整天都在互联网上搜索解决此问题的方法,但我不明白我所看到的一半,每个主题都只谈论 C++。我在某处看到它可能来自 VSCode 中的 tasks.json 文件,但它是关于 C++ 主题的。

我正在使用 macOS。

感谢您的宝贵时间。

编辑:.h 和 .c 文件都在不同的文件夹中,并且两个文件夹都在同一个目录中。

【问题讨论】:

  • 确保level.h存在于主代码所在的同一目录中。
  • 感谢您的回答,是的,它们都在同一个目录中。
  • 你的编辑怎么样?您是使用 IDE 还是从 bash 提示符运行它们?如果您是在 bash 提示符下执行此操作,您是如何运行它的?
  • 来自 Visual Studio 代码运行选项。

标签: c json visual-studio-code header fatal-error


【解决方案1】:

level.h 文件需要与main.c 位于SAME 文件夹中。

menu.h 文件也应该在同一个文件夹中。因此,请确保您将所有文件都放在同一个文件夹中。

另外这两行不应该在你的main.c

int level();
int menu();

因为这本质上是包含标题的内容。在那张纸条上,我也看不到您在 main.c 文件中调用函数 level(),因此您甚至根本不需要包含 level.h 文件。但是你确实调用了函数menu(),所以你需要像你正在做的那样#include "menu.h"

同样在menu.c 文件中,您不需要这行#include "menu.h",您应该将其更改为#include "level.h",因为您在代码中调用level() 函数,然后还删除它后面的行,那是; int level(); 行,因为这基本上就是包含级别标题将为您做的事情。

level.c 文件在我看来很好。

如果同一文件夹中的所有文件仍然出现错误,请确保正确编译它。

【讨论】:

    【解决方案2】:

    include "x" 中,x 的值代表你的头文件的路径(相对于使用当前目录中的目录... 或绝对路径,后者是一个BAD PRACTICE 不要这样做,但你可以,旁注:~ 不受支持)如果你所有的文件都在同一个目录中,你不应该有这样的错误,你不必使用相对路径默认情况下假定为当前目录,但如果不考虑以正确的方式包含它们。有关您的编码风格的更多说明,请参见 sveinnth 的答案。

    还有一件事,您应该在问题中包含编译选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多