473,395 Members | 1,696 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,395 software developers and data experts.

Weak Symbols

Hi all,
does C++ give a way to declare a specific symbol as a "weak symbol"?
In 'C', one can use #pragma directive.
Is this still a legal way in C++ ? Is there a (better?) alternative?

Nov 22 '05 #1
9 8209

"Neelesh Bodas" <ne***********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi all,
does C++ give a way to declare a specific symbol as a "weak symbol"?
What is a 'weak symbol'?
In 'C', one can use #pragma directive.
How so?
Is this still a legal way in C++ ? Is there a (better?) alternative?


Specifically what are you trying to do?

-Mike


Nov 22 '05 #2
Mike Wahler wrote:
What is a 'weak symbol'?
A weak symbol is one which can have legal mutiple definitions across
compilation units.
For example , suppose you have :
//a.c
int fun(int a)
{
return a;
}

//b.c

int fun(int b)
{
return 2*b;
}

Now these files in general cannot be linked together since linker will
complain about multiple definitions of "fun"
In 'C', one can use #pragma directive.
How so?


So if you add the line
#pragma weak fun
to either or both of these files, then it means that the symbol fun
_can_ be multiply defined across compilation units, and the current
definition (which is marked as "weak") will be safely ignored by the
linker. (Of course this is implementation dependent, but each
implementation has its own way. I am talking about gcc in this
example.)
Is this still a legal way in C++ ? Is there a (better?) alternative?


Specifically what are you trying to do?


So specifically what I am trying to ask is whether there is a
language-level support for this concept of "multiple definitions across
compilation units?"
I believe that the "#pragma" issue is compiler dependent. So I was
searching for any such compiler-independent way.

May be this post is a bit off topic. Apologies in that case, I will
post on an approriate group.

Nov 22 '05 #3
In article <11**********************@g47g2000cwa.googlegroups .com>,
"Neelesh Bodas" <ne***********@gmail.com> wrote:
So specifically what I am trying to ask is whether there is a
language-level support for this concept of "multiple definitions across
compilation units?"
I believe that the "#pragma" issue is compiler dependent. So I was
searching for any such compiler-independent way.


Isn't this what namespaces are for?
Nov 22 '05 #4
* Neelesh Bodas:
Mike Wahler wrote:
What is a 'weak symbol'?


A weak symbol is one which can have legal mutiple definitions across
compilation units.
For example , suppose you have :
//a.c
int fun(int a)
{
return a;
}

//b.c

int fun(int b)
{
return 2*b;
}

Now these files in general cannot be linked together since linker will
complain about multiple definitions of "fun"
In 'C', one can use #pragma directive.


How so?


So if you add the line
#pragma weak fun
to either or both of these files, then it means that the symbol fun
_can_ be multiply defined across compilation units,


