LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2013, 12:49 AM   #1
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
Why must a function with weak attribute be declared before it's first use? (In gcc)


If you don't, you get this warning....
warning: weak declaration of ‘foo’ after first use results in
unspecified behavior

I know moving it before it's first use stops the warning and works....
what I don't understand is why.
 
Old 09-11-2013, 03:16 AM   #2
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
If mutually recursive functions are coded without being declared then at least one of the functions will be called before it has been defined.

A decent compiler should still be able to sort that out though.
 
Old 09-11-2013, 03:44 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. Always declare your functions before you use them.
2. The compiler may generate different code if it knows that the called function is weak.

Last edited by NevemTeve; 09-11-2013 at 08:16 AM. Reason: No comma before 'if'
 
Old 09-11-2013, 07:23 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,642
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
"Always declare your functions before you use them," regardless.
 
Old 09-11-2013, 04:08 PM   #5
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Original Poster
Rep: Reputation: 87
Quote:
Originally Posted by NevemTeve View Post
1. Always declare your functions before you use them.
I think most of the responses in this thread are missing the question slightly... so let me elaborate.

====a.h====

void a(void);

===compiles without warning.c====
#include "a.h"
//Provide weak stub....
void __attribute__((weak)) a(void)
{
}

void b(void)
{
// Invoke a
a();
}

===compiles with warning.c====
#include "a.h"
void b(void)
{
// Invoke a
a();
}

// Get warning: weak declaration of ‘a’ after first use results in
unspecified behavior
void __attribute__((weak)) a(void)
{
}


Quote:
2. The compiler may generate different code if it knows that the called function is weak.
I guess is that is the heart of the question... why would it?
 
  


Reply

Tags
function_attribute, gcc, weak



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc warn_unused_result attribute r.stiltskin Programming 8 12-06-2011 12:40 AM
update problem "Attribute error object has no attribute 'rsplit' ruse Linux - Newbie 0 08-28-2009 01:46 AM
some question about the __user_walk function declared in linux-2.4.20/fs/namei.c ao.yuan.young Linux - Kernel 5 05-19-2009 08:52 PM
no member function declared in class...weird behavior... jaepi Programming 1 12-11-2007 10:23 PM
Howto access a variable outside the function from where it is declared in C daYz Programming 4 03-09-2007 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration