Makefile works on macos but doesn't on linux - c

I have a c program with a makefile that works perfectly on macos but when i try yo run it on linux it gives me a bunch of errors
gcc ./libft/libft.a srcs/main.o -o push_swap
/usr/bin/ld: srcs/main.o: in function `sa':
main.c:(.text+0x97): undefined reference to `ft_putstr'
/usr/bin/ld: srcs/main.o: in function `sb':
main.c:(.text+0x149): undefined reference to `ft_putstr'
/usr/bin/ld: srcs/main.o: in function `ss':
main.c:(.text+0x1eb): undefined reference to `ft_putstr'
/usr/bin/ld: srcs/main.o: in function `pa':
main.c:(.text+0x236): undefined reference to `ft_putstr'
/usr/bin/ld: srcs/main.o: in function `pb':
main.c:(.text+0x2df): undefined reference to `ft_putstr'
/usr/bin/ld: main.c:(.text+0x327): undefined reference to `ft_putnbr_fd'
/usr/bin/ld: srcs/main.o: in function `printstack':
main.c:(.text+0x3b3): undefined reference to `ft_putnbr_fd'
/usr/bin/ld: main.c:(.text+0x3bd): undefined reference to `ft_putchar'
/usr/bin/ld: main.c:(.text+0x3df): undefined reference to `ft_putnbr_fd'
/usr/bin/ld: main.c:(.text+0x3e9): undefined reference to `ft_putchar'
/usr/bin/ld: main.c:(.text+0x404): undefined reference to `ft_putstr'
/usr/bin/ld: main.c:(.text+0x413): undefined reference to `ft_putstr'
/usr/bin/ld: srcs/main.o: in function `main':
main.c:(.text+0x4a9): undefined reference to `ft_atoi'
collect2: error: ld returned 1 exit status
make: *** [Makefile:23: push_swap] Error 1
I believe the cause is the Makefile because it can't find the function that the makefile is suppose to compile too but im not sure about that.
Anyways here is my makefile:
NAME = push_swap
LIBFT_PATH = libft
LIBFT = libft.a
SRC_FILES = main.c
SRC_DIR = srcs/
SRC = ${addprefix ${SRC_DIR}, ${SRC_FILES}}
OBJ = ${SRC:.c=.o}
CC = gcc
CFLAGS = -Wall -Wextra -Werror -pg -g
AR = ar -rcs
%.o: %.c
${CC} -c -Imlx -c $< -o $#
all: ${NAME}
libft:
#MAKE -sC ${LIBFT_PATH}
${NAME}: libft ${OBJ}
${CC} ${LIBFT_PATH}/${LIBFT} ${OBJ} -o ${NAME}
clean:
rm -f ${OBJ}
${MAKE} -C ${LIBFT_PATH} clean
fclean: clean
rm -f ${NAME}
${MAKE} -C ${LIBFT_PATH} fclean
re: fclean all
.PHONY: all clean flcean re

Related

How to make object files into another directory

