Reputation: 3730
I have main.c, snmpy.c, snmpy.o, and a makefile. I running this on a Linux Server through command line. Here is what is all of them...
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "snmpy.h"
int main(void) {
char* message = sayHello();
printf("%s", message);
return 0;
}
snmpy.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "snmpy.h"
char* sayHello(){
char* hiya = "Hello!!\n";
return hiya;
}
snmpy.h:
char* sayHello();
makefile:
# Compiler
CC = /usr/bin/gcc
# Name of program
PROG = snmpy
# The name of the object files
OBJS = snmpy.o main.o
# All the header and c files
SRCS = main.c snmpy.c
HDRS = snmpy.h
# Add -I to the dir the curl include files are in
CFLAGS = -c -g -std=c99 -Wall
# Build the executable file
$(PROG): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(PROG)
# Seperately compile each .c file
main.o: main.c snmpy.h
$(CC) $(CFLAGS) -c main.c
snmpy.o: snmpy.c snmpy.h
$(CC) $(CFLAGS) -c snmpy.c
# Clean up crew
clean:
rm -fv core* $(PROG) $(OBJS)
cleaner: clean
rm -fv #* *~
When I compile it it gives me this error:
/usr/bin/gcc -c -g -std=c99 -Wall snmpy.o main.o -o snmpy
gcc: snmpy.o: linker input file unused because linking not done
gcc: main.o: linker input file unused because linking not done
Not sure what's going on whether there is something I'm not doing right, or if I didn't install something. I'm a noob at making make files. I haven't done them in a while.
Thanks in advance!!
Upvotes: 3
Views: 16047
Reputation: 21351
You need to lose the -c
option to do full linking. The -c
will just cause the source files to be compiled into *.o
files.
This is what the the man gcc
call says about the -c
option
-c Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o.
Unrecognized input files, not requiring compilation or assembly, are ignored.
Upvotes: 3
Reputation: 143047
Re:
/usr/bin/gcc -c -g -std=c99 -Wall snmpy.o main.o -o snmpy
The -c
option in the command means compile (but don't link). The result of this will be .o
(object) files.
Try the command without the -c
, it should link and create your snmpy
executable.
Upvotes: 8
Reputation: 7282
By doing:-
# Build the executable file
$(PROG): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(PROG)
Because CFLAGS
contains a -c
you've said to use -c
in the linking phase too.
You really want:-
$(PROG): $(OBJS)
Make should do the rest for you.
You could probably loose the *.o
targets too.
Upvotes: 4