Re: Link error ....redefinition of......

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Alex Luya <alex_luya@xxxxxxxx> writes:

> I download source code for book <</Data Structures and Algorithm
> Analysis in C++ (Second Edition), /by Mark Allen Weiss>>
> from:http://users.cs.fiu.edu/~weiss/dsaa_c++/code/,try to compiler
> it,but got many errors,most of them say:
> ...... previously declared here
> .......: redefinition of .....

These errors occur also with g++ -c, which does not run the
linker; therefore they are compile errors, rather than link
errors.

First, download the ANSI C++ versions of the source files
from <http://users.cs.fiu.edu/~weiss/dsaa_c++/code/ansi/>.
The original files are for GCC 2.8.1 and have problems
with GCC 4.3.3.

Then, do not compile StackAr.cpp separately.  That won't work.
StackAr.cpp begins with #include "StackAr.h", and there is an
#include "StackAr.cpp" near the end of StackAr.h, so you get a
loop (although not an infinite loop):

StackAr.cpp: #include "StackAr.h"
  StackAr.h: #ifndef STACKAR_H  /* not yet defined */
  StackAr.h: #define STACKAR_H
  StackAr.h: template <class Object> class Stack { ... };
  StackAr.h: #include "StackAr.cpp"
    StackAr.cpp: #include "StackAr.h"
      StackAr.h: #ifndef STACKAR_H /* now it's already defined */
      StackAr.h: /* the contents are therefore ignored */ 
      StackAr.h: #endif
    StackAr.cpp: ... defines the various functions ...
  StackAr.h: #endif
StackAr.cpp: ... tries to define the same functions again ...

Thus, StackAr.cpp should be used only via StackAr.h, never on its
own.  Perhaps it'd be better to name such files e.g. *.inl rather
than *.cpp, to discourage IDEs from attempting to compile them.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux