Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

makefile - C: compiling with -D in make file

Hi i'm having issues while compiling my c program. I'm using Makefile to compile it.

this is my make file :

# flags per la compilazione
#CFLAGS = -std=c89 -Wpedantic
    
CC = gcc
SO_HEIGHT= SO_HEIGHT=20
SO_WIDTH= SO_WIDTH=60

LIBS=libs/
OBJ = $(LIBS)ipc_utilities.o $(LIBS)utilities.o $(LIBS)dijkstra.o
OBJMAIN = main.o
OBJSOSOURCES=so_sources.o
OBJTAXI=taxi.o
all : utilities main so_sources taxi clean run
main: $(OBJMAIN) $(OBJ)
    $(CC)  $(OBJMAIN) $(OBJ)-o main
so_sources: $(OBJSOSOURCES) $(OBJ)
    $(CC) $(OBJSOSOURCES) $(OBJ)  -o so_sources
taxi: $(OBJTAXI) $(OBJ)
    $(CC) $(OBJTAXI) $(OBJ)-o taxi
utilities: 
    $(CC)   -c  -D $(SO_HEIGHT) -D $(SO_WIDTH) -o $(LIBS)utilities.o $(LIBS)utilities.c

clean:
    rm -f *.o 
    rm -f $(LIBS)*.o
    clear
    

# il target run si usa talvolta per eseguire l'applicazione
run: 
    ./main

this is the error i get :

gcc   -c  -D SO_HEIGHT=20 -D SO_WIDTH=60 -o libs/utilities.o libs/utilities.c
gcc    -c -o libs/ipc_utilities.o libs/ipc_utilities.c
In file included from libs/ipc_utilities.h:4,
                 from libs/ipc_utilities.c:8:
libs/utilities.h:44:27: error: ‘SO_HEIGHT’ undeclared here (not in a function)
   44 |     struct strada cityMap[SO_HEIGHT][SO_WIDTH];
      |                           ^~~~~~~~~
libs/utilities.h:44:38: error: ‘SO_WIDTH’ undeclared here (not in a function)
   44 |     struct strada cityMap[SO_HEIGHT][SO_WIDTH];
      |                                      ^~~~~~~~
make: *** [<builtin>: libs/ipc_utilities.o] Error 1

On ipc_utilities.h i include utilities.h :

#include <stdio.h>
#include <stdlib.h>
#include "utilities.h" // error

I'm familiar with makefile , anynone can help?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are a number of ways to put the pieces together, but since you need both the define name and its value, I would do something like the following:

SO_HEIGHT   := 20
SO_WIDTH    := 20

CFLAGS  := -std=c11 -Wall -Wextra -pedantic -Wshadow
CFLAGS  += -DSO_HEIGHT=$(SO_HEIGHT) -DSO_WIDTH=$(SO_WIDTH)
...
$(CCLD) -o $(APPNAME) $(OBJECTS) $(CFLAGS) $(LDFLAGS) $(LIBS)

Now you have the define label and value as part of your CFLAGS variable (e.g. -DSO_HEIGHT=$(SO_HEIGHT)) you do not need to include anything further in your makefile rule.


Of Course, you could also simply do:

SO_HEIGHT   := SO_HEIGHT=20
SO_WIDTH    := SO_WIDTH=20

CFLAGS  := -std=c11 -Wall -Wextra -pedantic -Wshadow
CFLAGS  += -D$(SO_HEIGHT) -D$(SO_WIDTH)

It's really however you want to do it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...