Board index » cppbuilder » Exporting .bpl functions and namespaces

Exporting .bpl functions and namespaces


2005-07-04 08:27:53 PM
cppbuilder12
After searching through the archives, I saw a number of threads that
talked about exporting functions and using namespaces, but nothing
really seemed to address/resolve my problem:
I've started incorporating namespaces, and added one to the header
file of a unit in a package. The exported functions were working fine
before the namespace was included, but they are not being exported
(they're no longer showing up in the .bpi file) when the namespace is
included in the header. Removing the namespace results in the
functions showing up in the .bpi again.
Since this is my first attempt at using namespaces, I figure I'm
missing something in using them properly -- eg, does something else
need to go on the function declaration lines? Any
comments/suggestions would be appreciated.
-- salamander
//=====================
// Package Header File
//=====================
namespace GIDInfoLib {
......
//===============================================
// Exported function declarations
//===============================================
extern "C" __stdcall PACKAGE void Version(int &, int &);
extern "C" __stdcall PACKAGE int GetMajorVersion(void);
extern "C" __stdcall PACKAGE int GetMinorVersion(void);
extern "C" __stdcall PACKAGE void ShowAbout(const GIDAboutInfo &);
extern "C" __stdcall PACKAGE void CreateSplash(const GIDAboutInfo &,
const HANDLE,
unsigned int);
extern "C" __stdcall PACKAGE void DestroySplash(void);
//===============================================
......
} using namespace GIDInfoLib;
 
 

Re:Exporting .bpl functions and namespaces

salamander1965 < XXXX@XXXXX.COM >wrote:
Quote
Since this is my first attempt at using namespaces, I figure I'm
missing something in using them properly -- eg, does something else
need to go on the function declaration lines? Any
comments/suggestions would be appreciated.

-- salamander


//=====================
// Package Header File
//=====================
namespace GIDInfoLib {
[snip];
Quote
} using namespace GIDInfoLib;
(Yeuch. At first sight, that shouldn't compile. On consideration, it
should. See end note)
Are you implementing the functions you say you export, or different
ones?
An example:
// GIDInfoLib.h
//
namespace GIDInfoLib
{
extern "C" void Version(int & Major, int & Minor);
}
// GIDInfoLib.cpp
//
namespace GIDInfoLib
{
void Version(int &Major, int &Minor)
{
Major = 1;
Minor = 5;
}
}
// Client source file
//
#include "GIDInfoLib.h"
using namespace GIDInfoLib;
void f()
{
int Major, Minor;
Version(Major, Minor);
}
Here, you may note that I implement the function
GIDInfoLib::Version(int&, int&). If I hadn't put the namespace block
around the function definition, then it would have been ::Version(int&,
int&), which is of course a totally different function, and wouldn't be
exported.
BTW - putting 'using namespace whatever;' into a header file is almost
guaranteed to bring you grief. It's legal (the language has no way of
preventing it), but it cancels out pretty much every benefit of a
namespace.
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
 

Re:Exporting .bpl functions and namespaces

Thanks for pointing out my error -- all is well now.
Quote
Are you implementing the functions you say you export, or different
ones?
I thought I was, but in fact I wasn't based on your example. Although
I had the function declarations in the header file enclosed in
namespace GIDInfoLib, the definitions in the .cpp file were not. I
initially thought that putting the declarations in the namespace was
enough, but in retrospect I see now that this would short-circuit the
whole purpose of namespaces if the header is included in a client unit
that uses functions of the same name.
Quote
// Client source file
//
#include "GIDInfoLib.h"
using namespace GIDInfoLib;
BTW - putting 'using namespace whatever;' into a header file is almost
guaranteed to bring you grief. It's legal (the language has no way of
preventing it), but it cancels out pretty much every benefit of a
namespace.
My overall goal was to be able to shorten my package function names by
removing the unique part of the name -- ie, I could change
GIDInfoLib_ShowAbout() to ShowAbout(). But, I was also wanting to
avoid having to explicitly qualify package function calls with the
namespace in the client code. The ambiguity that arises with TBitmap
requiring the use of Graphics::TBitmap came to mind, and I put 'using
namespace GIDInfoLib' in the header because that's the way the VCL
headers do it. I didn't realize it would cancel out the benefits of
namespace use, though, and I don't know enough about namespaces to
understand why this would be the case.
What would be a better way of doing this? It seems like following the
#include for the header file by a using namespace statement would be
equivalent to putting 'using namespace ...' in the header itself.
Thanks again -- salamander
 

{smallsort}

Re:Exporting .bpl functions and namespaces

On Mon, 04 Jul 2005 09:24:59 -0400, salamander1965 wrote:
Quote
What would be a better way of doing this? It seems like following the
#include for the header file by a using namespace statement would be
equivalent to putting 'using namespace ...' in the header itself.
You could of course explicitly reference the namespace
ThingieNamespace::MyFunction(a1,a2);
I realize you were trying to avoid this, but it does kill the ambiguity.
--
Good luck,
liz
 

Re:Exporting .bpl functions and namespaces

Quote
You could of course explicitly reference the namespace

ThingieNamespace::MyFunction(a1,a2);

I realize you were trying to avoid this, but it does kill the ambiguity.
My project is small so I don't think I'll ever have problem with
ambiguity with my own code, but with using functions named ShowAbout()
etc..., I can see that I might accidentally create a conflict. My
plan is to let the compiler let me know when ambiguity exists, then I
can put in the explicit namespace qualifier.
Thanks -- salamander
 

Re:Exporting .bpl functions and namespaces

salamander1965 < XXXX@XXXXX.COM >wrote:
Quote
What would be a better way of doing this? It seems like following the
#include for the header file by a using namespace statement would be
equivalent to putting 'using namespace ...' in the header itself.
Not equivalent - because that using directive only affects that source
file. If you put a using directive into a header file, then it affects
every file that includes it, directly or indirectly, whether or not they
wanted to be affected.
It's best to keep the 'using' scope nice and small. This definitely
means not putting "using namespace x;" into a header file (because then
it affects all symbols in that namespace, and all files that include
that header).
Using the "using mynamespace::mysymbol;" at the top of a source file, on
the other hand, is usually conveniently small. If the symbol in question
is only used in one function, then put the 'using' in that function. If
the symbol is used only once, then don't use 'using' at all.
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
 

Re:Exporting .bpl functions and namespaces

It's starting to make more sense to me now.
Thanks again -- salamander