简体   繁体   中英

Having trouble with makefile: it doesn't behave the way I've been taught it should and I'm not sure what to do

I am working on a project for a class teaching C. The idea is to create a visualization of the mandelbrot set using functions set in different source files and linked together using headers and a makefile. I can confirm 100% of complex.c (below) works correctly and there are no warnings or errors when it is compiled. For this project, however, I have moved the type definitions to the header file, and I have a global variable of the "imaginary" type in my main.c file. That global variable is referenced by my mandelbrot.h header file.

I get two errors when I attempt to compile using my makefile. These errors are:

1. "error: unknown type name "img""

This one occurred after I moved my "typedef struct{}img;"to the header file.

2. Undeclared variable c referenced for first time at line <whatever> in mandelbrot.c

I have img c declared in main.c, and extern img c declared in mandelbrot.h. I do not know what's going on with this, since our professor rather explicitly said to declare the variable as global in main.c and then reference it via extern in mandelbrot.h so it can be seen in mandelbrot.c

I have tried to be explicit because if I'm doing something wrong I want to trace and find it (plus we're supposed to use explicit makefiles and not use special variables, like $(CC) or such. The final executable is mandelbrot.

mandelbrot: main.o mandelbrot.o complex.o
    gcc -o -Wall mandelbrot main.o mandelbrot.o complex.o

main.o: main.c complex.h mandelbrot.h
    gcc -c main.c

mandelbrot.o: mandelbrot.c complex.h mandelbrot.h
    gcc -c mandelbrot.c

complex.o: complex.h complex.c
    gcc -c complex.c -lm

clean:
    rm *.o

Here is my source code (all headers also include my function prototypes, but I haven't copied them):

//complex.h
//Components of complex number.
typedef struct{
    float r;
    float j;
} img;

and here is the source file:

//complex.c
#include <stdio.h>
#include <math.h>

img function(c){
//does something with the global variable c
}

I have a second function, which is handling some checks involving the mandelbrot set. The function is in a separate file and has:

//mandelbrot.h
//Reference an external global variable. 
extern img c;

And the source file:

//mandelbrot.c
#include "complex.h"
img mandelbrot(int n){
    //Code that does stuff
    if (absolute_value(c, n-1) > 1000000){
    //does something
    }
}

I have one last source file:

//main.c
#include "mandelbrot.h"
#include "complex.h"
img c;

main(){
    //does some stuff.
}
  1. can be fixed by putting #include "complex.h" (which is reserved by C, use something else) in mandelbrot.h .

  2. can be fixed by putting #include "mandelbrot.h" inside mandelbrot.c and removing #include "complex.h" from mandelbrot.c and main.c .

  3. can be fixed by putting #include "complex.h" in complex.c .

  4. can be fixed by changing -o -Wall mandelbrot to -o mandelbrot -Wall .

You really need to start using header guards.

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