C头问题:#include和“undefined reference” [英] C header issue: #include and "undefined reference"
问题描述
好的,我一直试图用最长的时间来处理这个问题,而且我似乎无法让它正常工作。我有三个文件, main.c
, hello_world.c
和 hello_world.h
。无论出于何种原因,他们似乎都不能很好地编译,而我真的不知道为什么......
以下是我的源文件。第一个hello_world.c:
#include< stdio.h>
#includehello_world.h
int hello_world(void){
printf(Hello,Stack Overflow!\\\
);
返回0;
}
然后hello_world.h,简单:
int hello_world(void);
然后main.c:
#includehello_world.h
int main(){
hello_world();
返回0;
}
当我将它放入GCC时,这就是我得到的结果:
cc main.c -o main
/tmp/ccSRLvFl.o:函数`main':
main.c :(。text + 0x5):对`hello_world'的未定义引用
collect2:ld返回1退出状态
make:*** [main]错误1
任何人都可以帮助我?我真的很坚持这一点,但我99%肯定这是一个非常简单的修复程序。
code> gcc main.c hello_world.c -o main
另外,请始终使用标头防护:
#ifndef HELLO_WORLD_H
#define HELLO_WORLD_H
/ *头文件内容go here * /
#endif / * HELLO_WORLD_H * /
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...
Here are my source files. First 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:
int hello_world(void);
And then finally main.c:
#include "hello_world.h"
int main() {
hello_world();
return 0;
}
When I put it into GCC, this is what I get:
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.
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 */
这篇关于C头问题:#include和“undefined reference”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!