3

i think my "dear" code has a problem, i'm using code block and when i try to compile, the compiler show me this error: "ld returned 1 exit status"

main.c

#include <stdio.h>
#include <stdlib.h>
#include "C:\Users\alexl_000\Desktop\fibmodnopoin\fib.h"

int main(){
    int position;
    int *fib_ptr,fib=2;
    char confirm='y';
    fib_ptr = &fib;

printf("find Fibonacci's number by it's position \n");

while(confirm=='y'){
    printf("insert position:");
    scanf("%d", &position);
    printf("%d \n",position);
    fib=fibonacci(position);
    printf("%d \n", *fib_ptr);
    printf("find new number?");
    scanf("%s", &confirm);
}
return 0;
}

fib.h

#ifndef FIB_H_INCLUDED
#define FIB_H_INCLUDED
#endif // FIB_H_INCLUDED

int fibonacci(int );

fib.c

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


int fibonacci(int position){

    int fib1=1, fib2=1, count=3, fib;

    if (posizione==0)
      fib=0;

    else if (position==1||2 )
      fib=1;

    if (position>2){
      while(count <=  position){
         count++;
         fib=fib1+fib2;
         fib2=fib1;
         fib1=fib;
        }
    }
    return fib ;
}

compiler

||=== Build: Debug in fibmodnopoin (compiler: GNU GCC Compiler) ===|
||error: ld returned 1 exit status|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

before there were a linking problem due to "debug" and "release" box not checked

reference not defined to "fibonacci"

but i solve it: right click to fib.c or\and fib.h, properties->build

i really do not find a way to solve the first problem...

Thank you for the attention

LPs
  • 16,045
  • 8
  • 30
  • 61

3 Answers3

2

Its a linker error. You have provided declaration (prototype) of the function to the main program but the definition is not provided. You need to compile both the file and provide the object files to the linker (ld). In linux, compile together as follows

gcc main.c fib.c -o fib

NishanthSpShetty
  • 661
  • 5
  • 14
1

In Fib.c, you're checking a "posizione" variable, which is not declared anywhere. Once you change that, you should be able to rebuild the module and after that, the project.

As a side note, I'd recommend checking the include guard in fib.h. Normally, you'd put declarations in the ifdef block to make sure the variables are only declared once per file.

shanglun
  • 73
  • 9
  • it's a typing error, i'm italian and "posizione" is "position" i rewrite the variables' name in english when i write the question and i made a little mistake – alexioloruxo Mar 16 '16 at 15:00
0

the code is correct, i used the command prompt and the code

gcc main.c fib.c -o fib.exe

and it works, so i think it's a CodeBlocks' error. What could be the problem?