c - 如何在 Linux 中使用 makefile 编译多个 c 文件 - 出现变量错误

标签 c linux ubuntu-18.04

我正在尝试使用 Makefile 编译 5 个 *.c 文件(gcc -o prog add.c create.c delete.c display.c main .c) 但我收到了 variable is not declared 错误。我在上面的命令中创建了将 main.c 放在第一位,但仍然出现相同的错误。 有人可以指出我在这里做错了什么,我怎样才能创建变量以便编译器可以读取它?

同样下面的代码被分成了5个文件 错误:

add.c: In function ‘add’:
add.c:3:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
  printf("\n\n\t\t\t\t*****ADD Menu*****");
  ^~~~~~
add.c:3:2: warning: incompatible implicit declaration of built-in function ‘printf’
add.c:3:2: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
add.c:5:6: error: ‘left’ undeclared (first use in this function)
  if (left == SIZE - 1)
      ^~~~
add.c:5:6: note: each undeclared identifier is reported only once for each function it appears in
add.c:5:14: error: ‘SIZE’ undeclared (first use in this function)
  if (left == SIZE - 1)
              ^~~~
add.c:12:3: warning: implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]
   scanf_s("%d", &holder);
   ^~~~~~~
add.c:15:3: error: ‘numarr’ undeclared (first use in this function)
   numarr[left] = holder;
   ^~~~~~
add.c:16:7: error: ‘right’ undeclared (first use in this function)
   if (right == -1)
       ^~~~~
create.c: In function ‘create’:
create.c:3:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
  printf("\n\n\t\t\t\tArray has been created with the size of 10 elements");
  ^~~~~~
create.c:3:2: warning: incompatible implicit declaration of built-in function ‘printf’
create.c:3:2: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
delete.c: In function ‘delete’:
delete.c:4:6: error: ‘right’ undeclared (first use in this function)
  if (right == -1)
      ^~~~~
delete.c:4:6: note: each undeclared identifier is reported only once for each function it appears in
delete.c:6:3: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
   printf("\n\n\t\t\t\tERROR! Queue is empty. Please add element before deleting them");
   ^~~~~~
delete.c:6:3: warning: incompatible implicit declaration of built-in function ‘printf’
delete.c:6:3: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
delete.c:10:12: error: ‘numarr’ undeclared (first use in this function)
   holder = numarr[right];
            ^~~~~~
delete.c:11:3: warning: incompatible implicit declaration of built-in function ‘printf’
   printf("\n\n\t\t\t\tSUCCESS. Deleted item: %d", holder);
   ^~~~~~
delete.c:11:3: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
delete.c:13:15: error: ‘left’ undeclared (first use in this function)
  if (right == left)
               ^~~~
display.c: In function ‘display’:
display.c:4:6: error: ‘right’ undeclared (first use in this function)
  if (right == -1)
      ^~~~~
display.c:4:6: note: each undeclared identifier is reported only once for each function it appears in
display.c:6:3: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
   printf("\n\n\t\t\t\tEmter elements to array to be displayed here!");
   ^~~~~~
display.c:6:3: warning: incompatible implicit declaration of built-in function ‘printf’
display.c:6:3: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
display.c:10:3: warning: incompatible implicit declaration of built-in function ‘printf’
   printf("\n\n\t\t\t\tDisplay of current elements in queue: ");
   ^~~~~~
display.c:10:3: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
display.c:11:24: error: ‘left’ undeclared (first use in this function)
   for (i = right; i <= left; i++)
                        ^~~~
display.c:12:29: error: ‘numarr’ undeclared (first use in this function)
    printf("\n\n\t\t\t\t%d", numarr[i]);
                             ^~~~~~
main.c:3:10: fatal error: conio.h: No such file or directory
 #include <conio.h>
          ^~~~~~~~~
compilation terminated.

#include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #define SIZE 10
    int numarr[SIZE];
    int right, left;

    //function create is in separate c file named create.c
    void create()
    {
        printf("\n\n\t\t\t\tArray has been created with the size of 10 elements");
    }

    //function add is in separate c file named add.c
    void add()
    {
        printf("\n\n\t\t\t\t*****ADD Menu*****");
        int holder;
        if (left == SIZE - 1)
        {
            printf("\n\n\t\t\t\tQueue is FULL");
        }
        else
        {
            printf("\n\t\t\t\tEnter the element you wish to add into queue: ");
            scanf_s("%d", &holder);
            ++left;

            numarr[left] = holder;
            if (right == -1)
                right = 0;
        }
    }

    //function delete is in separate c file delete.c
    void delete()
    {
        int holder;
        if (right == -1)
        {
            printf("\n\n\t\t\t\tERROR! Queue is empty. Please add element before deleting them");
        }
        else
        {
            holder = numarr[right];
            printf("\n\n\t\t\t\tSUCCESS. Deleted item: %d", holder);
        }
        if (right == left)
        {
            right = left = -1;
        }
        else
        {
            right++;
        }
    }

    //function display is in separate c file named display.c
    void display()
    {
        int i;
        if (right == -1)
        {
            printf("\n\n\t\t\t\tEmter elements to array to be displayed here!");
        }
        else
        {
            printf("\n\n\t\t\t\tDisplay of current elements in queue: ");
            for (i = right; i <= left; i++)
                printf("\n\n\t\t\t\t%d", numarr[i]);
        }
    }

    //function main is in separate c file named main.c
    int main()
    {
        right = left = -1;
        int choice;
        do
        {
            printf("\n\n\t\t\t\tWELCOME TO C PROGRAM ON FIFO \n");
            printf("\t\t\t\t1. CREATE an array \n");
            printf("\t\t\t\t2. ADD to array \n");
            printf("\t\t\t\t3. DELETE from arra \n");
            printf("\t\t\t\t4. DISPLAY the array's content \n");
            printf("\t\t\t\t5. EXIT the program \n");
            printf("\t\t\t\tPlease SELECT from above options: ");
            scanf_s("%d", &choice);
            switch (choice)
            {
            case 1:
                //create
                create();
            break;

            case 2:
                //add
                system("cls");
                add();
                break;

            case 3:
                //delete
                delete();
                break;

            case 4:
                //display
                display();
                break;

            case 5:
                exit(0);
            }
        } while (choice != 5);

        return 0;
    }``

最佳答案

您正在开发一个程序,该程序已分成几个“模块”。每个模块都是一个 .c 文件。

当您编译所有这些模块时,编译器会为每个 .c 文件生成一个 .o 文件,并不知道它们是相关的。每个编译称为一个“编译单元”。

为了“告诉”编译器源文件是相关的,您可以创建 .h 文件,您可以在 .c 文件中 #include 并且包含关于每个 .c 的其他模块的信息文件需要知道。

关于c - 如何在 Linux 中使用 makefile 编译多个 c 文件 - 出现变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53863384/

相关文章:

c - 使用基于指针的条件时,即使循环条件为零,循环条件是否也有效?

regex - Perl\R 正则表达式去除 Windows 换行符

java - 如何从java捕获错误并打印c程序触发器的流

c - Posix 线程 - 并行线程

linux - 关于linux中的环境变量和init

ubuntu - 在 Ubuntu 18 上安装 gcloud/gsutil

python - 如何在 matplotlib 中使用 Humor Sans?

python - 在 WSL 发行版上的 Ubuntu 18.04 上安装 tkinter

c - 是否有在转义模式下打印字符串的功能?

linux - Linux下的动态内存管理