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

Dani AI

Generated

This thread has the right direction — stop including .c files and use a header — but a couple of important details were missed. , the compile/link errors come from including complex.c (which pulls in function bodies before your typedef) and from using a by-value function that can’t change the caller’s struct. and are correct to suggest a header; add one, include it in both .c files, and compile the .c files separately.

Two simple, safe designs (different from the snippets already posted) you can use:

  • Write a small header with the struct type (avoid the name complex because C99 provides a complex macro; use complex_t) and the prototype.
  • Either make the mutator take a pointer so it modifies the caller’s instance, or make a factory function that returns a struct by value.

Example (pointer mutator):

typedef struct { double re, im; } complex_t;

void set_complex(complex_t *c, double re, double im)
{
    if (c) { c->re = re; c->im = im; }
}

/* usage:
   complex_t A = {0};
   set_complex(&A, 5.1, 6.2);
*/

Example (return-by-value):

complex_t make_complex(double re, double im)
{
    complex_t c;
    c.re = re;
    c.im = im;
    return c;
}

/* usage:
   complex_t A = make_complex(5.1, 6.2);
*/

Makefile and troubleshooting tips:

  • Put the typedef and prototypes in complex.h and #include "complex.h" in both main.c and complex.c.
  • Do not #include "complex.c". That can produce “multiple definition” linker errors or undefined-type compile errors depending on ordering.
  • Link math -lm only at the final link step. Use header dependencies so changes recompile: e.g. main.o: main.c complex.h and complex.o: complex.c complex.h.
  • Watch warnings: passing 5.1 to an int parameter truncates the value; prefer double for real numbers. Use -Wall -Wextra to catch these.
  • If you see “unknown type name ‘complex’”, the typedef hasn’t been seen by that translation unit; check includes and include-guard ordering.

Following those steps will fix the compile errors and also make your function actually modify the complex value the way you expect.

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.