[Solved] Makefile in C (ubuntu) multiple definition


read documentation!

First, take a few hours to read documentation of GNU make, and read how to invoke GCC. You also need to understand more about the preprocessor, so read documentation of cpp. You want to take advantage of builtin GNU make rules (so run make -p to understand them) and variables. See also this answer. You could use remake (as remake -x) to debug your Makefile. You apparently don’t understand how make and how gcc should be used, so you need to read more. Read also a C tutorial, look into some C reference, and glance when needed into the C11 standard n1570. Of course, read the documentation of every function you use (e.g. printf(3) etc..). For Linux system programming, read a book like ALP and relevant man pages from syscalls(2) and intro(3) etc…

Then read How to debug small programs. You certainly want to compile with all warnings and debug info.


a better Makefile

You might try something like:

# a better Makefile
# your C compiler
CC= gcc

# the verbose remove
RM= rm -vf

# your C compilation flags
CFLAGS= -Wall -Wextra -g

# your C source files
MY_CSOURCES= logfind.c cmdargutils.c filesystem_utils.c file_utils.c strutils.c

# the corresponding object files
MY_OBJECTS= $(patsubst %.c, %.o, $(MY_CSOURCES))

# the conventional phony targets
.PHONY: all clean

# the only program is for the default target all
all: logfind
logfind: $(MY_OBJECTS)
     $(LINK.c) $< -o $@

# cleaning the mess
clean: 
     $(RM) logfind *.o *~

Of course, you need dependencies for object files on header files. You could compute them automatically, but it is simpler to explicit them, so add something like:

strutils.o: strutils.c strutils.h

and so on for each other object files.

BTW my HelloWorld/ directory on github is a tutorial example for using make


your multiple definition bug

You are getting multiple definition of MAX_LINE because it is defined in a header file included by several translation units, hence several translation units define it.

So either make it a preprocessor constant #define MAX_LINE 1024 in your header file_utils.h, or put there only a declaration like extern const int MAX_LINE; and define it only once in a single translation unit, as const int MAX_LINE=1024; in file_utils.c


general hints

I strongly recommend doing some iterative and incremental development: code only one or two dozen lines at once, then compile them, improve them to get no warnings, debug them with the GDB debugger and test them. At last repeat all this till satisfied. I do recommend using also a version control system (like git) even for school homework.

You might want to use valgrind to hunt memory leaks and other dynamic memory allocation bugs.

You could also use some static source analyzer like clang-analyzer or even Frama-C.

Once your program is debugged, you might add optimization flags like -O2 into your CFLAGS (in particular if you benchmark it with time(1)).

You could be interested by ntfw(3).

2

solved Makefile in C (ubuntu) multiple definition