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.
Related
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.
I need some help writing a GNU makefile. I have a C program "main.c", which is dependent on the value of "CONSTANT" defined in the file "constants.h".
"main.c"
#include <stdio.h>
#include "constants.h"
void work(void)
{
int array[CONSTANT];
for (int i = 0; i < CONSTANT; i++) {
printf("%d\n", i);
}
}
int main(int argc, char const* argv[])
{
printf("constant=%d\n", CONSTANT);
work();
return 0;
}
"constant.h"
#ifndef CONSTANTS_H
#define CONSTANTS_H
#define CONSTANT 4
#endif
What I'm trying to do here is to compile the program with different values for "CONSTANT". For example, "out1" is compiled with "CONSTANT=1" and with "make all", I should be able to produce all the variants ("out1", "out2" and "out4").
The problem is that "a.o" required by "main.c" also depends on the value of "CONSTANT". So "a.o" must be compiled after "sed%". However, as far as I understand, there is no way in "make" to force orders in dependencies (I guess this is the whole point of using makefiles).
What is the recommended way to address this situation?
"Makefile"
CC= gcc
CFLAGS = -std=c99 -Wall
CONSTANTS = 1 2 4
targets = $(addprefix out, $(CONSTANTS))
seds = $(addprefix sed, $(CONSTANTS))
.PHONY: $(seds)
$(seds): sed%:
sed -i 's/define CONSTANT [0-9]*/define CONSTANT $*/g' constants.h
$(targets): out%: main.c sed% a.o
$(CC) $(CFLAGS) $< a.o -o $#
a.o: a.c constant.h
$(CC) $(CFLAGS) $< a.o -o $#
.PHONY: all
all : $(targets)
Note that I'm aware that I can rewrite "main.c" so that it takes a parameter from the comman line. In practice, many other files other than "main.c" depend on "CONSTANT", so I want to avoid rewriting all these files. I'm also aware that I can do something like "gcc -DCONSTANT=n main.c", but every file dependent on "CONSTANT" must be recompiled as well.
Related Questions
How to specify Makefile target building order without put any physical dependencies?
Force order of dependencies in a Makefile
I'm ... aware that I can do something like "gcc -DCONSTANT=n main.c",
but every file dependent on "CONSTANT" must be recompiled as well.
This needn't be a hindrance if you have your makefile generate
the correct -DCONSTANT=n and distinct object file in every compilation recipe.
Here's an illustration:
constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
#ifndef CONSTANT
#define CONSTANT 4
#endif
#endif
foo.c
#include "constants.h"
int foo = CONSTANT;
main.c
#include <stdio.h>
#include "constants.h"
extern int foo;
int main()
{
printf("%d\n",CONSTANT + foo);
return 0;
}
Makefile
CC := gcc
CFLAGS := -std=c99 -Wall
CONSTANTS = 1 2 4
TARGETS = $(addprefix out, $(CONSTANTS))
SRCS := main.c foo.c
define compile =
$(basename $(1))$(2).o: $(1) constants.h
$$(CC) -c -DCONSTANT=$(2) $$(CFLAGS) $$< -o $$#
endef
.PHONY: all clean
all : $(TARGETS)
$(foreach src,$(SRCS),\
$(foreach const,$(CONSTANTS),$(eval $(call compile,$(src),$(const)))))
out%: main%.o foo%.o
$(CC) $^ -o $#
clean:
rm -f $(TARGETS) *.o
This runs like:
$ make
gcc -c -DCONSTANT=1 -std=c99 -Wall main.c -o main1.o
gcc -c -DCONSTANT=1 -std=c99 -Wall foo.c -o foo1.o
gcc main1.o foo1.o -o out1
gcc -c -DCONSTANT=2 -std=c99 -Wall main.c -o main2.o
gcc -c -DCONSTANT=2 -std=c99 -Wall foo.c -o foo2.o
gcc main2.o foo2.o -o out2
gcc -c -DCONSTANT=4 -std=c99 -Wall main.c -o main4.o
gcc -c -DCONSTANT=4 -std=c99 -Wall foo.c -o foo4.o
gcc main4.o foo4.o -o out4
And the resulting programs run like:
$ for i in 1 2 4; do ./out$i; done
2
4
8
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.
I am trying to use matlab libmat.dll in a C application. To compile my C application I use MinGW, for now I use matlab exemple "matcreate.c" and try to compile it, so the projects consist of only one file : main.c .
Here is the makefile I use :
MATINCLUDE = "C:\Program Files\MATLAB\R2010a\extern\include"
MATLIBRARY = "C:\Program Files\MATLAB\R2010a\bin\win64"
#
CC = gcc
LD = gcc
CFLAGS = -O3 -Wall
LFLAGS = -Wall -O3
LIBS = -I$(MATINCLUDE) -L$(MATLIBRARY)
#
PROG = matTest
LISTEOBJ = \
main.o
.c.o :
$(CC) -c $(CFLAGS) $(LIBS) -o $# $<
all : $(PROG)
$(PROG) : $(LISTEOBJ)
$(LD) -o $(PROG) $(LFLAGS) $(LISTEOBJ) $(LIBS)
clean :
rm -f *.obj
Here is what I get in the console
E:\Users\Desk\Dropbox\matTest>make
gcc -c -O3 -Wall -I"C:\Program Files\MATLAB\R2010a\extern\include" -L"C:\Pr
ogram Files\MATLAB\R2010a\bin\win64" -o main.o main.c
gcc -o Hello_world -Wall -O3 main.o -I"C:\Program Files\MATLAB\R2010a\extern\i
nclude" -L"C:\Program Files\MATLAB\R2010a\bin\win64"
main.o:main.c:(.text.startup+0x48): undefined reference to `matOpen'
main.o:main.c:(.text.startup+0x6e): undefined reference to `mxCreateDoubleMatrix
_730'
e:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: main.o: ba
d reloc address 0x6e in section `.text.startup'
e:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
make: *** [Hello_world] Error 1
Why do I have "undefined reference to matOpen'" and "undefined reference to mxCreateDoubleMatrix" ?? those function are declared in mat.h. and I added #include "mat.h" to the begining of main.c
thank you
Looks like you have included the path to the matlab library, but not the library itself. You need to add a -l<libraryname> to your link line.
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.)