This question already has answers here:
Closed 3 years ago.
What is an undefined reference/unresolved external symbol error and how do I fix it?
(32个答案)
除了混合和匹配.c.cpp文件外,这是一个简单的Mag文件——但是,我不太清楚为什么它没有编译。我认为这是因为链接器错误,因为它只是在抱怨网络类。你们中有谁能让大师知道下面的问题吗?
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()'

最佳答案

我猜你可能希望你的all目标看起来像这样

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

关于c++ - Makefile:链接器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37647947/

10-10 14:19