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
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?
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.
I am trying to compile the files csapp.c and csapp.h on a Ubuntu 12 operating system. I think I am missing some header file or some option but I do not know which one. Or perhaps I do not have the latest gcc. Here is my Makefile:
SRC = tiny.c
LIB = csapp.c
INC = csapp.h
ALL = $(SRC) $(LIB) $(INC)
webServer-gcc : $(ALL)
gcc -std=c99 -O2 -lpthread -lrt -o server $(ALL)
Here is my error output from the terminal:
csapp.c: In function ‘Kill’:
csapp.c:81:5: warning: implicit declaration of function ‘kill’ [-Wimplicit-function- declaration]
csapp.c: In function ‘Signal’:
csapp.c:124:22: error: storage size of ‘action’ isn’t known
csapp.c:124:30: error: storage size of ‘old_action’ isn’t known
csapp.c:127:5: warning: implicit declaration of function ‘sigemptyset’ [-Wimplicit- function-declaration]
csapp.c:128:23: error: ‘SA_RESTART’ undeclared (first use in this function)
csapp.c:128:23: note: each undeclared identifier is reported only once for each function it appears in
csapp.c:130:5: warning: implicit declaration of function ‘sigaction’ [-Wimplicit-function-declaration]
csapp.c: In function ‘Sigprocmask’:
csapp.c:138:5: warning: implicit declaration of function ‘sigprocmask’ [-Wimplicit-function-declaration]
csapp.c: In function ‘Sigfillset’:
csapp.c:152:5: warning: implicit declaration of function ‘sigfillset’ [-Wimplicit-function-declaration]
csapp.c: In function ‘Sigaddset’:
csapp.c:159:5: warning: implicit declaration of function ‘sigaddset’ [-Wimplicit-function-declaration]
csapp.c: In function ‘Sigdelset’:
csapp.c:166:5: warning: implicit declaration of function ‘sigdelset’ [-Wimplicit-function-declaration]
csapp.c: In function ‘Sigismember’:
csapp.c:174:5: warning: implicit declaration of function ‘sigismember’ [-Wimplicit-function-declaration]
csapp.c: In function ‘Fdopen’:
csapp.c:326:5: warning: implicit declaration of function ‘fdopen’ [-Wimplicit-function-declaration]
csapp.c:326:13: warning: assignment makes pointer from integer without a cast [enabled by default]
csapp.c: In function ‘open_clientfd’:
csapp.c:741:5: warning: implicit declaration of function ‘bzero’ [-Wimplicit-function-declaration]
csapp.c:743:5: warning: implicit declaration of function ‘bcopy’ [-Wimplicit-function-declaration]
make: *** [webServer-gcc] Error 1
Here is my last remaining error inside my main function:
tiny.c:23:24: error: storage size of ‘clientaddr’ isn’t known
here is line 23 of my code:
struct socketaddr_in clientaddr;
Use -std=gnu99, see this link.
and I write a new Makefile for you, hope this can help you.
CC = gcc
LIBS = -lpthread -lrt
INCS = -I./
CCFLAGS = -std=gnu99 -O2
all: server
server: csapp.o tiny.c
$(CC) $(CCFLAGS) $^ -o $# $(LIBS) $(INCS)
csapp.o: csapp.c csapp.h
$(CC) $(CCFLAGS) -c $< -o $# $(INCS)
clean:
rm -f server csapp.o
I'm trying to compile a program using mixed C and Ocaml sources, with the main of the application in C calling some pieces of OCaml code.
All right, no problem here, It's seems to be a common operation, fully documented, easy to do with the standard Ocaml tools.
Let me explain a bit, this kind of compilation is divided in 4 steps : Caml compiling to Caml objects, then compiling the Caml to C objects, then compiling the C files, and lastly compiling all the C objects together and getting the executable.
The theory is, the Ocaml compiler will embed the Caml runtime, GC, and all its stuff automatically, and we just have to indicate if we use whichever the ocaml bytecode (referencing -lcamlrun) or the native binary (referencing -lasmrun).
So, it seems to be quite simple, let's do it. Steps 1, 2 and 3 went as expected, good!
Only the 4th step is problematic. Just take a look:
cc -o /home/thomas/Documents/projects/ocaml/GogoGame/bin/GogoPlayer.exe \
-L/usr/lib/ocaml -lcamlrun \
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o \
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o \
/home/thomas/Documents/projects/ocaml/GogoGame/src/interface.o \
/home/thomas/Documents/projects/ocaml/GogoGame/src/caml_func.oo
/home/thomas/Documents/projects/ocaml/GogoGame/src/interface.o: In function `main':
interface.c:(.text+0x0): multiple definition of `main'
/usr/lib/ocaml/libcamlrun.a(main.o):(.text+0x0): first defined here
/usr/lib/ocaml/libcamlrun.a(floats.o): In function `caml_exp_float':
(.text+0x488): undefined reference to `exp'
/usr/lib/ocaml/libcamlrun.a(floats.o): In function `caml_fmod_float':
(.text+0x4f9): undefined reference to `fmod'
/usr/lib/ocaml/libcamlrun.a(floats.o): In function `caml_log_float':
(…)
/usr/lib/ocaml/libcamlrun.a(unix.o): In function `caml_dlopen':
(.text+0x2ed): undefined reference to `dlopen'
/usr/lib/ocaml/libcamlrun.a(unix.o): In function `caml_dlclose':
(.text+0x300): undefined reference to `dlclose'
/usr/lib/ocaml/libcamlrun.a(unix.o): In function `caml_dlsym':
(.text+0x31b): undefined reference to `dlsym'
/usr/lib/ocaml/libcamlrun.a(unix.o): In function `caml_dlerror':
(.text+0x342): undefined reference to `dlerror'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o:(.data.rel+0xc): undefined reference to `caml_array_get_addr'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o:(.data.rel+0x10): undefined reference to `caml_array_get_float'
(...)
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o:(.data.rel+0x31c): undefined reference to `caml_lazy_make_forward'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o:(.data.rel+0x320): undefined reference to `caml_get_public_method'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o:(.data.rel+0x3ac): undefined reference to `caml_terminfo_setup'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o:(.data.rel+0x3b0): undefined reference to `caml_terminfo_backup'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o:(.data.rel+0x3b4): undefined reference to `caml_terminfo_standout'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init_camlcode.o:(.data.rel+0x3b8): undefined reference to `caml_terminfo_resume'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__f_1030':
(.text+0xc): undefined reference to `camlPervasives'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__f_1030':
(.text+0x11): undefined reference to `camlPervasives__output_string_1191'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__f_1030':
(.text+0x19): undefined reference to `camlPervasives__string_of_int_1130'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__f_1030':
(.text+0x20): undefined reference to `camlPervasives'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__f_1030':
(.text+0x25): undefined reference to `camlPervasives__output_string_1191'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__format_result_1034':
(.text+0x9c): undefined reference to `camlPrintf__sprintf_1414'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__entry':
(.text+0xe1): undefined reference to `caml_c_call'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__entry':
(.text+0xfb): undefined reference to `caml_c_call'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__entry':
(.text+0x115): undefined reference to `caml_c_call'
/home/thomas/Documents/projects/ocaml/GogoGame/src/init.o: In function `camlInit__f_1030':
(.text+0x32): undefined reference to `camlPervasives__print_newline_1276'
collect2: ld returned 1 exit status
make: *** [GogoPlayer] Error 1
IMHO, it seems that there is two errors:
Multiple definitions of main
The linker doesn't find the module Pervasive
I have really no idea how to fix that, maybe I have to link an other file.
Does someone have an idea ?
As asked, I put the code that give these errors. It will be quite simple because there is very little code, most of it was given in an example in the documentation.
init.ml
let f x = print_string "f is applied to "; print_int x; print_newline()
let rec fib n = if n < 2 then 1 else fib(n-1) + fib(n-2)
let format_result n = Printf.sprintf "Result is: %d\n"
let _ =
Callback.register "Arbitrary Name" f;
Callback.register "fib" fib;
Callback.register "format_result" format_result
caml_func.c
#include <stdio.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>
void call_caml_f(int x)
{
static value * closure_f = NULL;
if (closure_f == NULL) /* First time around, look up by name */
closure_f = caml_named_value("Arbitrary Name");
caml_callback(*closure_f, Val_int(x));
}
int fib(int n)
{
static value * fib_closure = NULL;
if (fib_closure == NULL) fib_closure = caml_named_value("fib");
return Int_val(caml_callback(*fib_closure, Val_int(n)));
}
char * format_result(int n)
{
static value * format_result_closure = NULL;
if (format_result_closure == NULL)
format_result_closure = caml_named_value("format_result");
return strdup(String_val(caml_callback(*format_result_closure, Val_int(n))));
/* We copy the C string returned by String_val to the C heap
so that it remains valid after garbage collection. */
}
interface.c
#include <stdio.h>
#include "caml_func.c"
#define BYTECODE
int main(int argc, char **argv)
{
#ifdef BYTECODE
caml_startup(argv);
#else
caml_main(argv);
#endif
/* Make sure that stdout is not block buffered. */
setbuf(stdout, NULL);
/* Process GTP commands. */
//gtp_main_loop(commands, stdin, NULL);
// CAML code here ?
return 0;
}
And this is all. Obviously, I skipped all the meaningless stuff as this simple example should work and does not. It must be my Makefile, which follows.
By the way, it's quite ugly. If you have proposals for this kind of application (Caml inside C), or refactoring suggestions, I'll take them all.
Makefile.ocaml
.PHONY: all clean mrproper
# RULES and EXEC are magically set in Makefile.magic
all: depend $(RULES) $(EXE)
#echo [DONE]
mli: $(CAML_ONLY:.ml=.mli)
ml-byte: $(CAML_ONLY:.ml=.cmo)
ml-called-byte: $(CAML_CALLED_BY_C:.ml=.$(OBJ))
ml-nativ: $(CAML_ONLY:.ml=.cmx)
ml-called-nativ: $(CAML_CALLED_BY_C:.ml=.$(OBJ))
c-wrapper: $(C_WRAPPERS:.c=.oo)
c-only: $(C_ONLY:.c=.o)
$(EXE):
$(CC) -o $(BIN)/$(EXE).exe \
$(FLAGS) \
-L$(OCAMLLIB) $(LINKED) -l$(RUNLIB) \
$(wildcard $(SRC)/*.$(OBJ)) $(wildcard $(SRC)/*.oo) # */
%.o: %.c
$(CC) $(FLAGS_C) -c $< -o $(SRC)/$(*F).o
%.mli: %.ml
$(OCAMLC) $(FLAGS_ML) -i $< > $(SRC)/$(*F).mli
%.cmi: %.mli
$(OCAMLC) $(FLAGS_ML) -c $< -o $(SRC)/$(*F).cmi
%.cmo: %.ml
$(CAMLC) $(FLAGS_ML) -c $< -o $(SRC)/$(*F).cmo
%.cmx: %.ml
$(CAMLOPT) $(FLAGSOPT) -c $< -o $(SRC)/$(*F).cmx
# native
%.o: %.ml
$(cd $(SRC))
$(OCAMLC) -output-obj -o $(*F)_camlcode.o \
$(FLAGS_MLC) \
$<
# bytecode
%.ob: %.ml
$(cd $(SRC))
$(OCAMLOPT) -output-obj -o $(*F)_camlcode.ob \
$(FLAGS_MLC) \
$<
%.oo: %.c
$(CC) $(FLAGS_WRAP) -c $< -o $(SRC)/$(*F).oo
clean_mli:
rm -f $(SRC)/*.mli # */
clean:
rm -f $(BIN)/*.{a,o,oo,cmi,cmo,cmx} # */
rm -f $(SRC)/*.{a,o,oo,cmi,cmo,cmx} # */
mrproper: clean, clean_mli
rm -f $(BIN)/$(EXE)
depend:
$(OCAMLDEP) $(INCLUDES) $(SRC)/*.ml $(SRC)/*.mli > .depend # */
include .depend
Your link command is incorrect in two ways:
You need to link with -ldl for dlopen, etc.
You must put libraries after objects that reference them (i.e. your -lcamlrun arguments is in the wrong place on the link line). The order of arguments on the link line matters.