在 C 中使用具有多个文件的 makefile 时出现问题

发布于 2024-11-01 23:51:45 字数 2261 浏览 2 评论 0原文

开始了解 C 程序的 makefile,但在尝试包含多个文件时遇到一些问题。忽略下面的程序不完整的事实(就功能而言而不是编译而言),我试图使用 make 文件编译并运行该程序。

这是我的 make 文件:

main: main.o IntList.o
    gcc -o main main.o IntList.o

main.o: main.c
    gcc -c -ansi -pedantic -Wall main.c

IntList.o: IntList.c IntList.h
    gcc -c -ansi -pedantic -Wall Intlist.c

这是我收到的错误:

gcc -c -ansi -pedantic -Wall Intlist.c
gcc -o main main.o IntList.o
ld: duplicate symbol _getNewInt in IntList.o and main.o
collect2: ld returned 1 exit status
make: *** [main] Error 1

该程序的代码如下。我不确定是 make 文件还是我在程序文件中包含的内容导致了问题(或两者兼而有之!)

任何帮助都会很棒。干杯。

编辑:任何能引导我在模块化方面朝着正确方向前进的提示将不胜感激,因为我不确定我是否以最好的方式这样做。

IntList.h

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

/* Constants */
#define MAX_INTS 10

/* Signed ints can have a maximum of 10 digits. We make the length 11 to 
 * allow for the sign in negative numbers */
#define MAX_INPUT_LENGTH 11
#define EXTRA_SPACES 2

/* Typedefs / Structs */
typedef struct {
   int list[MAX_INTS];
   int noInts;
} IntList;

/* Proto Types */
int insertIntToList(int *list);
void shiftList(int offset);
void displayList();

IntList.c

#include "IntList.h"

int getNewInt(int *list)
{
   int valid = 0, inputInt;
   char inputString[MAX_INPUT_LENGTH + EXTRA_SPACES];

   while(!valid)
   {
      printf("Input an int: ");

      valid = 1;

      if((fgets(inputString, MAX_INPUT_LENGTH + EXTRA_SPACES, stdin)) != NULL)
      {
         sscanf(inputString, "%d", &inputInt);
         /* Check first that the input string is not too long */
         if(inputString[strlen(inputString) - 1] != '\n')
         {
            printf("\nError: Too many characters entered \n");
            valid = 0;
         }

         printf("\nThe Int: %d", inputInt);
         printf("\n");
      }
   }
}

void shiftList(int offset)
{
}

void displayList()
{
}

ma​​in.c

#include <stdio.h>
#include <stdlib.h>
#include "IntList.c"

int main(void)
{
   int intList[10];

   getNewInt(intList);

   return EXIT_SUCCESS;
}

Starting to get my head around makefiles for my C programs, but having some trouble when trying to include multiple files. Ignoring the fact that the program below is incomplete (in terms of functionality but not compilation), I'm trying to get this program compiling and running using a make file.

Here is my make file:

main: main.o IntList.o
    gcc -o main main.o IntList.o

main.o: main.c
    gcc -c -ansi -pedantic -Wall main.c

IntList.o: IntList.c IntList.h
    gcc -c -ansi -pedantic -Wall Intlist.c

And here is the error I am receiving:

gcc -c -ansi -pedantic -Wall Intlist.c
gcc -o main main.o IntList.o
ld: duplicate symbol _getNewInt in IntList.o and main.o
collect2: ld returned 1 exit status
make: *** [main] Error 1

The code for the program is below. I'm not sure whether it's the make file or my includes in the program files that are causing problems (or both!)

Any help would be great. Cheers.

Edit: Any tips to steer me in the right direction in terms of modularization would be much appreciated as I'm not sure if I am doing this the best way.

IntList.h

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

/* Constants */
#define MAX_INTS 10

/* Signed ints can have a maximum of 10 digits. We make the length 11 to 
 * allow for the sign in negative numbers */
