🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.migration
7 messages
7 total messages Started by Gene Sat, 05 Jul 2008 16:45
Dll problem in CBC2007.
#129
Author: Gene
Date: Sat, 05 Jul 2008 16:45
22 lines
1400 bytes
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.
Re: Dll problem in CBC2007.
#130
Author: Remy Lebeau (TeamB)
Date: Sun, 06 Jul 2008 06:46
26 lines
1461 bytes
"Gene" <shenjige@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 
Still not working.
#131
Author: Gene
Date: Mon, 07 Jul 2008 01:10
36 lines
1874 bytes

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!


"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"Gene" <shenjige@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 
>
>
Re: Still not working.
#132
Author: Ed Mulroy [TeamB]
Date: Mon, 07 Jul 2008 01:31
68 lines
3645 bytes
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...
>
> 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!
>
>
> "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>>
>>"Gene" <shenjige@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.
Re: Still not working.
#133
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Jul 2008 16:40
19 lines
1624 bytes
"Gene" <shenjige@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 
Re: Dll problem in CBC2007.
#136
Author: Ed Mulroy [TeamB]
Date: Tue, 08 Jul 2008 23:00
135 lines
3860 bytes
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...
>
> 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.
Re: Dll problem in CBC2007.
#137
Author: Ed Mulroy [TeamB]
Date: Tue, 08 Jul 2008 23:02
135 lines
3857 bytes
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...
>
> 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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads