lundi 16 mars 2020

Running makefile causes linker issue but plain g++ does not

When I run my makefile,

CXX = g++
CXXFLAGS = -std=c++1z -g -Wfatal-errors -Wall -Wextra -pedantic

output: main.o stack.o cube.o hslapixel.o
  $(CXX) $(CXXFLAGS) main.o stack.o cube.o hslapixel.o -o output

main.o: main.cpp
  $(CXX) $(CXXFLAGS) -c main.cpp

stack.o: stack.cpp
  $(CXX) $(CXXFLAGS) -c stack.cpp

cube.o: cube.cpp
  $(CXX) $(CXXFLAGS) -c cube.cpp

hslapixel.o: hslapixel.cpp
  $(CXX) -c hslapixel.cpp

clean:
  -rm *.o output

I get a duplicate symbol error.

duplicate symbol __ZN4uiuclsERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEERKNS_5StackE in:
    main.o
    stack.o
ld: 1 duplicate symbol for architecture x86_64
clang: fatal error: linker command failed with exit code 1 (use -v to see invocation)

I researched for a solution and most of the top references were for Xcode. Since I am on a mac, I assumed it was. I tried upgrading Xcode but couldn't because of the hardware limitation. Then I tried running just plain g++ with the files

g++ -std=c++11 main.cpp stack.cpp cube.cpp hslapixel.cpp

I had no issue. Compilation and linking succeeded. Why is this the case?

Other questions:

  1. I would appreciate some direction on reading the duplicate symbol below. How would I know what variable it is connected to?
__ZN4uiuclsERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEERKNS_5StackE
  1. What is referred to by the -v invocation in the error message? make -v gives me the version. Is this invocation flag for make or something else?

EDIT: Checking whether I am running g++ or clang.

$ g++ --version

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Trying to install gcc

$ brew install gcc

Error: gcc 7.1.0 is already installed
To upgrade to 9.3.0, run `brew upgrade gcc`.

Aucun commentaire:

Enregistrer un commentaire