473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Placing functions from .lib in a namespace?

Hi all.

For my application I'm using a third-party library which is supplied as a
library file (.lib) and an associated header file to prototype the functions
defined in the library. Important is that the library is already compiled.

Now, this library defines only functions, and they are declared in the
global namespace. Since this is rather ugly and inconvenient, I want to
place these functions in their own namespace.

Is it possible to change the fully qualified names of these functions if
they're already compiled, or maybe is there another way to get these
functions in their own namespace?

Thanks,
Leon.
Jul 22 '05 #1
12 1507
Leon wrote:
Hi all.

For my application I'm using a third-party library which is supplied as a
library file (.lib) and an associated header file to prototype the functions
defined in the library. Important is that the library is already compiled.

Now, this library defines only functions, and they are declared in the
global namespace. Since this is rather ugly and inconvenient, I want to
place these functions in their own namespace.

Is it possible to change the fully qualified names of these functions if
they're already compiled, or maybe is there another way to get these
functions in their own namespace?


I've usually gotten away with this:

namespace Acme
{
#include "acme_headers.h"
}

Acme::func( );

If that doesn't work, try:

namespace Acme
{
#include "acme_headers.h"
}

using namespace Acme;

func( );

It's just a band-aid, of course, but at least it's explicit.

Jul 22 '05 #2
Jeff Schwab wrote:

Leon wrote:
Hi all.

For my application I'm using a third-party library which is supplied as a
library file (.lib) and an associated header file to prototype the functions
defined in the library. Important is that the library is already compiled.

Now, this library defines only functions, and they are declared in the
global namespace. Since this is rather ugly and inconvenient, I want to
place these functions in their own namespace.

Is it possible to change the fully qualified names of these functions if
they're already compiled, or maybe is there another way to get these
functions in their own namespace?


I've usually gotten away with this:

namespace Acme
{
#include "acme_headers.h"
}

Acme::func( );

If that doesn't work, try:

namespace Acme
{
#include "acme_headers.h"
}

using namespace Acme;

func( );

It's just a band-aid, of course, but at least it's explicit.


Can't work. Acme::func is a different function from ::func. The library
defines 'extern "C" func", so calls to Acme::func won't link.

Even if you can recompile the library this probably won't work: the
behavior of a program that includes any standard headers inside a
namespace declaration is undefined, and chances are good that
"acme_headers.h" will do just that.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #3
Leon wrote:

Hi all.

For my application I'm using a third-party library which is supplied as a
library file (.lib) and an associated header file to prototype the functions
defined in the library. Important is that the library is already compiled.

Now, this library defines only functions, and they are declared in the
global namespace. Since this is rather ugly and inconvenient, I want to
place these functions in their own namespace.

Is it possible to change the fully qualified names of these functions if
they're already compiled, or maybe is there another way to get these
functions in their own namespace?


Nope. If you like busywork, though, you could write wrapper functions
inside a namespace that call the global ones in the library. But you
really haven't accomplished anything: the globals are still global.
Don't fight the library. Use it the way it's written, or rewrite it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #4

"Jeff Schwab" <je******@comcast.net> schreef in bericht
news:Iq********************@comcast.com...
Leon wrote:
Hi all.

For my application I'm using a third-party library which is supplied as a library file (.lib) and an associated header file to prototype the functions defined in the library. Important is that the library is already compiled.
Now, this library defines only functions, and they are declared in the
global namespace. Since this is rather ugly and inconvenient, I want to
place these functions in their own namespace.

Is it possible to change the fully qualified names of these functions if
they're already compiled, or maybe is there another way to get these
functions in their own namespace?


I've usually gotten away with this:

namespace Acme
{
#include "acme_headers.h"
}

Acme::func( );

If that doesn't work, try:

namespace Acme
{
#include "acme_headers.h"
}

using namespace Acme;

func( );

It's just a band-aid, of course, but at least it's explicit.


Hmm, ok, but if I'm correct this construction will produce link errors,
since the functions are now prototyped in the Acme namespace, but still
defined in the global namespace (since that's where they were defined when
the .lib was compiled).
When linking, the compiler will not be able to find the definitions for
Acme::func(), only for ::func() ?

