Makefile Giving Duplicate Error C - c

I have created a makefile to run 9 programs simultaneously.
The names of the 9 programs are init_aim.c, register.c, sort.c, schedule.c, add.c, class_schedule.c, class_list.c, grade.c, and report.c.
In each of the files I have implemented the code:
#include "aim.h"
int main(){
return 0;
}
I also have a header file with the code
#ifndef AIM_H
#define AIM_H
#endif
My only goal is to get my makefile working. And also add the "all:" rule. My current makefile looks like this.
AIS: init_aim.o register.o sort.o schedule.o add.o class_schedule.o
class_list.o grade.o report.o
gcc -Wall -ansi -pedantic init_aim.o register.o sort.o
schedule.o add.o class_schedule.o class_list.o grade.o report.o -o AIS
init_aim.o: init_aim.c aim.h
gcc -Wall -ansi -pedantic -c init_aim.c -o init_aim.o
register.o: register.c aim.h
gcc -Wall -ansi -pedantic -c register.c -o register.o
sort.o: sort.c aim.h
gcc -Wall -ansi -pedantic -c sort.c -o sort.o
schedule.o: schedule.c aim.h
gcc -Wall -ansi -pedantic -c schedule.c -o schedule.o
add.o: add.c aim.h
gcc -Wall -ansi -pedantic -c add.c -o add.o
class_schedule.o: class_schedule.c aim.h
gcc -Wall -ansi -pedantic -c class_schedule.c -o
class_schedule.o
class_list.o: class_list.c aim.h
gcc -Wall -ansi -pedantic -c class_list.c -o class_list.o
grade.o: grade.c aim.h
gcc -Wall -ansi -pedantic -c grade.c -o grade.o
report.o: report.c aim.h
gcc -Wall -ansi -pedantic -c report.c -o report.o
Please keep in mind the spacing does not look proper on here because there is too much code.
When I run make I get:
gcc -Wall -ansi -pedantic -c init_aim.c -o init_aim.o
gcc -Wall -ansi -pedantic -c register.c -o register.o
gcc -Wall -ansi -pedantic -c sort.c -o sort.o
gcc -Wall -ansi -pedantic -c schedule.c -o schedule.o
gcc -Wall -ansi -pedantic -c add.c -o add.o
gcc -Wall -ansi -pedantic -c class_schedule.c -o class_schedule.o
gcc -Wall -ansi -pedantic -c class_list.c -o class_list.o
gcc -Wall -ansi -pedantic -c grade.c -o grade.o
gcc -Wall -ansi -pedantic -c report.c -o report.o
gcc -Wall -ansi -pedantic init_aim.o register.o sort.o schedule.o add.o
class_schedule.o class_list.o grade.o report.o -o AIS
clang: warning: argument unused during compilation: '-ansi' [-Wunused-
command-line-argument]
duplicate symbol _main in:
init_aim.o
register.o
duplicate symbol _main in:
init_aim.o
sort.o
duplicate symbol _main in:
init_aim.o
schedule.o
duplicate symbol _main in:
init_aim.o
add.o
duplicate symbol _main in:
init_aim.o
class_schedule.o
duplicate symbol _main in:
init_aim.o
class_list.o
duplicate symbol _main in:
init_aim.o
grade.o
duplicate symbol _main in:
init_aim.o
report.o
ld: 8 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make: *** [AIS] Error 1
If anyone knows how to solve the duplicate error and add the "all:" command please let me know.

You have defined main() in each code file.
main() is the "start here" point for the executable you are trying to create.
The build fails because of multiple "start here" points.
To put it differently:
You can only have a single main() in a single executable.
You might be thinking of multithread programming.
In that case you need to do some research on the topic.
You might be thinking of running multiple programs from shell.
In that case you need to create multiple executables and start them separatly with the appropriate shell features, e.g. for background execution.

If you are writing nine separate programs you shouldn't try to link them all together at the end. I think this is what you want
all: init_aim register sort schedule add class_schedule class_list grade report
init_aim: init_aim.c aim.h
gcc -Wall -ansi -pedantic init_aim.c -o init_aim
register: register.c aim.h
gcc -Wall -ansi -pedantic register.c -o register
...
I removed -c, removed .o and added the all rule.

Related

Why while using makefile I receive undefined reference error?

