代码块:对 myFct 的未定义引用

Code Blocks: Undefined reference to myFct

main.c

#include <stdio.h>
#include <stdlib.h>
#include "functions.h"

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

functions.h

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
#include <stdio.h>

extern void myFct(void);

#endif // FUNCTIONS_H_INCLUDED

functions.c

#include "functions.h"

void myFct(void)
{
printf ("helloFCT");
}

编译这个项目时出现这个错误"undefined reference to myFct"

我正在使用 Code::Blocks13.12 和 windows 8

提前致谢

你需要编译这两个文件

当我刚编译时 main.c 我得到了错误

{yanivx@~/functions}$ gcc main.c
/tmp/ccoJitEe.o: In function `main':
main.c:(.text+0x7): undefined reference to `myFct'
collect2: ld returned 1 exit status

编译这两个文件时没有发现错误。

{yanivx@~/functions}$ gcc main.c functions.c
{yanivx@~/functions}$ ./a.out
helloFCT

要在 Codeblocks 中编译多个文件,您需要创建一个包含所有文件的项目。

下面的链接会对您有所帮助

http://forums.codeblocks.org/index.php?topic=13193.0

code::blocks - how to compile multiple file projects

"extern" 更改链接。使用关键字,函数/变量被假定在其他地方可用,解析被推迟到链接器。

删除 extern 问题应该得到解决。

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
#include <stdio.h>

void myFct(void);

#endif // FUNCTIONS_H_INCLUDED

谢谢 yanivx

我从这个link得到了解决方案:

code::blocks - how to compile multiple file projects

"Go to the left panel that says projects, and right-click on .cpp file. Select properties, then go to build. Check the boxes under the heading Belongs in Targets: "调试”和"Release"”