Mikołaj
Mikołaj

Reputation: 83

Program works only with {} symbols in .h file

I have three files. Why program works only with {} symbols in .h file?

szereg.c:

    #include "szereg.h"

double szereg(double x, double eps, int *sw, int *M, int *stop){ 

    double s, w;
    int i = 2;
        s=x;
        w=x;
    do{ 
        if(*sw==*M){
            if(fabs(w)<=eps && *sw==*M)
                *stop =3;
            else
                *stop = 1;
            return s;
        }
        w=((-w*x)/i)*(i-1);
        s=s+w;
        *sw+=1;
        i++;
    } 
    while (fabs(w) >= eps); 

    *stop = 2;

    return s; 
}

szereg.h:

    double szereg(){};

main.c:

        #include <stdio.h>
        #include <math.h>
        #include <stdlib.h>
        #include "szereg.h"

        FILE *fw; 
        extern double szereg(); 

        int main(){ 

            int lp, sw=1, M, stop = 0;
            double a, b, dx, x, y, eps, z; 
            char *stopwar = "";



            if((scanf("%lf %lf %d %lf %d", &a, &b, &lp, &eps, &M) != 5) || a<=-1 || b>1 || eps<0.0 || lp<1){ 
                printf("Blad danych\n"); 
                system("pause"); 
            exit(1); 
        } 

        if(!(fw = fopen("wyniki.txt", "w"))){ 
            printf("Blad otwarcia zbioru\n"); 
            exit(2); 
        } 

        dx=(b-a)/lp;

        for(x = a; x <= b + 0.5*dx; x += dx){ 
            y = szereg(x, eps, &sw, &M, &stop); 
            z = log(x+1); 

            sw=1;
        } 

        fclose(fw); 
        exit(0); 
        system("pause"); 
    } 

Upvotes: 0

Views: 51

Answers (1)

datenwolf
datenwolf

Reputation: 162164

Let me guess: Without the curly braces you're getting a linker error along the lines of an unresolved symbol.

Here's the thing, these two statements are exactly the same to the compiler:

extern double szereg();
double szereg(); // without {} is exactly the same as with extern

What it tells the compiler is, that there is a symbol szereg that belongs to a function that returns a double and takes an unspecified number of arguments. It however does not "glue" together the different compilations units.

What you have to do is compile each .c file separately to an object file, then link all the object files together in a final linking step. So far you're probably linking only the main object file and nothing else.

Upvotes: 5

Related Questions