简体   繁体   English

在makefile中使用-lstdc ++

[英]use -lstdc++ in a makefile

here's my error 这是我的错误

make
cc   msgd.o   -o msgd
msgd.o: In function `main':
/home/cam/Desktop/lab1/msgd.cc:37: undefined reference to `Server::Server(int, bool)'
/home/cam/Desktop/lab1/msgd.cc:39: undefined reference to `Server::~Server()'
msgd.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.7/iostream:75: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/4.7/iostream:75: undefined reference to `std::ios_base::Init::~Init()'
msgd.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
make: *** [msgd] Error 1

The first two undefined references to Server:: are bogus, the code works and Server.h is included properly in msgd.cc. 对Server ::的前两个未定义引用是虚假的,该代码有效,并且Server.h已正确包含在msgd.cc中。

However when I simply do "make server" it runs fine, and when I do "make client" it runs fine, but I want to be able to just do "make" and have it build both. 但是,当我简单地执行“ make server”时,它运行良好,当我执行“ make client”时,它运行良好,但是我希望能够仅执行“ make”并同时构建两者。

Here's my makefile. 这是我的makefile。 According to https://stackoverflow.com/a/10907227/2080104 I need to include -lstdcc++ but I can't seem to figure out how to do so in a makefile. 根据https://stackoverflow.com/a/10907227/2080104,我需要包含-lstdcc ++,但我似乎无法弄清楚如何在makefile中这样做。

# Makefile for socket examples

CXX=            g++ $(CCFLAGS)
msgd=           msgd.o Server.o Data.o User.o Message.o Str.o
msg=            msg.o Client.o Str.o
OBJS =          $(msgd) $(msg)


CCFLAGS= -std=c++11 -g

all:    msgd Server Data User Message Str msg Client

server:$(msgd)
    $(CXX) -o msgd $(msgd) 

client:$(msg)
    $(CXX) -o msg $(msg)

clean:
    rm -f $(OBJS) $(OBJS:.o=.d)

realclean:
    rm -f $(OBJS) $(OBJS:.o=.d) msgd msg


# These lines ensure that dependencies are handled automatically.
%.d:    %.cc
    $(SHELL) -ec '$(CC) -M $(CPPFLAGS) $< \
        | sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
        [ -s $@ ] || rm -f $@'

include $(OBJS:.o=.d)

You named a variable msgd when you had an object file named msgd.o , and then you got them mixed up. 当您有一个名为msgd.o的对象文件时,便为变量msgd命名,然后将它们混在一起。 Oh, and you have a target server which doesn't actually build server , it builds msgd . 哦,您有一个目标server实际上并没有构建server ,而是构建了msgd

You have a default target ( all ) which builds things other than what you want the default target to build. 您有一个默认目标( all ),它可以构建除您希望构建的默认目标以外的内容。

You want to Make to do something (ie use -lstdcc++ ) when either you don't know how to do it on the command line, or you don't know which part of the makefile corresponds to what you do on the command line. 当您不知道如何在命令行上执行操作,或者不知道makefile的哪一部分与您在命令行上执行的操作相对-lstdcc++您想使它执行某些操作(即,使用-lstdcc++ )。

I'd suggest specific changes to the makefile, but your intent is so unclear I'm afraid I'd do more harm than good. 我建议对Makefile进行特定更改,但是您的意图尚不清楚,以至于我担心弊大于利。

What happens is that target all requires msgd . 发生的是该目标都需要msgd

all:    msgd Server Data User Message Str msg Client

You don't have a rule for msgd , however, make figures it can build msgd from msgd.o from msgd.cc (or whatever extension your source has) using the built-in rule : 你没有一个规则msgd ,然而,让数字就可以建立msgdmsgd.omsgd.cc (或任何扩展你的来源有),使用内置的规则

Linking a single object file 链接单个目标文件

n is made automatically from no by running the linker (usually called ld) via the C compiler. n是通过C编译器运行链接器(通常称为ld)从否自动生成的。 The precise recipe used is '$(CC) $(LDFLAGS) no $(LOADLIBES) $(LDLIBS)'. 使用的精确配方为“ $(CC)$(LDFLAGS)否$(LOADLIBES)$(LDLIBS)”。

Your makefile should look more like the following: 您的makefile应该看起来更像以下内容:

CXX := g++
CXXFLAGS := -std=c++11 -g
CPPFLGS :=
LDFLAGS :=
LDLIBS := -g

all : msgd msg

msgd_obj := msgd.o Server.o Data.o User.o Message.o Str.o
msg_obj := msg.o Client.o Str.o
OBJ := ${msgd_obj} ${msg_obj}

msgd : ${msgd_obj}
msg : ${msg_obj}

# The rule to link the executables.
msgd msg :
    ${CXX} -o $@ ${LDFLAGS} $^ ${LDLIBS}

# The rule to compile object files and produce dependencies.
%.o : %.cc
    ${CXX} -o $@ ${CPPFLAGS} ${CXXFLAGS} -MD -MP $<

clean:
    rm -f $(OBJS) $(OBJS:.o=.d)

realclean:
    rm -f $(OBJS) $(OBJS:.o=.d) msgd msg

-include $(OBJS:.o=.d)

.PHONY: all clean realclean

Note that there should not be a specific rule to produce dependencies. 请注意,不应存在产生依赖关系的特定规则。 Dependencies are produced as a by-product of compilation. 依赖关系是编译产生的副产品。 On the first run dependencies are unnecessary because everything must be built anyway, on subsequent runs it uses dependencies from the previous run to determine what have changed since. 第一次运行不需要依赖,因为无论如何都必须构建所有内容,在后续运行中,它使用上一次运行的依赖关系来确定此后发生了什么变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM