【发布时间】:2014-02-21 03:34:31
【问题描述】:
我收到以下错误:
> /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
我已经将标题包含在我试图初始化的类中。所以我想也许它是我的 makefile?我对编写自定义 makefile 还是很陌生...
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);
主要:
#include "tcpcon.h"
int main()
{
TCPConnection *TCPCon = new TCPConnection("localhost", "7777");
cout << "Hi\n";
return 0;
}
制作文件:
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 $@ $<
【问题讨论】:
-
这是生成文件。
g++ -Wall -Wextra -g -o manager tcpcon.cpp manager.cpp应该可以正确构建。 -
好吧,你应该在最后一个命令中包含
tcpcon.o。g++ -Wall -Wextra -g -o manager tcpcon.o manager.cpp -
所以我想我真正的问题是,为什么我的 makefile 还没有这样做?
-
因为你写的是
$(TARGET),它没有定义,而不是$(TARGETS),它是定义的? -
啊哈!我没有将我的主文件 (manager.cpp) 添加到 OBJS 列表中 - 我已将其更新为
OBJS = manager.o tcpcon.o,并且似乎一切正常。