Or am I missing something here?

Cheers,
Leon.
Jul 22 '05 #5

Pete Becker skrev i meldingen <40***************@acm.org>...
Jeff Schwab wrote:


I've usually gotten away with this:

namespace Acme
{
#include "acme_headers.h"
}

Acme::func( );

If that doesn't work, try:

namespace Acme
{
#include "acme_headers.h"
}

using namespace Acme;

func( );

It's just a band-aid, of course, but at least it's explicit.
Can't work. Acme::func is a different function from ::func. The library
defines 'extern "C" func", so calls to Acme::func won't link.


Not necessarily. I participated in a discussion about this many
years ago, and just as an investigation wrote some simple applications
using the Windows API placed in a namespace. Linking wasn't a problem,
but all the macro definitions in the header files were...

In short it's a maintainance nightmare, even where it can be made
to work.

And for header files of the size of the Windows API, say, there's no
practical way to put everything in a namespace at once; instead, if
one chooses to do this thing it's necessary to add more and more (e.g.
macro workarounds) as the need arises.

Even if you can recompile the library this probably won't work: the
behavior of a program that includes any standard headers inside a
namespace declaration is undefined, and chances are good that
"acme_headers.h" will do just that.


Yep.

One non-standard workaround (with formally undefined effect) is to
include the basic C library headers before including the library
header(s). For the Windows API using one particular vendor's headers
that turned out to be just a handful of C library headers. I think it
was four.

Not that I recommend the approach, of course.

Jul 22 '05 #6
"Alf P. Steinbach" wrote:

Pete Becker skrev i meldingen <40***************@acm.org>...
Jeff Schwab wrote:


I've usually gotten away with this:

namespace Acme
{
#include "acme_headers.h"
}

Acme::func( );

If that doesn't work, try:

namespace Acme
{
#include "acme_headers.h"
}

using namespace Acme;

func( );

It's just a band-aid, of course, but at least it's explicit.


Can't work. Acme::func is a different function from ::func. The library
defines 'extern "C" func", so calls to Acme::func won't link.


Not necessarily. I participated in a discussion about this many
years ago, and just as an investigation wrote some simple applications
using the Windows API placed in a namespace. Linking wasn't a problem,
but all the macro definitions in the header files were...


I don't see how that could work. Except, perhaps, with a broken
compiler. The point of typesafe linking is that

int f();

can't be mistaken for

namespace wrapper
{
int f();
}

If a compiler generates the same name for both there's something wrong.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7
Leon wrote:

Hmm, ok, but if I'm correct this construction will produce link errors,
since the functions are now prototyped in the Acme namespace, but still
defined in the global namespace (since that's where they were defined when
the .lib was compiled).
When linking, the compiler will not be able to find the definitions for
Acme::func(), only for ::func() ?

Or am I missing something here?


Nope. I suspect he was answering a different question. If you can
rebuild the library, then wrapping the headers in a namespace might
work. It's still a bad idea, though.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #8
Pete Becker skrev i meldingen <40***************@acm.org>...
"Alf P. Steinbach" wrote:

Pete Becker skrev i meldingen <40***************@acm.org>...
>Jeff Schwab wrote:
>>
>>
>> I've usually gotten away with this:
>>
>> namespace Acme
>> {
>> #include "acme_headers.h"
>> }
>>
>> Acme::func( );
>>
>> If that doesn't work, try:
>>
>> namespace Acme
>> {
>> #include "acme_headers.h"
>> }
>>
>> using namespace Acme;
>>
>> func( );
>>
>> It's just a band-aid, of course, but at least it's explicit.
>
>Can't work. Acme::func is a different function from ::func. The library
>defines 'extern "C" func", so calls to Acme::func won't link.


Not necessarily. I participated in a discussion about this many
years ago, and just as an investigation wrote some simple applications
using the Windows API placed in a namespace. Linking wasn't a problem,
but all the macro definitions in the header files were...


I don't see how that could work. Except, perhaps, with a broken
compiler. The point of typesafe linking is that

int f();

can't be mistaken for

