3

我认为我的“亲爱的”代码有问题,我正在使用代码块,当我尝试编译时,编译器向我显示此错误:“ld 返回 1 退出状态”

主程序

#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;
}

文件.h

#ifndef FIB_H_INCLUDED
#define FIB_H_INCLUDED
#endif // FIB_H_INCLUDED

int fibonacci(int );

纤维网

#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 ;
}

编译器

||=== 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)) ===|

由于未选中“调试”和“发布”框而导致链接问题之前

reference not defined to "fibonacci"

但我解决了它:右键单击fib.cor\and fib.h,properties->build

我真的没有找到解决第一个问题的方法......

感谢您的关注

4

3 回答 3

2

它是一个链接器错误。您已向主程序提供函数的声明(原型),但未提供定义。您需要编译这两个文件并将目标文件提供给链接器 (ld)。在linux中,一起编译如下

gcc main.c fib.c -o fib

于 2016-03-16T13:31:05.807 回答
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.

于 2016-03-16T14:05:39.020 回答
0

代码是正确的,我使用了命令提示符和代码

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

它有效,所以我认为这是 CodeBlocks 的错误。可能是什么问题呢?

于 2016-03-16T15:42:28.477 回答