0

Other than mixing and matching .c and .cpp files, this is a simple Makefile -- yet, I'm not really sure why it's not compiling. I assume it's because of a linker error since it's only complaining about the network class. Any one of you Make gurus know what's wrong with the following?

CC=g++
DEBUG=-g
CFLAGS=$(DEBUG) -Wall -D_POSIX_C_SOURCE -pthread -std=c++0x
HEADERS=cl_network.h

%.o: %.c $(HEADERS)
    $(CC) $(CFLAGS) $< -o $@

all: server.o client.o cl_network.o
    $(CC) $(CFLAGS) server.o -o server
    $(CC) $(CFLAGS) client.o -o client
    $(CC) $(CFLAGS) cl_network.o -o cl_network

client.o: client.cpp cl_network.h
    $(CC) $(CFLAGS) -c client.cpp

server.o: server.c
    $(CC) $(CFLAGS) -c server.c

cl_network.o: cl_network.cpp cl_network.h
    $(CC) $(CFLAGS) -c cl_network.cpp

.PHONY: clean
clean:
    rm  server.o server client.o client cl_network cl_network.o

Errors:
client.cpp:49: undefined reference to `cl_network::cl_network(char*)'
client.cpp:50: undefined reference to `cl_network::connectToServer()'
client.cpp:63: undefined reference to `cl_network::~cl_network()'
client.cpp:63: undefined reference to `cl_network::~cl_network()'
user207421
  • 305,947
  • 44
  • 307
  • 483
MrPickles
  • 1,255
  • 1
  • 16
  • 31
  • Sidenote: If your compiler supports it, use -std=c++11 instead of -std=c++0x for a better artistic score. :) https://stackoverflow.com/questions/24589105/what-is-the-difference-between-std-c0x-and-std-c11 –  Jun 06 '16 at 01:06
  • the line: `.PHONY; all` should be inserted, before the 'all' target – user3629249 Jun 06 '16 at 02:15
  • to avoid multiple evaluations of the macros, use: `macroName := contents rather than `macroName=contents` – user3629249 Jun 06 '16 at 02:16
  • the first 'target' in the make file should be 'all:', not '%.o:' – user3629249 Jun 06 '16 at 02:19
  • why does the make file have this: `%.o: %.c $(HEADERS) $(CC) $(CFLAGS) $< -o $@` when each of the involved source files is being handled in a separate rule? – user3629249 Jun 06 '16 at 02:20
  • the 'client' and 'server' seem to both be using the function/class in 'cl_network', but that executable is not being included in the link statements for the 'client' and 'server' – user3629249 Jun 06 '16 at 02:25
  • it would be better to use the 'make' short hand rather than writing out each of the file names in the compile rules. – user3629249 Jun 06 '16 at 02:25
  • it would seem that the file 'cl_network.cpp' is being compiled/linked as a separate executable. That is not the right way to do it. Rather the cl_network.o file should be one of the files listed in the link statement for 'client'. – user3629249 Jun 06 '16 at 02:27

1 Answers1

3

I'm guessing that you probably want your all target to look like this

all: server.o client.o cl_network.o
    $(CC) $(CFLAGS) server.o cl_network.o -o server
    $(CC) $(CFLAGS) client.o cl_network.o -o client
Adrian
  • 14,931
  • 9
  • 45
  • 70