namespace wrapper
{
int f();
}

If a compiler generates the same name for both there's something wrong.


You're right that the compiler I used was seriously broken... ;-)

On the other hand, I'm not so sure that there's necessarily something
wrong when f is declared 'extern "C"', since that syntactic device is
there precisely in order to be able to link with non-type-safe C functions
(in effect, much simpler name mangling, perhaps just an underscore).

Right now I don't have access to any C++ compiler, nor my old
code, so unfortunately I cannot check whether e.g. MSVC 7.1 still supports
the hack, but I very much suspect that it does, and I suspect (but am
not at all 100% sure) that any conforming compiler must so long as
standard headers are not placed into a namespace.

But again, just in case someone Googles and finds just this posting,
that technique is not something I'd recommend...

Jul 22 '05 #9
"Alf P. Steinbach" wrote:

On the other hand, I'm not so sure that there's necessarily something
wrong when f is declared 'extern "C"', since that syntactic device is
there precisely in order to be able to link with non-type-safe C functions
(in effect, much simpler name mangling, perhaps just an underscore).


Well, that's a surprise: apparently defining an extern "C" function
inside a namespace is equivalent to defining it in the global namespace.
I don't see any support for that in the standard, but the compilers I
tried it with seem to do that, so I'm probably overlooking something.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #10
Pete Becker wrote:
Leon wrote:
Hmm, ok, but if I'm correct this construction will produce link errors,
since the functions are now prototyped in the Acme namespace, but still
defined in the global namespace (since that's where they were defined when
the .lib was compiled).
When linking, the compiler will not be able to find the definitions for
Acme::func(), only for ::func() ?

Or am I missing something here?

Nope. I suspect he was answering a different question. If you can
rebuild the library, then wrapping the headers in a namespace might
work. It's still a bad idea, though.

I believe I was answering the right question. This approach really has
worked for me, even when I couldn't recompile the existing library. I
don't have an explanation, though.

Jul 22 '05 #11
On Sun, 11 Jan 2004 17:07:42 -0500, Pete Becker <pe********@acm.org>
wrote:
"Alf P. Steinbach" wrote:

On the other hand, I'm not so sure that there's necessarily something
wrong when f is declared 'extern "C"', since that syntactic device is
there precisely in order to be able to link with non-type-safe C functions
(in effect, much simpler name mangling, perhaps just an underscore).


Well, that's a surprise: apparently defining an extern "C" function
inside a namespace is equivalent to defining it in the global namespace.
I don't see any support for that in the standard, but the compilers I
tried it with seem to do that, so I'm probably overlooking something.


7.5/6 says that an extern "C" declaration refers to the same function
regardless of the namespace it appears in.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #12
tom_usenet wrote:

7.5/6 says that an extern "C" declaration refers to the same function
regardless of the namespace it appears in.


Thanks.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bryant Huang | last post by:
Hello! I would like to read in files, during run-time, which contain plain Python function definitions, and then call those functions by their string name. In other words, I'd like to read in...
7
by: zbyszek | last post by:
I am working with a large C++ program which, for reasons of backward compatibility, uses C's printf and fprintf rather than iostreams. For a certain type of build I want to provide new functions...
25
by: JeffS | last post by:
Honest, I scoured the comp.lang.c.faq for this but found nothing. :) Is there a library function for placing the cursor position in the console? Or is it something that can only be done with a...
2
by: Empire City | last post by:
Can someone tell me what is the difference in placing the using in the namespace{} and before the namespace{} ? namespace MyProj { using System; using System.Web; } using System;
2
by: Dickyb | last post by:
Extracting an Icon and Placing It On The Desktop (C# Language) I constructed a suite of programs in C++ several years ago that handle my financial portfolio, and now I have converted them to...
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
5
by: Digital Puer | last post by:
Hi, I've inherited a bunch of C code that needs to be called from a C++ framework, so I thought it would be good to put these C functions into C++ classes for better organisation. What are best...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
6
by: Marco | last post by:
One of the things I like about C++ is that it doesn't force you to create fake "objects" in the OO sense. We had a debate at the office about the "right" way to encapsulate related utility...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.