Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dll problem in CBC2007.

2 views
Skip to first unread message

Gene

unread,
Jul 5, 2008, 12:45:25 PM7/5/08
to

Hello,

I got a new problem for Dll calling from BCB2007. The Dll calling is ok in BCB6. That code is as in below:

In the Dll header:

#ifdef __BUILDING_DLL
#define __EXPORT_TYPE __export
#else
#define __EXPORT_TYPE __import
#endif

template <class T>
void __EXPORT_TYPE ClearContainer(T& x) { T y; y.swap(x); };


Then I include the header in my main cpp file, and call the function. The Dll project build is ok, but when I try to build the main project. It says "Link32 error: unresolved external function..." Does any one know what causes that and what should I do to get around?

Any suggestion will be appreciated.

Gene.

Remy Lebeau (TeamB)

unread,
Jul 6, 2008, 2:46:00 AM7/6/08
to

"Gene" <shen...@yahoo.ca> wrote in message
news:486fa525$1...@newsgroups.borland.com...

> #ifdef __BUILDING_DLL
> #define __EXPORT_TYPE __export
> #else
> #define __EXPORT_TYPE __import
> #endif

Use ___declspec() instead:

#ifdef __BUILDING_DLL
#define __EXPORT_TYPE __declspec(dllexport)
#else
#define __EXPORT_TYPE __declspec(dllimport)
#endif

> template <class T>
> void __EXPORT_TYPE ClearContainer(T& x) { T y; y.swap(x); };

Why are you trying to export an inlined template function? That is not
necessary.


Gambit


Gene

unread,
Jul 6, 2008, 9:10:25 PM7/6/08
to

It doesn't seem to work. The only purpose I have the inline function exported is to test if a function that takes a template class auguement works. I have a couple of that kind of functons in my old BCB6 project, they are ok when I use my old way in BCB6. When I upgrade to BCB2007, all exported functions are ok except those functions that takes tamplate class auguements.

Any further idea, Remy? Thanks a lot!

Ed Mulroy [TeamB]

unread,
Jul 6, 2008, 9:31:29 PM7/6/08
to
The code in a templated function actually specifies the algorithm rather
than the code. Only when it is called is the type of the template argument
specified so only at that point does it have the information necessary to
generate code.

For example: were it called with a double, machine code for floating point
arithmetic must be generated and if called with an int, code for fixed point
generated.

In this compilation unit only the function prototype is supplied so the
compiler must depend upon a call to a function it hopes has been resolved
with the appropriate type. The linker must resolve that so it must find a
public symbol for this function as expanded for the needed type.

In the DLL code that you do not show, insert a call to the function using
the type that is needed so that a version of the function for that type will
be available.

The use of 'export', '_export', '__export', 'import', '_import' and
'__import' was replaced when the current compiler went to 32 bits only. The
C++ Language standard now includes 'export' as a C++ keyword with a
different meaning than that for which you are using it.

The old words are in the compiler for those who cannot avoid legacy code and
are not intended for code from the last 10 years or code meeting the C++
Language Standard of the last 10 or so years. Do what Mr Lebeau has said
and use __declspec(dllexport) for _export and __declspec(dllimport) for
_import.

. Ed

> Gene wrote in message
> news:48716d01$1...@newsgroups.borland.com...

Remy Lebeau (TeamB)

unread,
Jul 7, 2008, 12:40:22 PM7/7/08
to

"Gene" <shen...@yahoo.ca> wrote in message
news:48716d01$1...@newsgroups.borland.com...

> It doesn't seem to work.

Whenever you say "does not work", you have to be more specific. WHAT does
not work exactly?

> The only purpose I have the inline function exported is to test
> if a function that takes a template class auguement works.

There is still no point in attempting that. By their very nature, templates
are always inlined. Whatever code unit includes the header file(s) will
pull in the entire function code directly. There is no lookup needed to
find the code. So the export is useless even if it did work for templates.


Gambit


Ed Mulroy [TeamB]

unread,
Jul 8, 2008, 7:00:54 PM7/8/08
to
Below is the source for a program and a DLL such as you are asking about, a
program which provides no body for the templated function and a DLL that
provides a body for the templated function specialized for the type used to
call the function in the main program.

. Ed

----------------------------------------------
#ifndef THEDLLH
#define THEDLLH

// header file used by both the DLL and the program using the DLL
class IntType
{
public:
IntType(int i = 0) : value(i)
{
}
~IntType()
{
}
void swap(IntType & a)
{
value ^= a.value;
a.value ^= value;
value ^= a.value;
}
int Value() const
{
return value;
}

private:
int value;
};


#ifdef __BUILDING_DLL
#define __EXPORT_TYPE _declspec(dllexport)
#else
#define __EXPORT_TYPE _declspec(dllimport)
#endif


__EXPORT_TYPE
template <class T>
void ClearContainer(T& x);

#endif

----------------------------------------------

// main program source

#include <windows.h>
#include "thedll.h"

void ShowContainer(const char * title, const IntType & c)
{
char buffer[128];

wsprintf(buffer, "Container value is %d", c.Value());
MessageBox(HWND_DESKTOP, buffer, title, MB_OK);
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
IntType container(12);

ShowContainer("Starting", container);
ClearContainer(container);
ShowContainer("After Calling ClearContainer", container);
return 0;
}

----------------------------------------------
// DLL source

#include <windows.h>

#define __BUILDING_DLL
#include "thedll.h"


int WINAPI DllEntryPoint(HINSTANCE, unsigned long, void*)
{
return 1;
}


// specialization of ClearContainer for T == IntType
__EXPORT_TYPE template <>
void ClearContainer<IntType>(IntType& x)
{
IntType y;
y.swap(x);
};


// dummy function used to force compiler to create an
// instance of ClearContainer specialized for T == IntType
__EXPORT_TYPE void NeverCalled(IntType& x)
{
ClearContainer(x);
}

----------------------------------------------

> Gene wrote in message
> news:486fa525$1...@newsgroups.borland.com...

Ed Mulroy [TeamB]

unread,
Jul 8, 2008, 7:02:06 PM7/8/08
to

. Ed

private:
int value;
};

#endif

----------------------------------------------

// main program source

#include <windows.h>
#include "thedll.h"

----------------------------------------------
// DLL source

#include <windows.h>

#define __BUILDING_DLL
#include "thedll.h"

----------------------------------------------

0 new messages