简体   繁体   中英

Extern command in c programming

I'm trying to write a program that prints the Fibonacci sequence. I must use “header.h” and “source.c” files (i can't write the function in the header file).

So I have 3 files:

MAIN

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

SOURCE

// 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 compiler:

undefined reference to `fibonacci'

error: ld returned 1 exit status

I think i need to add something to the header file because i don't think he knows where the function is.

The problem is with the linker, you need to add the file to the project.

Solution: Add all the files to the project https://www.quora.com/How-can-I-add-h-header-files-in-Code-Blocks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM