0

First, I do know that it is not recommended to include .c in another .c but to do it with .h instead, everyone told me that
but my instructor demands it, but whenever i try it it just won't compile.
i appretiate any help...
im working on ubunto 12.04 platform and it gives me this error in the picture attached.
so i have the main.c :

#include <stdio.h>
#include "complex.c"
void read_comp(complex,int,int);

typedef struct  ComplexNumber{  /* Structure that holds a complex number*/

  double x;
  double y;

} complex;

int main()
{

  complex A,B,C,D,E,F;
            /*=========== Resets numbers to zero ===========*/
  A.x = A.y = B.x = B.y = C.x = C.y = D.x = D.y = E.x = E.y = F.x = F.y = 0;

  read_comp(A,5.1,6.2);

  return 0;
}

and my function from other .c file named, complex.c :

void read_comp(complex compNum, int a, int b)
{
    compNum.x = a;
    compNum.y = b;
}

and here's the makefile i did :

myprog: main.o complex.o 
    gcc -g -Wall -ansi -pedantic main.o complex.o -o myprog -lm

main.o: main.c
    gcc -c -Wall -ansi -pedantic main.c -o main.o -lm

complex.o: complex.c
    gcc -c -Wall -ansi -pedantic complex.c -o complex.o -lm
Attachments ללא_שם.jpg 67.46 KB
3
Contributors
2
Replies
35
Views
5 Months
Discussion Span
Last Post by kristiangd
0

You're including a .c file, not a file (.h). Thus the body of function "void read_comp (complex, int int)" in the .c file is declared before of prototype declaration in main.c.

You have to create a header file and put the prototype of "void read_comp (complex, int, int);" And the declaration of complex typedef and include this header file in main.c.

It would look more or less like that.

complex.h

#ifndef __COMPLEX_H__
#define __COMPLEX_H__

typedef struct  ComplexNumber{  /* Structure that holds a complex number*/
  double x;
  double y;
} complex;

void read_comp(complex compNum, int a, int b);

#endif

complex.c

#include "complex.h"

void read_comp(complex compNum, int a, int b)
{
    compNum.x = a;
    compNum.y = b;
}

main.c

#include <stdio.h>
#include "complex.h"

int main()
{
  complex A,B,C,D,E,F;
            /*=========== Resets numbers to zero ===========*/
  A.x = A.y = B.x = B.y = C.x = C.y = D.x = D.y = E.x = E.y = F.x = F.y = 0;
  read_comp(A,5.1,6.2);
  return 0;
}

Edited by kristiangd

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.