overload new in shared library (crazy)

S

shahehe

Hi

I have overloaded new in a shared library (s1.so) sucessfully. but I do
not what main program and any other shared library to be able to use
this new operator. In other words, I do not operator new to be globally
used.

I tried some ld options -Bsymbolic as follows

g++ -shared -Wl,-Bysmbolic f1.o -o s1.so
g++ -lstdc++ main.o s1.so -o main

However, if I put -lstdc++ after s1.so, the overloaded new being used
by main (which is not what I want )

Any comments?

thanks
yan
 
V

Victor Bazarov

I have overloaded new in a shared library (s1.so) sucessfully. but I
do not what main program and any other shared library to be able to
use this new operator. In other words, I do not operator new to be
globally used.

I tried some ld options -Bsymbolic as follows

g++ -shared -Wl,-Bysmbolic f1.o -o s1.so
g++ -lstdc++ main.o s1.so -o main

However, if I put -lstdc++ after s1.so, the overloaded new being used
by main (which is not what I want )

Any comments?

For compiler- and platform- specific questions please ask in the newsgroups
for that compiler or that platform. "Shared library" is a concept undefined
in C++. "ld options" is specific to your compiler and/or platform.

Usually, a way to limit the use of the overloaded operator 'new' is to give
it an additional argument, similar to

// header
class my_new_identifier {}; // just some unique type
extern my_new_identifier mynew;

// implementation
my_new_identifier mynew;
void* operator new(size_t howmuch, my_new_identifier&) {
... // same as before
}

// somewhere in the code
SomeClass* ptr = new SomeClass(args); // regular 'new'
SomeClass* ptr2 = new (mynew) SomeClass(args); // your 'new'

Well, you get the idea. Disclaimer: I didn't test this.

V
 
A

Aman Angrish

gcc has an option for controlling symbol visibility.
You need to declare your overloaded function with
__attribute__ ((visibility("hidden")))
My gcc compiler is not yet configured to use the "visibility" option and
I couldn't try it out.
good luck.
-Aman.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top