c语言中的外部命令

Extern command in c programming

我正在尝试编写一个打印斐波那契数列的程序。我必须使用“header.h”和“source.c”文件(我不能在header文件中编写函数)。

所以我有 3 个文件:

主要

// main.c file
#include <stdio.h>
#include "header1.h"

int main(){
    int n=15, i=0;
    printf("Fibonacci series terms are:\n");

    for (int c=1; c<=n; c++ ){
        printf("%d ", fibonacci(i));
        i++;
    }

    return 0;
}

HEADER

// header1.h file
#ifndef header1_h
#define header1_h

extern int fibonacci(int n);

#endif

来源

// source1.c
#include "header1.h"

int fibonacci(int n){
    if ( n==0 || n==1 )
        return n;
    else
    return fibonacci(n-1) + fibonacci(n-2);
}

CodeBlock 编译器:

对“斐波那契”的未定义引用

错误:ld 返回了 1 个退出状态

我想我需要在 header 文件中添加一些东西,因为我认为他不知道函数在哪里。

问题出在链接器上,您需要将文件添加到项目中。

解决方法: 将所有文件添加到项目中 https://www.quora.com/How-can-I-add-h-header-files-in-Code-Blocks