-1

I encounter g++ compile/link problem. I want to implement a Dynamic Array. I separated my class definition and implementation into "DArray.h" and "DArray.cpp" files. And do test in "DArraytest.cpp" file within which contains a main functionn.

all there files are in same directory, and I use the following command to compile g++ *.cpp

but it still gives me such link error /tmp/cc4tu73o.o: In function __static_initialization_and_destruction_0(int, int)': DArray.cpp:(.text+0x1d): undefined reference tostd::ios_base::Init::Init()' DArray.cpp:(.text+0x34): undefined reference to std::ios_base::Init::~Init()' /tmp/ccdHUZjJ.o: In functionmain': DArraytest.cpp:(.text+0x12): undefined reference to DArray<int>::DArray()' DArraytest.cpp:(.text+0x2e): undefined reference toDArray::append(int const&)' DArraytest.cpp:(.text+0x4a): undefined reference to DArray<int>::append(int const&)' DArraytest.cpp:(.text+0x66): undefined reference toDArray::append(int const&)' DArraytest.cpp:(.text+0x82): undefined reference to DArray<int>::append(int const&)' DArraytest.cpp:(.text+0xa3): undefined reference tostd::cout' DArraytest.cpp:(.text+0xa8): undefined reference to std::ostream::operator<<(int)' DArraytest.cpp:(.text+0xb0): undefined reference tostd::basic_ostream >& std::endl >(std::basic_ostream >&)' DArraytest.cpp:(.text+0xb8): undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' DArraytest.cpp:(.text+0xc9): undefined reference toDArray::~DArray()' DArraytest.cpp:(.text+0xeb): undefined reference to DArray<int>::~DArray()' /tmp/ccdHUZjJ.o: In function__static_initialization_and_destruction_0(int, int)': DArraytest.cpp:(.text+0x11d): undefined reference to std::ios_base::Init::Init()' DArraytest.cpp:(.text+0x134): undefined reference tostd::ios_base::Init::~Init()' /tmp/ccdHUZjJ.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0' collect2: error: ld returned 1 exit status

Can someone help me

LiloD
  • 17
  • 2
  • 1
    It looks like you defined templates in a source file. Move them into the header: http://stackoverflow.com/questions/495021 – Mike Seymour Oct 08 '14 at 23:51

1 Answers1

0

I think you need to provide all the .cpp file names to g++. To avoid this you can create a makefile which can automatically apply same compilation rule to all .cpp files

John_Avi_09
  • 323
  • 1
  • 9