Makefile中用于链接C ++目标文件的默认链接器设置 [英] Default linker setting in Makefile for linking C++ object files

查看:130
本文介绍了Makefile中用于链接C ++目标文件的默认链接器设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此Makefile

% cat Makefile
main: main.o add.o

使用cc而不是g++链接目标文件

which uses cc instead of g++ to link the object files

% make
g++ -Wall -pedantic -std=c++0x   -c -o main.o main.cpp
g++ -Wall -pedantic -std=c++0x   -c -o add.o add.cpp
cc   main.o add.o   -o main
main.o:main.cpp:(.text+0x40): undefined reference to `std::cout'
...

我如何告诉(GNU)使用g++(链接C ++库)而不是cc?

How do I tell (GNU) Make to use g++ (which links the C++ libraries) instead of cc?

推荐答案

(GNU)Make具有内置规则,这很好,因为它足以提供没有规则的依赖项:

(GNU) Make has built-in rules, which is nice, because it is enough to give dependencies without rules:

main: main.o add.o
    # no rule, therefore use built-in rule

但是,在这种情况下,内置规则使用$(CC)链接目标文件.

However the build-in rule in this case uses $(CC) for linking object files.

% make -p -f/dev/null
...
LINK.o = $(CC) $(LDFLAGS) $(TARGET_ARCH)
...
LINK.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
...
%: %.o
#  recipe to execute (built-in):
        $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@

要让Make选择正确的链接器,只需将LINK.o设置为LINK.cc.因此,最小的Makefile可能看起来像

To let Make chose the correct linker, it is sufficient to set LINK.o to LINK.cc. A minimal Makefile can therefore look like

% cat Makefile
LINK.o = $(LINK.cc)
CXXFLAGS=-Wall -pedantic -std=c++0x

main: main.o add.o

这篇关于Makefile中用于链接C ++目标文件的默认链接器设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