Makefile's output explanation - c

I wrote the following makefile:
MAIN = main
# Macro nome compilatore
CC = gcc
# Macro flag del compilatore
CFLAGS = -Wall
# Macro file oggetto
OBJS = $(MAIN).o
# Macro libreria matematica
LIBS = -lm
# Target
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) $(LIBS) -o $(MAIN)
When i run it for the first time, the output is:
gcc -Wall -c -o main.o main.c
gcc -Wall main.o -lm -o main
Can you explain me the first line please? What does it mean?
Thank you for your time.

Related

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?
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.

Make file in C giving fatal error

I am trying to create a makefile in C but i am facing some issues.
I have my main .c, one .c which keeps the functions implementations and one .h which keeps the functions declarations.
When i try to run the makefile, i get a fatal error.
here is my makefile:
INCL = prog.h
SRC = prog.c prog_fun.c
OBJ = $(SRC:.c=.o)
EXE = prog
CC = gcc
CFLAGS = -c
RM = rm -rf
all: prog
prog: prog.o prog_fun.o
gcc -o prog prog.o prog_fun.o
prog.o: prog.c
gcc -c prog.c
prog_fun.o: prog_fun.c prog.h
gcc -c prog.c
clean:
$(RM) $(OBJ) $(EXE)
The error i get is this:
gcc -c prog.c
prog.c:11:19: fatal error: prog.h: No such file or directory
compilation terminated.
Makefile:17: recipe for target 'prog.o' failed
make: *** [prog.o] Error 1
Can anyone please help me with this?
What you are trying to do by using this
prog_fun.o: prog_fun.c header.h
It should be only
prog_fun.o: prog_fun.c
Modify your make file as
xyz#xyz-PC:~$ vi makefile
INCL = header.h
SRC = prog.c prog_fun.c
OBJ = $(SRC:.c=.o)
EXE = prog
CC = gcc
CFLAGS = -c
RM = rm -rf
all: prog
prog: prog.o prog_fun.o
gcc -o prog prog.o prog_fun.o
prog.o: prog.c
gcc -c prog.c header.h
prog_fun.o: prog_fun.c
gcc -c prog_fun.c header.h
clean:
$(RM) $(OBJ) $(EXE)
next run it
xxyz#xyz-PC:~$ make
gcc -c prog.c header.h
gcc -c prog_fun.c header.h
gcc -o prog prog.o prog_fun.o
I hope it will work now and make sure indention is correct(not manual spaces, it should be tab key)

Syntax for setting paths to library and compiler in makefile