That is not standard C (it's a gcc extension), and the explanation is
incorrect: what it does is allow you to create a library function that
can be "overridden" by a definition in user code, a non-weak symbol.

If what you want is to use the same function name and signature for
different functions in different compilation units, then in in standard
C you can use the keyword 'static', and in standard C++ you can
additionally use a namespace, e.g. an anonymous one.

Alternatively, if you guarantee that all definitions are exactly the
same, then in standard C++ you can use the keyword 'inline'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #5
Alf P. Steinbach wrote:
* Neelesh Bodas:
Mike Wahler wrote:
What is a 'weak symbol'?
A weak symbol is one which can have legal mutiple definitions across
compilation units.
For example , suppose you have :
//a.c
int fun(int a)
{
return a;
}

//b.c

int fun(int b)
{
return 2*b;
}

Now these files in general cannot be linked together since linker will
complain about multiple definitions of "fun"

> In 'C', one can use #pragma directive.

How so?


So if you add the line
#pragma weak fun
to either or both of these files, then it means that the symbol fun
_can_ be multiply defined across compilation units,


That is not standard C (it's a gcc extension), and the explanation is
incorrect: what it does is allow you to create a library function that
can be "overridden" by a definition in user code, a non-weak symbol.

If what you want is to use the same function name and signature for
different functions in different compilation units, then in in standard
C you can use the keyword 'static', and in standard C++ you can
additionally use a namespace, e.g. an anonymous one.


Well it's not really "additionally". In C++, using static to specify
internal linkage is deprecated. Anonymous namespaces *should* be used
instead.
Alternatively, if you guarantee that all definitions are exactly the
same, then in standard C++ you can use the keyword 'inline'.

Jonathan

Nov 22 '05 #6
* Jonathan Mcdougall:
* Alf P. Steinbach:

If what you want is to use the same function name and signature for
different functions in different compilation units, then in in standard
C you can use the keyword 'static', and in standard C++ you can
additionally use a namespace, e.g. an anonymous one.
Well it's not really "additionally". In C++, using static to specify
internal linkage is deprecated.


I'm sorry, that's incorrect.

Use of static is deprecated for declaring objects in namespace scope.

Not for functions.

Anonymous namespaces *should* be used instead.


We all have our preferences... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #7
Alf P. Steinbach wrote:
* Jonathan Mcdougall:
* Alf P. Steinbach:

If what you want is to use the same function name and signature for
different functions in different compilation units, then in in standard
C you can use the keyword 'static', and in standard C++ you can
additionally use a namespace, e.g. an anonymous one.


Well it's not really "additionally". In C++, using static to specify
internal linkage is deprecated.


I'm sorry, that's incorrect.

Use of static is deprecated for declaring objects in namespace scope.

Not for functions.


A0.1.2 The use of the static keyword is deprecated when declaring
objects in namespace scope (see _basic.scope.namespace_).

Well, I think I stand corrected. Somehow, I knew I couldn't be right.
:)

Jonathan

Nov 22 '05 #8
Neelesh Bodas wrote:
So specifically what I am trying to ask is whether there is a
language-level support for this concept of "multiple definitions across
compilation units?"


namespace?

Otherwise, you are asking for an extension to C that someone added to their
compiler to deal with that language's lack of namespaces.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Nov 22 '05 #9

Neelesh Bodas wrote:
Mike Wahler wrote:
What is a 'weak symbol'?


A weak symbol is one which can have legal mutiple definitions across
compilation units.


Or no definition at all.
#pragma weak fun
void fun(void);
if(fun)......

This is good for simulating inheritance and function overriding in C,
but C++
has that already.

class Base{
public:
virtual void fun(void)=0;
};

........
void (Base* b)
{
b->fun();
//.......
}
................

As a side note some version of libpthread under linux has
pthread_create and
I guess rest of pthread_xxx functions as weak symbols.
One poor programer forgot to link the library and of course linker
does not complains, effectivelly making every call to a pthread_create
into a
(0)(......) (call to pure virtual function :) which produced segfault.
stack trace easilly showed what's going on.

Greetings, Bane.

Nov 22 '05 #10

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

Similar topics

94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
1
by: aerobar2 | last post by:
When I debug my application I get "no symbols loaded" ================= 'DefaultDomain': Loaded 'c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded. 'MHSFire': Loaded...
1
by: asdf | last post by:
Hello, I was enjoying working in VS for half a year without any problems and now I cannot debug anymore. Without any really reason my Studio tells me that the page that I want to debug has - No...
4
by: Vinay | last post by:
Hello My question is regarding "weak external symbols". Consider the following eg. class test { public : int func1(void); {cout <<"func1";} int func2(void);
3
by: memememe | last post by:
I see weak reference on the .net api, but I do not see soft or phantom, are they supported on .net?
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
3
by: John Nagle | last post by:
Are weak refs slower than strong refs? I've been considering making the "parent" links in BeautifulSoup into weak refs, so the trees will release immediately when they're no longer needed. In...
1
by: simbasaurus | last post by:
Hello! I built a shared object on linux from several object files. In one of the files i redefined the global new and delete C++ operators. I deffinately do not want these operators to be seen...
1
by: Henri.Chinasque | last post by:
Hi all, I've been considering that my objects should subscribe to an event via a weak reference, however I've found several warnings that this approach comes with concurrency considerations,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.