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.)
Related
I am compiling a program meant for the Unix-like OS to windows with msys2 using GCC
program source: repository of the program,
then this error popped up,
src/gfx/../util/time.h:12:27: error: 'TIME_UTC' undeclared (first use in this function); did you mean 'TIME_H'?
12 | timespec_get(&ts, TIME_UTC);
| ^~~~~~~~
| TIME_H
How do I fix this
also here is my MakeFile:
UNAME_S = $(shell uname -s)
CC = gcc
CFLAGS = -std=c11 -O3 -g -Wall -Wextra -Wpedantic -Wstrict-aliasing -fdiagnostics-color
CFLAGS += -Wno-pointer-arith -Wno-newline-eof -Wno-unused-parameter -Wno-gnu-statement-expression
CFLAGS += -Wno-gnu-compound-literal-initializer -Wno-gnu-zero-variadic-macro-arguments
CFLAGS += -Ilib/cglm/include -Ilib/glad/include -Ilib/glfw/include -Ilib/stb -Ilib/noise -lrt # -fbracket-depth=1024
LDFLAGS = lib/glad/src/glad.o lib/cglm/libcglm.a lib/glfw/src/libglfw3.a lib/noise/libnoise.a -lm
# GLFW required frameworks on OSX
ifeq ($(UNAME_S), Darwin)
LDFLAGS += -framework OpenGL -framework IOKit -framework CoreVideo -framework Cocoa
endif
ifeq ($(UNAME_S), Linux)
LDFLAGS += -ldl -lpthread
endif
SRC = $(wildcard src/**/*.c) $(wildcard src/*.c) $(wildcard src/**/**/*.c) $(wildcard src/**/**/**/*.c)
OBJ = $(SRC:.c=.o)
BIN = bin
.PHONY: all clean
all: dirs libs game
libs:
cd lib/cglm && cmake . -DCGLM_STATIC=ON -G "MSYS Makefiles" -DCMAKE_C_COMPILER=C:/msys64/mingw64/bin/gcc.exe -DCMAKE_CXX_COMPILER=C:/msys64/mingw64/bin/g++.exe && make
cd lib/glad && $(CC) -o src/glad.o -Iinclude -c src/glad.c
echo GLFW
cd lib/glfw && cmake -G "MSYS Makefiles" -DCMAKE_C_COMPILER=C:/msys64/mingw64/bin/gcc.exe -DCMAKE_CXX_COMPILER=C:/msys64/mingw64/bin/g++.exe . && make
cd lib/noise && make
echo noice
dirs:
mkdir -p ./$(BIN)
run: all
$(BIN)/game
game: $(OBJ)
$(CC) -o $(BIN)/game $^ $(LDFLAGS)
%.o: %.c
$(CC) -o $# -c $< $(CFLAGS)
clean:
rm -rf $(BIN) $(OBJ)
this is the modified time.h file
#ifndef TIME_H
#define TIME_H
#define HAVE_STRUCT_TIMESPEC
#define _POSIX_C_SOURCE 199309L
#include <time.h>
#include <stdio.h>
#define TIME_UTC time(NULL)
#define NS_PER_SECOND (1000000000)
#define NS_PER_MS (1000000)
int NOW(){
struct timespec ts;
timespec_get(&ts, TIME_UTC);
return ((ts.tv_sec * NS_PER_SECOND) + ts.tv_nsec);
}
#endif
Enviorment:
get windows 10
install msys2
install boost from the zip onto the C++ include(not related but, just a heads up)
run the mingw64 shell
git clone the included link
edit the time.h in the src/util with the code
edit the Makefile with the posted one
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.
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´m trying to create a Makefile that build a lib and compile the file. My problem is that depending on the %.c file I need to compile with different lib.
The SRC_MLX need the $(LFLAGS) and the SRC don´t. So the gcc compiler don´t allow me to compile the SRC with the LFLAGS. That´s the reason I need to separate.
I´ve tried this way:
SRCS = $(DIR_SRC)/ft_utils.c \
$(DIR_SRC)/ft_adt.c \
$(DIR_SRC)/ft_circle.c \
$(DIR_SRC)/ft_line.c \
$(DIR_SRC)/ft_trgb.c \
$(DIR_SRC)/ft_quadrilateral.c \
$(DIR_SRC)/ft_player.c \
$(DIR_SRC)/ft_color.c
SRCS_MLX = $(DIR_SRC)/win_update.c \
$(DIR_SRC)/ft_vars.c \
$(DIR_SRC)/ft_image.c \
$(DIR_SRC)/ft_map.c
$(NAME): $(LIB_NAME)
$(CC) $(CFLAGS) main.c -I. -I$(DIR_MLX) -L$(DIR_MLX) -L. $(LFLAGS) $(LIB_FLAG) -o $# -g
$(DIR_OBJ)/$(OBJ).o: $(DIR_SRC)/$(SRCS).c
mkdir -p $(DIR_OBJ)
$(CC) $(CFLAGS) -c $< -I. -I$(DIR_MLX) -o $#
$(DIR_OBJ)/$(OBJ_MLX).o: $(DIR_SRC)/$(SRCS_MLX).c
mkdir -p $(DIR_OBJ)
$(CC) $(CFLAGS) -c $< -I. -I$(DIR_MLX) -L$(DIR_MLX) $(LFLAGS) -o $#
$(LIB_NAME): $(OBJ_MLX) LIB_OBJ
$(AR) $(LIB_NAME) $(OBJ)
ranlib $(LIB_NAME)
LIB_OBJ: $(OBJ)
$(AR) $(LIB_NAME) $(OBJ)
In the example above I´ve tried to create a lib with one kind of files and after that create a lib with the first lib with the others files. But I keep getting this error:
Makefile:41: warning: overriding recipe for target '.objs/'
Makefile:37: warning: ignoring old recipe for target '.objs/'
make: Warning: File 'Makefile' has modification time 454 s in the future
rm -f .objs/ft_utils.o .objs/ft_adt.o .objs/ft_circle.o .objs/ft_line.o .objs/ft_trgb.o .objs/ft_quadrilateral.o .objs/ft_player.o .objs/ft_color.o
rm -f libcub3d.a
rm -f main
make: *** No rule to make target 'src/src/win_update.c', needed by '.objs/win_update.o'. Stop.
How can I compile this objects with different lib?
P.S. Those are my variables:
DIR_OBJ = .objs
DIR_SRC = src
DIR_LIB = lib
DIR_MLX = ./minilibx-linux
NAME = main
LIB_NAME = libcub3d.a
OBJ = $(patsubst $(DIR_SRC)/%.c, $(DIR_OBJ)/%.o, $(SRCS))
OBJ_MLX = $(patsubst $(DIR_SRC)/%.c, $(DIR_OBJ)/%.o, $(SRCS_MLX))
CC = clang
CFLAGS = -Wall -Werror -Wextra
LFLAGS = -lmlx -lm -lX11 -lXext -lbsd
LIB_FLAG = -lcub3d
AR = ar -rc
RM = rm -f
These lines are definitely not right:
$(DIR_OBJ)/$(OBJ).o: $(DIR_SRC)/$(SRCS).c
...
$(DIR_OBJ)/$(OBJ_MLX).o: $(DIR_SRC)/$(SRCS_MLX).c
along with the way you've defined SRCS and SRCS_MLX and these:
OBJ = $(patsubst $(DIR_SRC)/%.c, $(DIR_OBJ)/%.o, $(SRCS))
OBJ_MLX = $(patsubst $(DIR_SRC)/%.c, $(DIR_OBJ)/%.o, $(SRCS_MLX))
Just expand the variables in the rule in your head, or else ask make to expand it for you with the info function, and you'll see it's definitely not right:
$(info output is '$(DIR_OBJ)/$(OBJ).o: $(DIR_SRC)/$(SRCS).c')
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.