user12091669
user12091669

Reputation:

How to resolve "multiple definition of 'main()'" C compiling error?

I recently started learning C and must create program that scanf two integer values from standard input separated by a space then printf the sum of these two integers. Must be able to accept negative values. I'm using repl.it to write code 1st then pasting in .c to compile.

Attempt:

#include <stdio.h>

int main() {    

    int number1, number2, sum;

    printf("Enter two integers: ");

    scanf("%d", &number1);

    scanf("%d", &number2);

    // calculating sum

    sum = number1 + number2;      

    printf("%d + %d = %d", number1, number2, sum);

    return 0;
}

[OP originally said "Except this prints" -- but this is not program output -- this is error output during the compilation process before the program ever ran]

Except when I try to compile the IDE outputs the errors

/tmp/t2-8eec00.o: In function `main':
t2.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
/tmp/t3-72a7ab.o: In function `main':
t3.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
/tmp/main-2c962b.o: In function `main':
main.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
exit status 1

The output is wrong so what mistake did I make? What's the correct method to get expected values?

(eg 1+2=3)

platform using it on:

https://i.sstatic.net/zJElj.jpg

Upvotes: 0

Views: 2642

Answers (2)

BaseZen
BaseZen

Reputation: 8718

This is a project management issue. The IDE shows that you have 4 files, all of which conflict with each other. You have t1.c, t2.c, t3.c, and main.c. They all try to define main(), so in fact you have a 4-way conflict.

Functions in C exist in a global namespace across the whole project.

Remove all files from the project that don't have the version of main() you actually want, and re-compile. -- OR rename the functions in the other files to mutually distinct names different than main(). You may get a warning that these functions are never used, but the project will compile.

Upvotes: 5

Aravinda KS
Aravinda KS

Reputation: 195

The error is saying that you have defined the main function multiple times, try to compile it in a new file. The code is correct, so try to compile the code by creating new file.

Upvotes: 0

Related Questions