0

I'm learning C++ on Mac and trying to follow along with the SFML Game Development by Example book. Any obvious places to start looking for problems when I get:

duplicate symbol __Z2dpi in:
    /var/folders/f1/4ddx4sk171v4hp6s50c93zh40000gn/T/sfgamewindow-374355.o
    /var/folders/f1/4ddx4sk171v4hp6s50c93zh40000gn/T/statemanager-3f5acc.o
[ plus 34 more similar duplicate symbols]

? Would that have anything to do with #including?

This is my make command:

cd ~/Programming/CPP;
clang++  -g -O0 -Wall -std=c++14 \
-o Testgame         \
-I. -I/Users/johnrebekah/Programming/SFML\ source/include \
-I/Users/johnrebekah/Programming/CPP/gameheaders \
-lsfml-window.2.5.1     \
-lsfml-system.2.5.1     \
-lsfml-graphics.2.5.1   \
 "/Users/johnrebekah/Programming/CPP/gameheaders/sfgamewindow.cpp" \
 "/Users/johnrebekah/Programming/CPP/gameheaders/statemanager.cpp" \
 "/Users/johnrebekah/Programming/CPP/gameheaders/introstate.cpp" \
 "/Users/johnrebekah/Programming/CPP/gameheaders/eventmanager.cpp" \
 "/Users/johnrebekah/Programming/CPP/gameheaders/game.cpp" \
 "/Users/johnrebekah/Programming/CPP/testgame.cpp" \

All of the listed duplicate symbols are from the first file in the list of cpps in the make command, paired with various other of the cpp files...

  • Do you have a symbol called `dpi` somewhere? If so, please show the containing file. – 0x5453 May 18 '21 at 04:38
  • None of the symbols listed are intelligible to me, they aren't anything that I wrote in my source files. Any chance they are from the SFML library? – johnnywz00 May 18 '21 at 04:41
  • Wait, I did write a debug print function called dp... I'm going to try commenting that out... – johnnywz00 May 18 '21 at 04:45
  • 1
    According to https://demangler.com/ `__Z2dpi` is a function called "dp" that takes an integer. And the error message says it is defined in both /Users/johnrebekah/Programming/CPP/gameheaders/sfgamewindow.cpp and /Users/johnrebekah/Programming/CPP/gameheaders/statemanager.cpp – Jerry Jeremiah May 18 '21 at 04:48
  • I commented out a top-level function in a header of my own that I included, and now the duplicate symbol warning is gone but I have this:``` ld: can't open output file for writing: Testgame, errno=21 for architecture x86_64 clang: error: linker command failed with exit code 1``` – johnnywz00 May 18 '21 at 04:52
  • Seems that some functions are defined in some header but not inline and not static so those violate one definition rule. – Öö Tiib May 18 '21 at 04:52
  • Is there a keyword I need to add before the function for it to compile correctly? – johnnywz00 May 18 '21 at 04:54
  • If it is in header then keyword is inline. If you have functions with same name in different cpps then keyword is static. – Öö Tiib May 18 '21 at 04:58
  • Don't define non-inline functions in header files. Declarations go to header files. Definitions go to the .cpp files. Also look up "name mangling". – n. m. could be an AI May 18 '21 at 05:11
  • Thank you all for the help, got it running... – johnnywz00 May 18 '21 at 05:26
  • Note: Compiling all the `.cpp` files at once is asking for trouble. The normal process is to build each individually, then link together for the final library or executable. – tadman May 18 '21 at 05:49

0 Answers0