I am attempting to compile a basic array wrapper template class into a .o file, to try and figure out how to make dynamic libraries. However, when compiling the source with
g++ -std=c++0x -c array.cpp
the resulting file is only ~650 bytes. I examined the file with nm, and found the only symbols it contained were
00000001 r _ZStL13allocator_arg
00000000 r _ZStL19piecewise_construct
which seem to be features of the C++11 compilation, as when compiled without the -std=c++0x flag they are gone.
Attempting to compile the program test code and all with the command
g++ -std=c++0x -o tester tester.cpp array.cpp
produces linker errors for everything in array.cpp but otherwise compiles cleanly.
I honestly have no idea what is going on with this. I can post the contents of array.cpp and array.hpp if you suspect this an issue with my code itself, rather than how I am compiling it.
That's normal. Templates only result in code when they're instantiated, and you haven't done that. You've only provided the template. While the compiler is compiling the template, it has no reason to generate any specific instances of the template with actual types filled in for the template arguments.
Later, the compiler compiles the other source files, where you use the template type. That doesn't cause instantiation, either. It merely tells the compiler that it should expect to find instances of the template elsewhere. The compiler notes that in the object files. The linker then looks for definitions that the compiler said it should find, but it fails.
That's why you're always advised to provide definitions for your templates inline with the declaration, in the header. That way, when the compiler sees you use a template, it can instantiate the template right away, using the definition it saw in the header. If you use the template in multiple source files, you might end up with multiple definitions from instantiating the template multiple times, but that's allowed, and the linker knows how to handle it.
The alternative is to explicitly instantiate the templates in your array.cpp file, but it's easier to just define the template members inline.