I am trying to compile a program and I have to set the paths to the NETCDF inc and lib directories in the Makefile as well as set the gfortran and c compiler settings. The variables were already named as shown below, it just told me to edit the paths/options which I have tried to do. Here is what I have for my netcdf inc and lib, as well as the hdf5:
NETCDFINC = -I$(NETCDFF_INCDIR) -I$(NETCDFC_INCDIR)
NETCDFLIB = -Wl,-rpath=$(NETCDFF_LIBDIR) -L$(NETCDFF_LIBDIR) -Wl,-
rpath=$(NETCDFC_LIBDIR) -L$(NETCDFC_LIBDIR) -Wl,-rpath=$(HDF5_LIBDIR) -
L$(HDF5_LIBDIR) -lhdf5_hl -lhdf5 -lz
Note that the NETCDFLIB is on one line on my file, and the paths of the variables are correct, but the syntax may not be.
Next for the fortran and c compiler information I have the following:
FC = gfortran -m64 -g -O0 -ffixed-line-length-132 -Wunused -Wuninitialized
CC = gcc
CFLAGS = -m64 -c -g -I. -DLONG32 -DUNDERSCORE -DLITTLE -Wunused -
Wuninitialized
Finally, when I run the program I first get a bunch of warnings such as:
oban_namelist.f90:495.29:
real :: flt
1
Warning: Unused variable 'flt' declared at (1)
Followed by a bunch of undefined reference errors such as:
oban.o: In function `check':
/uufs/chpc.utah.edu/common/home/zpu-
group3/dhodges/DART/data/radar/opaws/oban.f90:1902: undefined reference to
`__netcdf_MOD_nf90_strerror'
The error shouldn't be in this oban.o function since I didn't write it. The only things I have edited in the program I listed above for the netcdf path and gfortran/c information and so I think the error lies there. This is my first question on here, so if I missed anything you need feel free to ask. What can I try?
Edit: By request, this is the entire makefile I am using. Also, I just type 'make' to compile it per the instructions in the README file.
# Makefile for creating the OPAWS analysis software
#
# Rev: 02/05/10 LJW
# 12/05/10 DCD
# 02/11/11 LJW
#
# netCDF4 libs - you need to fill in the blanks
NETCDFINC = -I$(NETCDFF_INCDIR) -I$(NETCDFC_INCDIR)
NETCDFLIB = -Wl,-rpath=$(NETCDFF_LIBDIR) -L$(NETCDFF_LIBDIR) -Wl,-rpath=$(NETCDFC_LIBDIR) -L$(NETCDFC_LIBDIR) -Wl,-rpath=$(HDF5_LIBDIR) -
L$(HDF5_LIBDIR) -lhdf5_hl -lhdf5 -lz
# Fortran and C compiler information - various configurations are setup, try and find one close
#=====>> Gfortran
#
FC = gfortran -m64 -g -O0 -ffixed-line-length-132 -Wunused -Wuninitialized
CC = gcc
CFLAGS = -m64 -c -g -I. -DLONG32 -DUNDERSCORE -DLITTLE -Wunused -Wuninitialized
# Leave this stuff alone
EXEC = x.oban
EXECcs = x.clutter_stats
EXECmd = mosaic_2_dart
OBJS = DART.o oban_module.o dict_module.o oban_namelist.o derived_types.o util.o fileio.o read_dorade.o binio.o v5d.o
OBJScs = DART.o dict_module.o oban_namelist.o derived_types.o fileio.o util.o read_dorade.o binio.o v5d.o
OBJSmd = DART.o dict_module.o oban_namelist.o derived_types.o fileio.o util.o read_dorade.o binio.o v5d.o
default: $(EXEC) $(EXECcs) $(EXECmd)
$(EXEC): $(OBJS) oban.o
$(FC) $(OPT) -o $(EXEC) oban.o $(OBJS) $(NETCDFLIB)
$(EXECcs): $(OBJScs) clutter_stats.o
$(FC) $(OPT) -o $(EXECcs) clutter_stats.o $(OBJScs) $(NETCDFLIB)
$(EXECmd): $(OBJSmd) mosaic_2_dart.o
$(FC) $(OPT) -o $(EXECmd) mosaic_2_dart.o $(OBJSmd) $(NETCDFLIB)
clean:
rm $(EXEC) oban.o $(OBJS) $(EXECcs) clutter_stats.o $(OBJScs) $(EXECmd) mosaic_2_dart.o $(OBJSmd) *.mod ncgen.input *.pyc sweep_file_list.txt
# Individual compilation instructions
oban.o: oban.f90 structures.inc opaws.inc DART.o
$(FC) $(OPT) -c $(NETCDFINC) oban.f90
clutter_stats.o: clutter_stats.f90 opaws.inc
$(FC) $(OPT) -c $(NETCDFINC) clutter_stats.f90
mosaic_2_dart.o: mosaic_2_dart.f90 opaws.inc
$(FC) $(OPT) -c $(NETCDFINC) mosaic_2_dart.f90
oban_module.o: oban_module.f90 derived_types.o opaws.inc
$(FC) $(OPT) -c oban_module.f90
read_dorade.o: read_dorade.c read_dorade.h
$(CC) $(CFLAGS) -c read_dorade.c
fileio.o: fileio.f90
$(FC) $(OPT) $(NETCDFINC) -c fileio.f90
util.o: util.f opaws.inc structures.inc
$(FC) $(OPT) -c util.f
DART.o: DART.f
$(FC) $(OPT) -c DART.f
derived_types.o: derived_types.f90
$(FC) $(OPT) -c derived_types.f90
oban_namelist.o: oban_namelist.f90 opaws.inc
$(FC) $(OPT) -c oban_namelist.f90
dict_module.o: dict_module.f90
$(FC) $(OPT) -c dict_module.f90
binio.o: binio.c
$(CC) $(CFLAGS) binio.c -o binio.o
v5d.o: v5d.c
$(CC) $(CFLAGS) v5d.c -o v5d.o
The problem was solved by editing the NETCDFLIB line as follows:
I edited the NETCDFLIB line as follows and it works: NETCDFLIB = -Wl,-
rpath=$(NETCDFF_LIBDIR) -L$(NETCDFF_LIBDIR) -Wl,-rpath=$(NETCDFC_LIBDIR) -
L$(NETCDFC_LIBDIR) -lnetcdff -lnetcdf -lm -Wl,-rpath=$(HDF5_LIBDIR) -
L$(HDF5_LIBDIR) -lhdf5_hl -lhdf5 -lz
Basically I just needed to add:
-lnetcdff -lnetcdf -lm
This allowed for the compiler to get access to the libraries it needed that were originally undefined.

make makes not written rules