I created the following makefile:
assembler: main.o first_pass.o second_pass.o helpers.o data_array.o symbol_table.o
gcc -Wall -ansi -pedantic main.o first_pass.o second_pass.o helpers.o data_array.o symbol_table.o -o assembler
main.o: main.c header.h
gcc -Wall -ansi -pedantic main.c -o main.o
first_pass.o: first_pass.c header.h
gcc -Wall -ansi -pedantic first_pass.c -o first_pass.o
second_pass.o: second_pass.c header.h
gcc -Wall -ansi -pedantic second_pass.c -o second_pass.o
helpers.o: helpers.c header.h data.h
gcc -Wall -ansi -pedantic helpers.c -o helpers.o
data_array.o: data_array.c header.h data.h
gcc -Wall -ansi -pedantic data_array.c -o data_array.o
symbol_table.o: symbol_table.c header.h data.h
gcc -Wall -ansi -pedantic symbol_table.c -o symbol_table.o
In my 'main.c' file I have #include "header.h". Where in 'header.h' I have the declarations of all the functions.
But I receive the following error:
gcc -Wall -ansi -pedantic main.c -o main.o
/tmp/cc3dP7hx.o: In function `main':
main.c:(.text+0x257): undefined reference to `first_pass`
main.c:(.text+0x2e4): undefined reference to `second_pass'
main.c:(.text+0x37f): undefined reference to build_obj_file
main.c:(.text+0x3a9): undefined reference to build_ent_file
main.c:(.text+0x427): undefined reference to free_all_data
main.c:(.text+0x436): undefined reference to free_all_symbols
main.c:(.text+0x44d): undefined reference to free_all_words
collect2: error: ld returned 1 exit status
makefile:4: recipe for target 'main.o' failed
make: *** [main.o] Error 1
The problem is that your command doesn't create object files, but attempt to build executable programs:
gcc -Wall -ansi -pedantic main.c -o main.o
You need the -c option to create object files:
gcc -Wall -pedantic main.c -c -o main.o
A better idea would be to rely on the make implicit rules so you don't have to explicitly list all commands:
CC = gcc
LD = gcc
CFLAGS = -Wall -pedantic
LDFLAGS =
assembler: main.o first_pass.o second_pass.o helpers.o data_array.o symbol_table.o
You might want to add rules for the header-file dependencies, or figure out some way to auto-generate them (it's possible). Other than that the above Makefile should be enough to build all object files and then link them together into the assembler executable program.
Note that I have removed the -ansi flag, as it's mostly obsolete these days.

error: cannot specify -o when generating multiple output files

How to fix this error?
I am trying to compile the C project, but I get such error
Alekseys-MBP:mmn14 aleksey$ make
gcc -Wall -ansi -pedantic -c -o assembler.o assembler.c
gcc -Wall -ansi -pedantic -c -o utils.o utils.c
gcc -Wall -ansi -pedantic -c -o symbol_table.o symbol_table.c
gcc -Wall -ansi -pedantic -c -o file_scan.o file_scan.c
file_scan.c:323:2: warning: no newline at end of file [-Wnewline-eof]
}
^
1 warning generated.
gcc -Wall -ansi -pedantic -c -o data_block.o data_block.c
gcc -Wall -ansi -pedantic -c -o commands.o commands.c
gcc -Wall -ansi -pedantic assembler.o utils.o symbol_table.o file_scan.o utils.h data_block.o commands.o -o assembler
clang: error: cannot specify -o when generating multiple output files
make: *** [assembler] Error 1
there is my makefile
EXEC_FILE = assembler
C_FILES = assembler.c utils.c symbol_table.c file_scan.c utils.h data_block.c commands.c
H_FILES = common.h symbol_table.h data_block.h commands.h file_scan.h
O_FILES = $(C_FILES:.c=.o)
all: $(EXEC_FILE)
$(EXEC_FILE): $(O_FILES)
gcc -Wall -ansi -pedantic $(O_FILES) -o $(EXEC_FILE)
%.o: %.c $(H_FILES)
gcc -Wall -ansi -pedantic -c -o $# $<
clean:
rm -f *.o $(EXEC_FILE)
What am I doing wrong?
This list of C files:
C_FILES = assembler.c utils.c symbol_table.c file_scan.c utils.h data_block.c commands.c
contains a header, file utils.h.
When this statement:
O_FILES = $(C_FILES:.c=.o)
constructs the list of object files, the substitution .c=.o does not alter utils.h, which results in O_FILES containing a header file. Then this build command:
gcc -Wall -ansi -pedantic $(O_FILES) -o $(EXEC_FILE)
asks GCC to do its default actions with the files, which is to link the object files and write an executable file and to “precompile” (analyze) the header file and write a “precompiled header” data file. Thus, you have two output files.
To fix this, remove utils.h from C_FILES and put it in H_FILES.

