1

I'm getting the following error:

> /tmp/ccYbdvB8.o: In function `main':
> /home/caleb/Documents/dev/cs438/tote2/mp2/manager.cpp:5: undefined
> reference to `TCPConnection::TCPConnection(char const*, char const*)'
> collect2: error: ld returned 1 exit status make: *** [manager] Error 1

I've included the header to the class I'm trying to initialize.. so I'm thinking maybe it's my makefile? I'm pretty new to writing custom makefiles...

cpp:

#include "tcpcon.h"

const string TCPConnection::Client = "client";
const string TCPConnection::Server = "server";

TCPConnection::TCPConnection(const char* t, const char* p) : target(t), port(p)
{ }

h:

class TCPConnection{    
    public:    
        TCPConnection(const char *target, const char *port);

main:

#include "tcpcon.h"

int main()
{
    TCPConnection *TCPCon = new TCPConnection("localhost", "7777");
    cout << "Hi\n";
    return 0;
}

makefile:

CC=g++
CCOPTS=-Wall -Wextra -g

OBJS = tcpcon.o
TARGETS = manager

.PHONY: all clean

$(TARGET) : $(OBJS)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

all: $(OBJS) $(TARGETS)

clean:
    rm -f $(TARGETS) $(OBJS)

%: %.cpp
    $(CC) $(CCOPTS) -o $@ $<
timrau
  • 22,578
  • 4
  • 51
  • 64
MrDuk
  • 16,578
  • 18
  • 74
  • 133
  • 1
    It's the makefile. `g++ -Wall -Wextra -g -o manager tcpcon.cpp manager.cpp` should build correctly. – timrau Feb 21 '14 at 03:46
  • Hm, mine is: `g++ -Wall -Wextra -g -o manager manager.cpp` – MrDuk Feb 21 '14 at 03:47
  • 1
    Well, you should include `tcpcon.o` in the last command. `g++ -Wall -Wextra -g -o manager tcpcon.o manager.cpp` – timrau Feb 21 '14 at 03:48
  • 1
    So I guess my real question here is, why doesn't my makefile already do that? – MrDuk Feb 21 '14 at 03:50
  • 2
    Because you write `$(TARGET)`, which is not defined, instead of `$(TARGETS)`, which is defined? –  Feb 21 '14 at 04:01
  • Hm, that produced: g++ -o manager tcpcon.o ...///... (.text+0x20): undefined reference to `main' – MrDuk Feb 21 '14 at 04:07
  • 1
    Ah-ha! I didn't add my main file (manager.cpp) to the OBJS list - I've updated it to `OBJS = manager.o tcpcon.o`, and all seems to be working. – MrDuk Feb 21 '14 at 04:24
  • You could write your working makefile as answer and accept it. – timrau Feb 21 '14 at 17:27
  • Didn't even consider that - thanks! – MrDuk Feb 21 '14 at 17:34

1 Answers1

1

It turns out that the issue may have been related to a couple of things:

  • Use of $(TARGET) instead of $(TARGETS) in one place
  • Did not include the main file (manager.cpp) to the OBJS list

My updated makefile is below:

CC=g++
CCOPTS=-Wall -Wextra -g

OBJS = manager.o tcpcon.o
TARGETS = manager

.PHONY: all clean

$(TARGETS) : $(OBJS)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

all: $(TARGETS) $(OBJS)

clean:
    rm -f $(TARGETS) $(OBJS)

%: %.cpp
    $(CC) $(CCOPTS) -o $@ $<
MrDuk
  • 16,578
  • 18
  • 74
  • 133