C标题问题:#include和“未定义引用”

时间:2021-10-10 15:08:41

Alright, I've been trying to work with this for the longest time, and I simply can't seem to get it to work right. I have three files, main.c, hello_world.c, and hello_world.h. For whatever reason they don't seem to compile nicely, and I really just can't figure out why...

好吧,我花了很长一段时间来研究这个问题,但我似乎就是做不好。我有三个文件,main。c,hello_world。c,hello_world.h。不管出于什么原因,他们似乎没有很好地编译,我真的不知道为什么…

Here are my source files. First hello_world.c:

这是我的源文件。第一个hello_world.c:

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

int hello_world(void) {
  printf("Hello, Stack Overflow!\n");
  return 0;
}

Then hello_world.h, simple:

然后hello_world。h,简单:

int hello_world(void);

And then finally main.c:

最后c:

#include "hello_world.h"

int main() {
  hello_world();
  return 0;
}

When I put it into GCC, this is what I get:

当我把它放入GCC时,我得到了:

cc     main.c   -o main
/tmp/ccSRLvFl.o: In function `main':
main.c:(.text+0x5): undefined reference to `hello_world'
collect2: ld returned 1 exit status
make: *** [main] Error 1

Anyone able to help me out? I'm really stuck on this, but I'm 99 percent sure it's a really simple fix.

有人能帮我吗?我真的被困住了,但我百分之九十九肯定这是一个很简单的解决办法。

5 个解决方案

#1


36  

gcc main.c hello_world.c -o main

Also, always use header guards:

另外,一定要使用头戴式护罩:

#ifndef HELLO_WORLD_H
#define HELLO_WORLD_H

/* header file contents go here */

#endif /* HELLO_WORLD_H */

#2


8  

You are not including hello_world.c in compilation.

你不包括hello_world。在编译c。

   gcc hello_world.c main.c   -o main

#3


4  

You should link object file compled from your second .c file hello_world.c with your main.o

您应该链接来自第二个.c文件hello_world的补充对象文件。c你main.o

try this

试试这个

cc -c main.c
cc -c hello_world.c
cc *.o -o hello_world

#4


4  

You are not linking against hello_world.c.

您没有链接到hello_world.c。

An easy way to do this is to run this compilation command:

一个简单的方法是运行这个编译命令:

cc -o main main.c hello_world.c

cc - o主要主要。c hello_world.c

More complicated projects often use build scripts or make files that separate the compilation and linking commands, but the above command (combining both steps) should do fine for small projects.

更复杂的项目通常使用构建脚本或生成文件来分离编译和链接命令,但是上面的命令(结合这两个步骤)对于小型项目来说应该很好。

#5


1  

Ya it seems you have forgotten to link hello_world.c. I will be gcc hello_world.c main.c -o main. If the number of files are less we can use this approach but in larger projects better to use Make files or some compilation scripts.

是的,看来你忘了连接hello_world.c了。我将是gcc hello_world。c主要。c - o主要。如果文件数量较少,我们可以使用这种方法,但在较大的项目中,最好使用Make文件或一些编译脚本。

#1


36  

gcc main.c hello_world.c -o main

Also, always use header guards:

另外,一定要使用头戴式护罩:

#ifndef HELLO_WORLD_H
#define HELLO_WORLD_H

/* header file contents go here */

#endif /* HELLO_WORLD_H */

#2


8  

You are not including hello_world.c in compilation.

你不包括hello_world。在编译c。

   gcc hello_world.c main.c   -o main

#3


4  

You should link object file compled from your second .c file hello_world.c with your main.o

您应该链接来自第二个.c文件hello_world的补充对象文件。c你main.o

try this

试试这个

cc -c main.c
cc -c hello_world.c
cc *.o -o hello_world

#4


4  

You are not linking against hello_world.c.

您没有链接到hello_world.c。

An easy way to do this is to run this compilation command:

一个简单的方法是运行这个编译命令:

cc -o main main.c hello_world.c

cc - o主要主要。c hello_world.c

More complicated projects often use build scripts or make files that separate the compilation and linking commands, but the above command (combining both steps) should do fine for small projects.

更复杂的项目通常使用构建脚本或生成文件来分离编译和链接命令,但是上面的命令(结合这两个步骤)对于小型项目来说应该很好。

#5


1  

Ya it seems you have forgotten to link hello_world.c. I will be gcc hello_world.c main.c -o main. If the number of files are less we can use this approach but in larger projects better to use Make files or some compilation scripts.

是的,看来你忘了连接hello_world.c了。我将是gcc hello_world。c主要。c - o主要。如果文件数量较少,我们可以使用这种方法,但在较大的项目中,最好使用Make文件或一些编译脚本。