0

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

Bou6
  • 84
  • 2
  • 10
  • May I ask what is the use for the extern keyword for your function? Try to remove it and compile again to see what happens. – Corb3nik Apr 21 '15 at 11:09
  • extern means that the function is declared in another file . I removed extern but the same error appears – Bou6 Apr 21 '15 at 11:19
  • Yes, I am aware. But extern is obsolete when it comes to function declarations in headers. You can remove them. – Corb3nik Apr 21 '15 at 11:23
  • You need to make sure both the files are compiled at the same time see my answer below I am using gcc compiler to do the same. This fixed the issue and also gave the expected results. – Vinay Shukla Apr 21 '15 at 11:27

2 Answers2

3

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
Community
  • 1
  • 1
Vinay Shukla
  • 1,818
  • 13
  • 41
0

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""

Community
  • 1
  • 1
Bou6
  • 84
  • 2
  • 10