#define MAX_INPUT_LENGTH 11
#define EXTRA_SPACES 2

/* Typedefs / Structs */
typedef struct {
   int list[MAX_INTS];
   int noInts;
} IntList;

/* Proto Types */
int insertIntToList(int *list);
void shiftList(int offset);
void displayList();

IntList.c

#include "IntList.h"

int getNewInt(int *list)
{
   int valid = 0, inputInt;
   char inputString[MAX_INPUT_LENGTH + EXTRA_SPACES];

   while(!valid)
   {
      printf("Input an int: ");

      valid = 1;

      if((fgets(inputString, MAX_INPUT_LENGTH + EXTRA_SPACES, stdin)) != NULL)
      {
         sscanf(inputString, "%d", &inputInt);
         /* Check first that the input string is not too long */
         if(inputString[strlen(inputString) - 1] != '\n')
         {
            printf("\nError: Too many characters entered \n");
            valid = 0;
         }

         printf("\nThe Int: %d", inputInt);
         printf("\n");
      }
   }
}

void shiftList(int offset)
{
}

void displayList()
{
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "IntList.c"

int main(void)
{
   int intList[10];

   getNewInt(intList);

   return EXIT_SUCCESS;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小巷里的女流氓 2024-11-08 23:51:45

不要在 main 中包含 .c 文件,而是包含 .h 文件。否则,IntList.c 中的代码会同时编译到 IntList.omain.o 中,因此您将得到重复的符号。

在 main.c 中使用它而不是 IntList.c

#include "IntList.h"

Don't include the .c file in main, include the .h file. Otherwise the code in IntList.c gets compiled both into the IntList.o and the main.o, so you'll get duplicate symbols.

Use this in main.c instead of IntList.c:

#include "IntList.h"
淡墨 2024-11-08 23:51:45
#include "IntList.c"

应该是:

#include "IntList.h"

另外(虽然与您的问题无关)我建议不要在源文件名称中使用混合大小写,因为它可能导致可移植性问题并且难以诊断“没有这样的文件”错误 - 使用全部小写,就像标准库头一样。

#include "IntList.c"

should be:

#include "IntList.h"

Also (though nothing to do with your problem) I would recommend not using mixed case in the names of source files, as it can lead to portability problems and hard to diagnose "no such file" errors - use all lower case, like the standard library headers do.

寻找我们的幸福 2024-11-08 23:51:45

不要将 #include "IntList.c" 放入 main.c

Don't #include "IntList.c" into main.c

标点 2024-11-08 23:51:45

您不应该:

#include "IntList.c"

在您的主程序中,它应该是:

#include "IntList.h"

通过包含 C 文件,您可以在 mainmain 中创建一个 getNewInt IntList 对象文件,这就是当您尝试将它们链接在一起时出现重复定义错误的原因。

You should not have:

#include "IntList.c"

in your main program, it should be:

#include "IntList.h"

By including the C file, you create a getNewInt in both your main and IntList object files, which is why you're getting the duplicate definition error when you try to link them together.

紫竹語嫣☆ 2024-11-08 23:51:45

main.c 应包含“IntList.h”,而不是“IntList.c”。

如果包含 IntList.c,则 IntList.c 中的函数将在 IntList.o 和 main.o 中实现,这将产生您所看到的“重复符号”错误。

main.c should include "IntList.h", not "IntList.c".

If you include IntList.c, the functions in IntList.c will be implemented both in IntList.o and in main.o, which would produce the "duplicate symbol" error you're seeing.

原谅我要高飞 2024-11-08 23:51:45

正如其他人提到的,您包含 .h 文件,而不是 .c 文件
此外,当您编译时,您只编译 .c 文件,因此您应该删除 Makefile 中对 .h 文件的任何引用

As others have mentioned, you include .h files, not .c files
Also when you compile, you only compile .c files so you should remove any references to .h files in your Makefile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文