Xcode error: Command /Developer/usr/bin/clang++ failed with exit code 1 due to duplicate symbol

StackOverflow https://stackoverflow.com/questions/10006737

  •  29-05-2021
  •  | 
  •  

سؤال

I'm trying to write a program in C++ which runs Conway's Game of Life. I think I have everything that I need, but I'm having some trouble with compiling.

The program is composed of four files: gameoflife.h, a header file which contains my global constants and function declarations, gameoflife.cpp, which defines the functions, main.cpp, which uses the functions, and seeds.cpp, which contains a list of predefined seeds to be used.

When I go to compile the application, I seem to have a clash of duplicate symbols between main.cpp and gameoflife.cpp over an array called currGen which is declared in gameoflife.h.

Both main.cpp and gameoflife.cpp include gameoflife.h, which of course is necessary so that they have access to the global constants and function declarations.

The exact error I receive is the following:

duplicate symbol _currGen in /(same_path)/ConwaysGameOfLife.build/Objects-normal/
x86_64/gameoflife.o and
/(same_path)/ConwaysGameOfLife.build/Objects-normal/x86_64/main.o
for architecture x86_64
Command /Developer/usr/bin/clang++ failed with exit code 1

I've looked around on Stack Overflow but haven't found anything which matches my problem. Any help would be greatly appreciated!

هل كانت مفيدة؟

المحلول

You are probably defining the variable currGen in your header file, not just declaring it.

There needs to be exactly one definition, in one .cpp file. The .h file should just declare it, using extern.

This answer goes into much more detail.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top