Skip to main content
GameDev.net gamedev.net
🔒 Locked

templates and dll

Started by Metal Typhoon Oct 28, 2005 at 7:46 PM 0 replies 1k views
Original Post
Metal Typhoon
Metal Typhoon
I'm trying to to make a template function inside a dll and i need to export it. I'm manuall loading the dll file. This is what i have.

#define DLLOUT extern "C" __declspec(dllexport) 

#define N_INPUT_ENTERSTATE  0x09
#define	N_INPUT_PRINTCOUNT	0x0A
#define N_INPUT_MAXBUFFER	0x0B
#define N_INPUT_BUFFER		0x0C

template <class type> type nGet (int Return);
this compiles fine.. but if i put the DLLOUT in front of the template it gives me errors. it says (C:\DOCUMENTS AND SETTINGS\METAL TYPHOON\MY DOCUMENTS\Input\nInput.h(41) : warning C4091: '__declspec(dllexport ) ' : ignored on left of 'int' when no variable is declared) when i put it here DLLOUT type nGet ... it gives me this error (C:\DOCUMENTS AND SETTINGS\METAL TYPHOON\MY DOCUMENTS\Input\nInput.h(41) : error C2059: syntax error : 'string') Where do i put this DLLOUT in order to export it ? thank you for any help
Metal Typhoon
MaulingMonkey
MaulingMonkey
Quote:
Original post by Metal Typhoon
I'm trying to to make a template function inside a dll


You can't export templates with current compilers.

Wait, that's a lie, let me rephrase this.

You can't export generic templates with current compilers.
You can export explicit template instantiations. Google for "C++ template emit" "C++ explicitly instantiated template" or similar to check what I'm talking about.

It won't work so well with the extern "C" bit I suspect. The name mangling's kind of helpful/needed I would suspect.

Basically, you'll end up doing something like this:

__declspec(dllexport) /* <-- dunno what compilers this will be/is necessary on */ template int nGet< int >( int );__declspec(dllexport) template float nGet< float >( int );


And then get linker errors if you try to use nGet< char >, since that wasn't explicitly instantiated. For situations where this isn't appropriate, you're going to have to make that part of the library header-only. Sorry.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.