Code Blocks: Undefined reference to myFct

时间秒杀一切 提交于 2020-03-25 14:28:44

问题


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");
}

While compiling this project i have this error "undefined reference to myFct"

I'am using Code::Blocks13.12 and windows 8

Thanks in advance


回答1:


You need to compile both the files

When i just compiled main.c I got the error

{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

On compiling with both the files no errors were found.

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

To compile multiple files in Codeblocks you need to create a project which includes all the files.

Links below will help you

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

code::blocks - how to compile multiple file projects

"extern" changes the linkage. With the keyword, the function / variable is assumed to be available somewhere else and the resolving is deferred to the linker.

By removing the extern the issue should get resolved.

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

void myFct(void);

#endif // FUNCTIONS_H_INCLUDED



回答2:


Thanks yanivx

I get the solution from this 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: "Debug" and "Release""



来源:https://stackoverflow.com/questions/29770358/code-blocks-undefined-reference-to-myfct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!