Linking fails with templates? [SOLVED]

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

pcmattman wrote:Actually there's another reason why that loader has to go first - it holds the multiboot header...
I guess you can tell it to put the multiboot header in its own section and then place it. I'm not sure you can tell it not to throw it away though, but then again you'll probably use it from your code so it won't remove it anyway.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Re: Linking fails with templates?

Post by B.E »

pcmattman wrote:Any ideas?
I need to see the whole header file. What the errors till me is that it can't find the correct implementation. Just remember the template can never be compiled. only the resulting code from the template can be compiled (i.e each time a template is used with different args, a copy of that code is compiled with them arguments and added to the final executable. by arguments I mean the stuff between the < and >).
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Here you go:

Code: Select all

#include <console.h>

template< class T >
class Accumulator
{
	public:
		Accumulator() { };
		~Accumulator() { };
		T Value();
		T operator = ( T val );
	private:
		T m_val;
};

template< class T > T Accumulator<T>::Value()
{
	return(m_val);
}

template< class T > T Accumulator<T>::operator = ( T val )
{
	return( m_val = val );
}
Note that when I told G++ to link as well as compile (ie. not doing the link myself) there were no errors. The only problem was it complained about the loader object format (assembled via NASM). And there is no way I'm converting it to GAS syntax.

Edit: OK, here's what happens with the following command line:

Code: Select all

gpp -I "include" -masm=intel loader.obj *.cc */*.cc -nostdlib -fno-builtin -fno-rtti -fno-exceptions -Wl,-T linker.ld
Outputs:

Code: Select all

Warning: input file is not COFF or stubbed COFF
loader.obj is an ELF object file assembled with NASM. Any ideas?

Edit 2: Wait. The error comes from 'stubify.exe' - converting the final image to an exe??? I'm playing around with the command line. Once I've got it working, I'm making a makefile so I don't keep getting complaints about command line length...

Edit 3: Nope, still the same problems... I just can't get templates to work!

Code: Select all

c:/djgpp/bin/ld.exe: c:/djgpp/tmp/ccnnBa6D.o: Unrecognized storage class 127 for /4 symbol `Accumula
tor<int>::Accumulator()'
c:/djgpp/bin/ld.exe: c:/djgpp/tmp/ccnnBa6D.o: Unrecognized storage class 127 for /44 symbol `Accumul
ator<int>::test()'
c:/djgpp/bin/ld.exe: c:/djgpp/tmp/ccnnBa6D.o: Unrecognized storage class 127 for /87 symbol `Accumul
ator<int>::operator=(int)'
c:/djgpp/bin/ld.exe: c:/djgpp/tmp/ccnnBa6D.o: Unrecognized storage class 127 for /127 symbol `Accumu
lator<int>::Value()'
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

And there is no way I'm converting it to GAS syntax
You might reconsider this because the problems now seem very familiar and i fixed it by converting the bootloader to GAS and because of the integration with the C++ part it works perfectly. My bootsector does not contain any data, only code, to load the stage1 loader. .. but that is not what you want to hear.

You can either use a cross-compiler and also build your C++ code as ELF or you have to convert the loader.obj to COFF format. I guess you can use objcopy for that.

Or you could build you loader.obj with 'nasm -f coff' to compile it in coff format.
Author of COBOS
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

OK. The main reason why I don't want to convert is because there's a lot of code that would then need to be rewritten.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

I have no idea, but have you rebuilt the source from scratch lately. Some times a linker erroris can be solved simply by rebuilding the whole thing from scratch.
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Yes, multiple times :shock:

And I've rewritten the loader in GAS (GAS is actually really nice to read).
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

OK, I must know nothing about templates... Visual C++ throws this error on some template code I wrote:

Code: Select all

main.obj : error LNK2001: unresolved external symbol "public: int __thiscall CLinkedList<int>::test(int)" (?test@?$CLinkedList@H@@QAEHH@Z)
This is insanity! Can someone please save me and tell me how to do the templates properly?

Edit: OK, almost there! New problem:

Code: Select all

c:/djgpp/bin/ld-elf.exe: C:\OSDev\MattiseOS\CPP_Kernel\kernel.o: Unrecognized storage class 127 for
/4 symbol `__ZN4TestIiE2hiEi'
From the following:

Code: Select all

template< typename _T >
class Test
{
	public:
		void hi( _T );
};

template< typename _T > void Test<_T>::hi( _T t )
{
	kprintf( "hi!" );
}
Any ideas?

Edit 2: Actually, it's not a new problem... This is somewhat frustrating.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I'm rebuilding my cross-compiler. I'm so sick of this.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Ha! Beat that, GCC! I win!

Guess what? I compiled in my newly-built (rebuilt) cross-compiler, and BAM!!! Templates work!

I'm a very happy person!
Post Reply