C编程中的外部命令

问题描述

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

所以我有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个退出状态

我认为我需要在头文件中添加一些内容,因为我不认为他知道函数在哪里。

解决方法

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...