Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc: not linking files

Tags:

c

gcc

linker

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!!

like image 621
Rob Avery IV Avatar asked May 09 '26 18:05

Rob Avery IV


2 Answers

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.

like image 113
Levon Avatar answered May 12 '26 09:05

Levon


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.

like image 35
IanNorton Avatar answered May 12 '26 10:05

IanNorton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!