There are my main dir structure
push_swap
|helpers/ -> *.c files
|format_parsing/ -> *.c files
|push_swaph/ -> push_swap.c, sort_a.c
|libft/ -> *.c files, libft.h
|ft_printf/ -> *.c files, libftprintf.h
|includes/ -> header_push_swap.h
______________________________________________________
In which method in a makefile i can create an object files from all *.c files and put them into another directory like objs/?
NAME = push_swap
LIBFT = libft.a
PRINTF = libftprintf.a
HEADER = includes/header_push_swap.h
LIBFTHEADER = libft/libft.h
PRINTFHEADER = ft_printf/includes/header.h
OBJS_DIR = objs/
SRC = $(wildcard ./format_parsing/*.c) \
$(wildcard ./helpers/*.c) \
$(wildcard ./push_swaph/*.c)
OBJS = $(SRC:.c=.o)
CC = gcc
FLAGS = -Wall -Wextra -Werror
all: $(NAME)
$(NAME) : $(LIBFTHEADER) $(PRINTFHEADER) $(LIBFT) $(PRINTF) $(OBJS)
#$(CC) $(FLAGS) $(OBJS) -o $(NAME)
$(LIBFT) :
#cd ./libft && make
$(PRINTF) :
#cd ./ft_printf/ && make
clean :
#rm -rf $(OBJS_DIR)
#rm -f $(OBJS)
fclean : clean
#rm -rf $(OBJS)
#cd ./libft/ && make fclean
#cd ./ft_printf/ && make fclean
I made this makefile but it is still creates an object files inside of my C files directories.
And a second problem when i want to make $(NAME) it compiles all files and in last case it didn't found the references of functions in libft and ft_printf. But i put the header files also $(LIBFTHEADER) $(PRINTFHEADER).
/usr/bin/ld: ./format_parsing/parsing.o: in function `stack_nums_counter':
parsing.c:(.text+0x3b): undefined reference to `ft_strlen'
/usr/bin/ld: ./helpers/actions1.o: in function `pa':
actions1.c:(.text+0x60): undefined reference to `ft_printf'
/usr/bin/ld: ./helpers/actions1.o: in function `pb':
actions1.c:(.text+0xc9): undefined reference to `ft_printf'
/usr/bin/ld: ./helpers/actions1.o: in function `ra':
actions1.c:(.text+0x156): undefined reference to `ft_printf'
/usr/bin/ld: ./helpers/actions1.o: in function `rb':
actions1.c:(.text+0x1e3): undefined reference to `ft_printf'
/usr/bin/ld: ./helpers/actions2.o: in function `rr':
actions2.c:(.text+0x35): undefined reference to `ft_printf'
/usr/bin/ld: ./helpers/actions2.o:actions2.c:(.text+0x8e): more undefined references to `ft_printf' follow
/usr/bin/ld: ./helpers/helpers.o: in function `__store__':
helpers.c:(.text+0x4c): undefined reference to `ft_split'
/usr/bin/ld: helpers.c:(.text+0xd9): undefined reference to `ft_atoi'
/usr/bin/ld: ./helpers/helpers.o: in function `__check__collection':
helpers.c:(.text+0x342): undefined reference to `ft_printf'
collect2: error: ld returned 1 exit status
make: *** [Makefile:18: push_swap] Error 1
The easiest way is to specify the targets with directories and use VPATH to find the source files. There is no way to test your supplied Makefile you didn't supply a minimal reproducible example so here is a skeleton instead:
Makefile:
.PHONY: all clean
NAME = push_swap
VPATH = src
all: $(NAME)
clean:
rm -f objs/* $(MAME)
$(NAME): objs/push_swap.o
objs/%.o: %.c
$(CC) -o $# $<
and create a couple of directories:
mkdir objs src
and an example src file:
#include <stdio.h>
int main(void) {
printf("ok\n");
return 1;
}
You can now run make and the object files end up objs, and in this case the binary in the top-level directory:
$ ls objs/* push_swap
ls: cannot access 'objs/*': No such file or directory
ls: cannot access 'push_swap': No such file or directory
$ make
$ ls objs/* push_swap
objs/push_swap.o
push_swap

Makefile: crt1.o: In function `_start': (.text+0x20): undefined reference to `main'

When I compile my makefile, I encounter this problem:
gcc parser.tab.o -o parser.tab
/usr/lib/../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
parser.tab.o: In function `yyparse':
parser.tab.c:(.text+0x2c3): undefined reference to `yylex'
parser.tab.c:(.text+0x3f5): undefined reference to `execute'
parser.tab.c:(.text+0x82b): undefined reference to `yyerror'
parser.tab.c:(.text+0x947): undefined reference to `yyerror'
collect2: ld returned 1 exit status
make: *** [parser.tab] Error 1
Here is my make file:
CC = gcc
CFLAGS = -lreadline
PROGS = d8sh parser.tab executor lexer
all: $(PROGS)
clean:
rm -f *.o $(PROGS) *.tmp
d8sh: d8sh.o
d8sh.o: executor.h lexer.h
executor.o: command.h
lexer.o: parser.tab.h
parser.tab.o: command.h
Only d8sh.c has a main function. Can anyone figure out what the problem is?

GCC: Undefined reference (Makefile issue)

Trying to compile these files with gcc under Makefile rules. Running gcc / make on cygwin.
Here's the makefile:
CC = gcc
CFLAGS = -g -O2 -Wall -std=c99
OBJECTS = simulation.o element.o
simulation.exe : $(OBJECTS)
$(CC) $(CFLAGS) -o simulation.exe $(OBJECTS)
simulation.o : file_priorite.o element.o
$(CC) $(CFLAGS) -c simulation.c
file_priorite.o: file_priorite.h element.o
$(CC) $(CFLAGS) -c file_priorite.c
element.o : element.h
$(CC) $(CFLAGS) -c element.c
clean:
rm -f *.o simulation.exe
And getting these errors:
CLEAN SUCCESSFUL (total time: 52ms)
gcc -g -O2 -Wall -std=c99 -c element.c
gcc -g -O2 -Wall -std=c99 -c simulation.c
gcc -g -O2 -Wall -std=c99 -o simulation.exe simulation.o element.o
simulation.o: In function `main':
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:11: undefined reference to `construire'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:11:(.text.startup+0x17): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `construire'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:20: undefined reference to `ajouter_element'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:20:(.text.startup+0x69): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `ajouter_element'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:23: undefined reference to `consommer_element'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:23:(.text.startup+0x76): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `consommer_element'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:23: undefined reference to `consommer_element'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:23:(.text.startup+0x86): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `consommer_element'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:25: undefined reference to `obtenir_taille'
/cygdrive/d/Dropbox/UQAM AUTOMNE 2014/INF7330/lab10/lab10/simulation.c:25:(.text.startup+0x96): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `obtenir_taille'
collect2: error: ld returned 1 exit status
Makefile:6: recipe for target 'simulation.exe' failed
make: *** [simulation.exe] Error 1
The problematic functions are all implemented in file_priorite.c.
Any idea?
You need to have
OBJECTS = simulation.o element.o file_priorite.o
Notice that your build log doesn't include file_priorite in any form - that means you're not building/linking against it. Add file_priorite.o to your OBJECTS line.

compiling C project with makefile

I have a directory called reliability:
ls reliability
analyze.c appl.sh constr.c creer.c greedys.c Makefile
Now I want to install and compile it:
First of all the content of Makefile is:
CC = gcc
CFLAGS = -g -Wall -pthread
SRCS = constr.c creer.c analyze.c greedys.c
PROG = constr creer analyze greedys
all: $(PROG)
constr: constr.c
$(CC) $(CFLAGS) -o constr constr.c
creer: creer.c
$(CC) $(CFLAGS) -o creer creer.c
analyze: analyze.c
$(CC) $(CFLAGS) -o analyze analyze.c
greedys: greedys.c
$(CC) $(CFLAGS) -o greedys greedys.c
clean:
rm -f $(PROG)
When I start compiling it:
make -f Makefile
The error occurs:
gcc -g -Wall -pthread -o constr constr.c
/tmp/cca4NKQl.o: In function `main':
/home/t1/Desktop/reliability/constr.c:89: undefined reference to `exp'
/home/t1/Desktop/reliability/constr.c:89: undefined reference to `exp'
/home/t1/Desktop/reliability/constr.c:89: undefined reference to `pow'
/home/t1/Desktop/reliability/constr.c:89: undefined reference to `log'
collect2: ld returned 1 exit status
make: *** [constr] Error 1
I pass these errors by adding -lm to gcc,
Even when I change CFLAGS from Makefile to CFLAGS = -g -Wall -pthread -lm there is also that error.
My question:
Should I compile all .c programs separately now?
Thank you I really need help,
UPDATE
When I compile creer.c there is an error:
creer.c:39:10: warning: unused variable ‘val2’ [-Wunused-variable] creer.c:38:7:
warning: unused variable ‘val’ [-Wunused-variable]
Part of creer.c is:
void values(){
int val;
double val2;
FILE *fp;
fp = fopen("instances","r");
fseek(fp,0,SEEK_SET);
if(fscanf(fp,"%d",&p)==EOF){printf("EOF\n");}
if(fscanf(fp,"%d",&m)==EOF){printf("EOF\n");}
if(fscanf(fp,"%d",&K)==EOF){printf("EOF\n");}
if(fscanf(fp,"%lf",&lambda_com)==EOF){printf("EOF\n");}
if(fscanf(fp,"%d",&num_inst)==EOF){printf("EOF\n");}
if(fscanf(fp,"%d",&boundl)==EOF){printf("EOF\n");}
if(fscanf(fp,"%d",&boundp)==EOF){printf("EOF\n");}
fclose(fp);
}
You need to add that linker option -lm to the end of your command line.
Do something like this:
LDFLAGS=-lm
...
constr: constr.c
$(CC) $(CFLAGS) -o constr constr.c $(LDFLAGS)
Or you could define a pattern rule:
%: %.c
$(CC) $(CFLAGS) -o $# $< $(LDFLAGS)
and use it to replace all those rules for constr, creer, and so on.

Getting compilation errors whn included a third party library

#include<stdio.h>
#include "flite.h"
cst_voice *register_cmu_us_kal();
int main()
{
cst_voice *v;
cst_wave *w;
char *text = "Hello world programming";
//Initialising the flite variables used
flite_init();
w = new_wave();
v = register_cmu_us_kal(NULL);
flite_text_to_speech(text,v,"hello_wave");
if(cst_wave_load_riff(w,"hello_wave")!=CST_OK_FORMAT){
printf("\nCompare_wave:Can read file or wrong format!\n");
}
else{
play_wave(w);
}
return 0;
}
Makefile
all:compile \
./compile
compile:eg1.o
gcc -o $# eg1.o
eg1.o:eg1.c $(LIBS_DIR) $(INC_DIR) $(LIBS)
gcc -c $<
LIBS_DIR = -L /home/b/flite-1.4-release/build/i386-linux-gnu/lib
INC_DIR = -I /home/b/flite-1.4-relase/include
LIBS = -lflite_cmu_us_slt -lflite -lflite_cmulex -lflite_cmu_time_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_cmu_usenglish
INCLUDE:
clean:
rm -f *.o
I tried by giving he library and header file paths as LIBS_DIR = ../build/i386-linux-gnu/lib and INC_DIR = ../include
I tried the folowing c program by including a third party library. This program an a makefile is located in b\flite-1.4-release\Learnin_though_example folder. Th flite libraries are located in b\flite-1.4-release\build\i386-linux-gnu\lib and the header files are in b\flite-1.4-release\include .
I assume that i have given the makefile th correct path to search for the files. But its not identifyin it and i'm gettin an error as,
make clean all
rm -f *.o
gcc -c eg1.c
eg1.c:2:19: error: flite.h: No such file or directory
eg1.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
eg1.c: In function ‘main’:
eg1.c:6: error: ‘cst_voice’ undeclared (first use in this function)
eg1.c:6: error: (Each undeclared identifier is reported only once
eg1.c:6: error: for each function it appears in.)
eg1.c:6: error: ‘v’ undeclared (first use in this function)
eg1.c:7: error: ‘cst_wave’ undeclared (first use in this function)
eg1.c:7: error: ‘w’ undeclared (first use in this function)
eg1.c:17: error: ‘CST_OK_FORMAT’ undeclared (first use in this function)
make: *** [eg1.o] Error 1
Please help me understand what is the mistake i'm doing
EDITED:
I modiied th makefile as per matt's guidance:
all:compile
compile:eg1.o
gcc $(INC_DIR) $(LIB_DIR) -o $# $^ $(LIBS)
eg1.o:eg1.c
gcc $(INC_DIR) -o $# -c $^
LIBS_DIR = -L../build/i386-linux-gnu/lib
INC_DIR = -I../include
LIBS = -lflite -lflite_cmulex -lflite_cmu_time_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_cmu_usenglish -lflite_cmu_us_slt
clean:
rm -f *.o
but i'm getting ifferent error whn compiled with the command "make clean all" as,
rm -f *.o
gcc -I../include -o eg1.o -c eg1.c
gcc -I../include -o compile eg1.o -lflite -lflite_cmulex -lflite_cmu_time_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_cmu_usenglish -lflite_cmu_us_slt
/usr/bin/ld: cannot find -lflite
collect2: ld returned 1 exit status
make: *** [compile] Error 1
EDITED:
rm -f *.o
gcc -I../include -o eg1.o -c eg1.c
gcc -I../include -L../build/i386-linux-gnu/lib -o compile eg1.o -lflite -lflite_cmulex -lflite_cmu_time_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_usenglish -lflite_cmu_us_slt -lflite_cmu_us_rms
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `sin'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `exp'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `sqrt'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `log'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `fmod'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `pow'
Your makefile is, I'm afraid to say, completely broken.
The basic Makefile syntax is:
target: pre-requisite(s)
<tab>Stuff to do to build target from pre-reqs (if required)
So this is wrong, eg1.o can't be a pre-requisite for building itself.
compile:eg1.o
gcc -o eg1.o
You should have:
eg1.o: eg1.c
gcc $(INC_DIR) -o $# -c $^
($# is the target, $^ all the pre-reqs.)
Then you can:
myexe: eg1.o
gcc $(INC_DIR) $(LIBS_DIR) -o $# $^ $(LIBS)
This will produce myexe from eg1.o. And your all rule should be all: myexe, with no recipe (no commands), and at the top as you have it.
Then you've got your include directories and library directories mixed up. -I is for include paths, -L for library paths.
Place your variable definitions before the rules, that's more common/usual. And don't put a space between -L/-I and the path that follows it.
The include directories to search is specified by -I flag, not -L.
Change:
LIBS_DIR = -I /home/b/flite-1.4-release/build/i386-linux-gnu/lib
INC_DIR = -L /home/b/flite-1.4-relase/include
to:
LIBS_DIR = -L /home/b/flite-1.4-release/build/i386-linux-gnu/lib
INC_DIR = -I /home/b/flite-1.4-relase/include

Resources