User9123
User9123

Reputation: 774

Makefile Giving Duplicate Error 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.

Upvotes: 1

Views: 145

Answers (2)

john
john

Reputation: 87959

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.

Upvotes: 3

Yunnosch
Yunnosch

Reputation: 26703

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.

Upvotes: 3

Related Questions