简体   繁体   English

指定备用编译器和链接器

[英]Specify alternate compiler and linker to make

I have both a default C/C++ tool set (gcc 4.8) and have recently built and installed 5.2 in its own directory. 我既有默认的C / C ++工具集(gcc 4.8),又最近在其自己的目录中构建并安装了5.2。 I want to build some C++ programs using a makefile that uses CC and CXX explicity to compile but uses the implicit built-in rule to link the .o files to build the executable. 我想使用makefile构建一些C ++程序,该makefile使用CC和CXX显式编译,但使用隐式内置规则链接.o文件以构建可执行文件。

When I run make on the makefile, I use the command 在makefile上运行make时,我使用以下命令

make all CC=/usr/gcc-5.2.0/bin/gcc-5.2.0 CXX=/usr/gcc-5.2.0/bin/g++-5.2.0

The compile steps all use the 5.2 compilers but when the executable is build, the default g++ is used to link everything. 编译步骤全部使用5.2编译器,但是在生成可执行文件时,将使用默认的g ++链接所有内容。 Now, this happens to work and the results run but it isn't what I want. 现在,这碰巧可以工作并且结果可以运行,但这不是我想要的。 I tried adding 我尝试添加

LD=/usr/gcc-5.2.0/bin/g++-5.2.0

to the make command line but LD is ignored. 到make命令行,但LD被忽略。 Without changing the makefile, how can I get the link step to use the 5.2 compiler? 在不更改makefile的情况下,如何获得使用5.2编译器的链接步骤?

Here is the make file: 这是make文件:

CPPFLAGS = -Wall
LDFLAGS = -lstdc++
CXX = g++
CC = g++

all: TestSHA3 HashSHA3 HashZeroBytes LongTest sha3sum

debug: override CPPFLAGS += -ggdb
debug: all

SHA3-o3:
        $(CXX) $(CPPFLAGS) -O3 -c -o SHA3.o SHA3.cpp
o3:  SHA3-o3 all

TestSHA3: TestSHA3.o SHA3.o
HashSHA3: HashSHA3.o SHA3.o
HashZeroBytes: HashZeroBytes.o SHA3.o
LongTest: LongTest.o SHA3.o
sha3sum: sha3sum.o SHA3.o

.PHONY: clean realclean rc debug all o3 SHA3-o3
clean:
        rm SHA3.o TestSHA3.o HashSHA3.o HashZeroBytes.o LongTest.o sha3sum.o
rc: realclean
realclean: clean
        rm TestSHA3 HashSHA3 HashZeroBytes LongTest sha3sum

Here is the command: 这是命令:

 make all CXX=g++-5.2.0 LD=g++-5.2.0

And here are the results of the make: 这是make的结果:

g++-5.2.0  -Wall  -c -o TestSHA3.o TestSHA3.cpp
g++-5.2.0  -Wall  -c -o SHA3.o SHA3.cpp
g++ -lstdc++  TestSHA3.o SHA3.o   -o TestSHA3
g++-5.2.0  -Wall  -c -o HashSHA3.o HashSHA3.cpp
g++ -lstdc++  HashSHA3.o SHA3.o   -o HashSHA3
g++-5.2.0  -Wall  -c -o HashZeroBytes.o HashZeroBytes.cpp
g++ -lstdc++  HashZeroBytes.o SHA3.o   -o HashZeroBytes
g++-5.2.0  -Wall  -c -o LongTest.o LongTest.cpp
g++ -lstdc++  LongTest.o SHA3.o   -o LongTest
g++-5.2.0  -Wall  -c -o sha3sum.o sha3sum.cpp
g++ -lstdc++  sha3sum.o SHA3.o   -o sha3sum

Use: 采用:

make all CXX=g++-5.2.0 CC=g++-5.2.0

The implicit rule for linking uses CC ( make manual ): 链接的隐式规则使用CC制作手册 ):

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)”。

With your Makefile and dummy source files: 使用您的Makefile和虚拟源文件:

$ make all CXX=`which g++`
/usr/bin/g++  -Wall  -c -o TestSHA3.o TestSHA3.cpp
/usr/bin/g++  -Wall  -c -o SHA3.o SHA3.cpp
g++ -lstdc++  TestSHA3.o SHA3.o   -o TestSHA3
/usr/bin/g++  -Wall  -c -o HashSHA3.o HashSHA3.cpp
g++ -lstdc++  HashSHA3.o SHA3.o   -o HashSHA3
/usr/bin/g++  -Wall  -c -o HashZeroBytes.o HashZeroBytes.cpp
g++ -lstdc++  HashZeroBytes.o SHA3.o   -o HashZeroBytes
/usr/bin/g++  -Wall  -c -o LongTest.o LongTest.cpp
g++ -lstdc++  LongTest.o SHA3.o   -o LongTest
/usr/bin/g++  -Wall  -c -o sha3sum.o sha3sum.cpp
g++ -lstdc++  sha3sum.o SHA3.o   -o sha3sum

$ make all CC=`which g++`
g++  -Wall  -c -o TestSHA3.o TestSHA3.cpp
g++  -Wall  -c -o SHA3.o SHA3.cpp
/usr/bin/g++ -lstdc++  TestSHA3.o SHA3.o   -o TestSHA3
g++  -Wall  -c -o HashSHA3.o HashSHA3.cpp
/usr/bin/g++ -lstdc++  HashSHA3.o SHA3.o   -o HashSHA3
g++  -Wall  -c -o HashZeroBytes.o HashZeroBytes.cpp
/usr/bin/g++ -lstdc++  HashZeroBytes.o SHA3.o   -o HashZeroBytes
g++  -Wall  -c -o LongTest.o LongTest.cpp
/usr/bin/g++ -lstdc++  LongTest.o SHA3.o   -o LongTest
g++  -Wall  -c -o sha3sum.o sha3sum.cpp
/usr/bin/g++ -lstdc++  sha3sum.o SHA3.o   -o sha3sum

$ make all CC=`which g++` CXX=`which g++`
/usr/bin/g++  -Wall  -c -o TestSHA3.o TestSHA3.cpp
/usr/bin/g++  -Wall  -c -o SHA3.o SHA3.cpp
/usr/bin/g++ -lstdc++  TestSHA3.o SHA3.o   -o TestSHA3
/usr/bin/g++  -Wall  -c -o HashSHA3.o HashSHA3.cpp
/usr/bin/g++ -lstdc++  HashSHA3.o SHA3.o   -o HashSHA3
/usr/bin/g++  -Wall  -c -o HashZeroBytes.o HashZeroBytes.cpp
/usr/bin/g++ -lstdc++  HashZeroBytes.o SHA3.o   -o HashZeroBytes
/usr/bin/g++  -Wall  -c -o LongTest.o LongTest.cpp
/usr/bin/g++ -lstdc++  LongTest.o SHA3.o   -o LongTest
/usr/bin/g++  -Wall  -c -o sha3sum.o sha3sum.cpp
/usr/bin/g++ -lstdc++  sha3sum.o SHA3.o   -o sha3sum

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

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