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

Recommended Answers

All 2 Replies

I think you need a file complex.h containing the line

void read_comp(complex, int, int);

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.