Here is my makefile code :
CC = gcc
IFLAGS = -Iinclude
CFLAGS = -std=c99 -fPIC -pedantic -Wall -Wextra -c # -march=native -ggdb3
LFLAGS = -shared -lX11 # -march=native -ggdb3
DFLAGS = -L./lib -lOSPOOC
TARGET = lib/libOSPOOC.so
SOURCES = $(shell echo src/*.c)
HEADERS = $(shell echo include/*.h)
OBJECTS = $(patsubst %.c,%.o, $(SOURCES))
# CURLIBPATH = $(PWD)/lib
# LDLIBPATH = $(shell echo $(LD_LIBRARY_PATH) | grep $(CURLIBPATH))
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(LFLAGS) -o $(TARGET) $(OBJECTS)
*.o: *.c
echo allo
$(CC) $(IFLAGS) $(CFLAGS) -o $# $<
demo: $(TARGET) demo/windows.c
$(CC) $(IFLAGS) -o bin/windows demo/windows.c $(DFLAGS)
clean:
rm -r */*.o */*.so bin/*
The rule *.o is not called, and, I don't know how, but it compiles correctly...
Result :
make demo
gcc -std=c99 -fPIC -pedantic -Wall -Wextra -c -c -o src/OSPobj.o src/OSPobj.c
gcc -std=c99 -fPIC -pedantic -Wall -Wextra -c -c -o src/OSPutl.o src/OSPutl.c
gcc -std=c99 -fPIC -pedantic -Wall -Wextra -c -c -o src/OSPwin.o src/OSPwin.c
gcc -shared -lX11 -o lib/libOSPOOC.so src/OSPobj.o src/OSPutl.o src/OSPwin.o
gcc -Iinclude -o bin/windows demo/windows.c -L./lib -lOSPOOC
The problem is that -c is used twice, and echo allo isn't event called...
What do I don't understand, why do I have this strange behaviour?
Ok that's not *.o: *.c but %.o: %.c
*.o is for bash, %.o is for make

Cygwin - Makefile-error: recipe for target `main.o' failed

I am currently failing to write a good makefile and don't know the reason why.. -.-
This is my main.c:
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("MEEEEEP");
return (0);
}
This is my makefile:
# make SYSTEM= OS= ENVIRONMENT=
# Binaries to use
ifeq ($(ENVIRONMENT),MINGW)
CXX = i686-pc-mingw32-g++
else
CXX = g++
endif
REMOVE = rm -vf
RC = windres
EXE = .exe
#############################################################
# Info
ifeq ($(CXX),g++)
INFO_CXX = g++ -dumpversion; g++ -dumpmachine
endif
#############################################################
# Flags
DEBUG = -DDEBUG -g
OPTIMIZATION = -O2 #-Winline -finline-functions
CFLAGS = -Wall -Wextra -W -static $(DEBUG) $(OPTIMIZATION) -D$(SYSTEM) -D$(OS) -D$(ENVIRONMENT) $(PRGFLAGS)
ifeq ($(SYSTEM),I686)
CFLAGS += -m32
ifeq ($(OS),WIN32)
CFLAGS += -D_WIN32
endif
ifeq ($(ENVIRONMENT),MINGW)
CFLAGS += -fexceptions
endif
endif
LFLAGS =
#############################################################
# Files
CFILES = main.c
OBJS = ${CFILES:.c=.o}
#############################################################
# Include
INCLUDES = -I.
#############################################################
# Library
LIBRARIES =
#############################################################
# Targets
.PHONY: all
all:
#echo == Standard build: make SYSTEM=I686 OS=WIN32 ENVIRONMENT=MINGW
#echo
#echo
make SYSTEM=I686 OS=WIN32 ENVIRONMENT=MINGW gyro
#############################################################
# Implicit rules and filename extensions...
.SUFFIXES: .h .o .c
.c.o: %.h
#echo Compiling $< for $(SYSTEM) $(OS) $(ENVIRONMENT) ...
#echo MEEP
$(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $#
#echo MEEP2
#############################################################
# Target rules
gyro: $(OBJS)
#echo Building software for $(SYSTEM) ...
#echo
$(CXX) $(CFLAGS) $(LFLAGS) -o $#$(EXE) $(OBJS) $(LIBRARIES)
#############################################################
# Clean
.PHONY: clean
clean:
$(REMOVE) $(OBJS)
#############################################################
# Info
.PHONY: info
info:
#echo
#echo Information about C++ Compiler/Linker:
#echo
$(INFO_CXX)
When i type in make gyro,
i receive the output:
Compiling main.c for Windows_NT ...
MEEP
g++ -Wall -Wextra -W -static -DDEBUG -g -O2 -D -DWindows_NT -D -I. -c main.c -o main.o
makeNew.mak:83: recipe for target `main.o' failed
make: *** [main.o] Error 1
But Line number 83 is behind .c.o: %.h. And i don’t understand why.
Does anyone have a solution for me?
You see the two empty -D entries in the g++ command line? They're causing the problem. You must have values in the -D items e.g. -DWIN32
if you're insistent on using something like -D$(SYSTEM) -D$(ENVIRONMENT) then you can use something like:
SYSTEM ?= generic
ENVIRONMENT ?= generic
in the makefile which gives them default values.
Your output looks to be missing the all important output:
<command-line>:0:1: error: macro names must be identifiers
<command-line>:0:1: error: macro names must be identifiers
just to clarify, what actually got sent to g++ was -D -DWindows_NT, i.e. define a preprocessor macro called -DWindows_NT; which is of course not a valid identifier (similarly for -D -I.)

Resources