Failed to do 'make' on .c and .s files using Mac

I'm trying to compile a simple project with .c and .s files using my Mac.
When I run 'make' it goes threw on the compilation, and I think it failed when its trying to link (not sure).
Here is the error it shows:
gcc -m32 -g -Wall -c -o main.o main.c
gcc -m32 -g -Wall -c -o numbers.o numbers.c
nasm -g -f macho -w+all -o add.o add.s
gcc -m32 -g -Wall -o run main.o numbers.o add.o
ld: malformed file
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libSystem.tbd:4:18: error: unknown enumerated scalar
platform: zippered
^~~~~~~~
file '/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libSystem.tbd'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [run] Error 1
and I'll add the makefile as well:
run: main.o numbers.o add.o
gcc -m32 -g -Wall -o run main.o numbers.o add.o
main.o: main.c
gcc -m32 -g -Wall -c -o main.o main.c
numbers.o: numbers.c
gcc -m32 -g -Wall -c -o numbers.o numbers.c
add.o: add.s
nasm -g -f macho -w+all -o add.o add.s
.PHONY: clean
clean:
rm -f *.o run

Makefile not executing, error expects symbol

Just have been trying to write a makefile that consists of 5 files --> arrays.h
arrays.c pointers.h pointers.c stringtest.c
When I run make however, I get and error expecting "=", "," ";" etc and it then later says that no targets are specified. Any ideas?
stringtest : stringtest.c arrays.o pointers.o
cc -std=c99 -Wall -pedantic -Werror -o stringtest stringtest.c arrays.o pointers.o
arrays.o : arrays.c arrays.h
cc -std=c99 -Wall -pedantic -Werror -c -o arrays.o arrays.c
pointers.o : pointers.c pointers.h
cc -std=c99 -Wall -pedantic -Werror -c -o pointers.o pointers.c
You are using TABs for indentation?
Commands for an target must be indent with it. Like:
pointers.o: ...
---->cc -std=c99 ..

gcc builds with -o but not -o3?

My Makefile looks like this:
CC=gcc
CFLAGS=-Wall -Wextra -std=c99 -pedantic
OBJECTS=main.o Scene.o Matrix.o Vector.o Triangle.o Color.o Raster.o
render: $(OBJECTS)
$(CC) $(CFLAGS) -lm -o render -g $(OBJECTS)
rm $(OBJECTS)
clean:
rm -f render*
This builds my executable with no errors, but when I change -o to -o2 or -o3, I get the error:
gcc -Wall -Wextra -std=c99 -pedantic -c -o main.o main.c
gcc -Wall -Wextra -std=c99 -pedantic -c -o Scene.o Scene.c
gcc -Wall -Wextra -std=c99 -pedantic -c -o Matrix.o Matrix.c
gcc -Wall -Wextra -std=c99 -pedantic -c -o Vector.o Vector.c
gcc -Wall -Wextra -std=c99 -pedantic -c -o Triangle.o Triangle.c
gcc -Wall -Wextra -std=c99 -pedantic -c -o Color.o Color.c
gcc -Wall -Wextra -std=c99 -pedantic -c -o Raster.o Raster.c
gcc -Wall -Wextra -std=c99 -pedantic -lm -o3 render -g main.o Scene.o Matrix.o Vector.o Triangle.o Color.o Raster.o
gcc.exe: error: render: No such file or directory
make: *** [render] Error 1
There could be some error in my code detected by the optimization flags, but as I don't get any error messages before this it's hard to know what's going wrong. I'm using MinGW/MSYS on Windows 7.
-o render means create the output file with the name render.
Now you are changing this -o to -o3 which is incorrect. Instead you need to keep -o render as it is and add a -O3 flag for optimization. Note the capital letter O.
-o is the output file flag. You were thinking of -O (capital).

Resources