Reputation: 13
I am trying to compile the following source file using gcc -g -Wall -Wextra -pedantic -std=c99 -o main -lm
.
void simple_sum(void)
#include "source.h"
#include <stdio.h>
void simple_sum(void)
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d + %d = %d\n",a, b, a + b);
}
#include "source.h"
#include <stdio.h>
int main(void)
{
printf("\n");
simple_sum();
return 0;
}
I get following error:
gcc -g -Wall -Wextra -pedantic -std=c99 -o main -lm
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function _start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:6: recipe for target 'main' failed
make: *** [main] Error 1
Could someone please suggest how to fix this?
I get the following error when I run using gcc -g -Wall main.c -o main
/tmp/ccEAL4iG.o: In function
main':
/home/a/aalto_university/functions/calculation/main.c:7: undefined reference to simple_sum'
collect2: error: ld returned 1 exit status
Upvotes: 0
Views: 705
Reputation: 11921
Here
gcc -g -Wall -Wextra -pedantic -std=c99 -o main -lm
you are not providing source file name to linker, hence it throw error like
undefined reference to `main'
While compiling provide source file main.c
and source.c
. For e.g first run this
gcc -g -Wall -Wextra -pedantic -std=c99 -c main.c source.c -lm
to create the object.o
files & then create the executable by running
gcc source.o main.o -o my_exe
And finally run the executable. Also declaration of simple_sum()
missing ;
it should be
void simple_sum(void); /* you miss ;*/
Also learn how to use Makefile for compilation as @Basile pointed, there you don't have to create .o
file manually, your Makefile
will create .o
file & compile if it's written correctly.
Upvotes: 3
Reputation: 1
Compile with
gcc -g -Wall -Wextra -pedantic -std=c99 source.c main.c -o myprog -lm
(actually, -lm
is not needed, you don't use <math.h>
functions; but keeping -lm
should not harm)
Later, learn to write your Makefile
to do these things in several steps:
First, get the source.o
object file with
gcc -g -Wall -Wextra -pedantic -std=c99 -c source.c
then get the main.o
object file with
gcc -g -Wall -Wextra -pedantic -std=c99 -c main.c
At last, link both of them
gcc -g source.o main.o -lm -o myprog
Upvotes: 4