LinuxQuestions.org
Review your favorite Linux distribution.
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-12-2014, 08:49 AM   #1
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Rep: Reputation: Disabled
multiple declaration error


I have a case of interlocking subroutines.
Code:
int mouse[2];
void doit();
void submouse(int btn, int state, int x, int y){
  mouse[0] = x;
  mouse[1] = y;
  doit();
}
void newwindow(){
  subWindow[1][0] = glutCreateSubWindow(mainWindow, 10, 10, 616, 274);
  windowlevel = 1;
  glutMouseFunc(submouse);
}

void doit(){
  // if subwindow has not been created, create subwindow
  if(windowlevel == 0)newwindow();
  else{ //process whatever the mouse clicked
  }
}
In other words, doit() calls newwindow() which creates submouse() which calls doit().
However, that coding produces the following error message:
Quote:
program.c:4418:6: error: static declaration of ‘doit’ follows non-static declaration
program.c:3745:6: note: previous declaration of ‘doit’ was here
What am I doing wrong?
 
Old 09-12-2014, 09:32 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
It's saying that your actual code has the "static" keyword in one place but not the other:

Code:
static 
void doit() {
 
Old 09-12-2014, 12:21 PM   #3
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Original Poster
Rep: Reputation: Disabled
I do not use the "static" keyword in either place.
The latest version of gcc for my i686 machine (Debian stable) is 4:4.7.2-1. The current for my AMD64 machine is 4:4.8.2-4
This program has compiled without error on the AMD64 for the past year.

Why is the 4:4.7.2-1 giving me static (sic), and how do I get it to compile?

Quote:
Originally Posted by smallpond View Post
It's saying that your actual code has the "static" keyword in one place but not the other:

Code:
static 
void doit() {
 
Old 09-12-2014, 01:27 PM   #4
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Are you sure 'doit()' is only declared once in your program (check other files as well).

P.S. doit is not the best name for a function.
 
Old 09-12-2014, 03:43 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Just check the lines the compiler mentioned.
(btw this program is bigger than good -- the right size is a few hundred lines, not five thousands)
 
Old 09-12-2014, 05:25 PM   #6
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Original Poster
Rep: Reputation: Disabled
doit() is declared twice: once as a placeholder, and the second time with its 230 lines of code. The placeholder is necessary since I have interlocking subroutines.

doit() gets called whenever a mouse gets clicked. It sorts out which window (of currently about a dozen) was under the mouse when it got clicked, and then calls the appropriate subroutine for execution of the mouse-picked function. Note: doit() is not static and not recursive: It is called the first time per mouse click on the main window, whereupon it creates an appropriate subwindow. It then goes away until the mouse is clicked on that subwindow. Think of doit() as a universal state monitor. Including header files, the program is currently up to ~7000 lines of C & OpenGL, and growing.

Quote:
Originally Posted by metaschima View Post
Are you sure 'doit()' is only declared once in your program (check other files as well).

P.S. doit is not the best name for a function.
 
Old 09-12-2014, 05:43 PM   #7
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Either rename the doit function or make it static (prototype and declaration). I suspect it may be declared in an include file somewhere. If you make it static it will override the previous declaration. Not the best solution, renaming it would be better.
 
Old 09-12-2014, 10:18 PM   #8
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Original Poster
Rep: Reputation: Disabled
See bug report http://gcc.gnu.org/ml/gcc-bugs/2004-12/msg00415.html
The declaration of a function should match the prototype of that declaration. I am seeing that bug error in gcc 4:4.7.2-1 but not 4:4.8.2-4
In other words, it is a compiler error, not a coding error.
 
Old 09-13-2014, 12:29 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
So, this function was declared as 'static' even if you stated otherwise in #3.
The definition and the declaration should be identical, even if some compilers (or compiler versions) don't give error if they aren't.

(Also you should do a little reorganization, eg:

old:
static void doit ();

new:
static int dispatch_mouse_event ();
)
 
Old 09-13-2014, 07:39 AM   #10
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Original Poster
Rep: Reputation: Disabled
1. either "static void doit()" or "void static doit()" results in an error message, "invalid storage class for function doit". Apparently this is a bug: http://www.geeksforgeeks.org/what-ar...unctions-in-c/
2. Upon research, my use of the prototype of doit is not only acceptable practice, but is actually recommended. The bug appeared when gcc started requiring that the arguments of the prototype exactly match the arguments of the coded function. Gcc version 3.* allowed an empty argument field, which could be promoted by the coded version of the function.

Quote:
Originally Posted by NevemTeve View Post
So, this function was declared as 'static' even if you stated otherwise in #3.
The definition and the declaration should be identical, even if some compilers (or compiler versions) don't give error if they aren't.

(Also you should do a little reorganization, eg:

old:
static void doit ();

new:
static int dispatch_mouse_event ();
)
 
Old 09-13-2014, 09:47 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Sure, the complete code should include the parameters, too:

Code:
static void doit (void);
...
static void doit (void)
{...}
but, as I of said, it should be redesigned, eg:

Code:
typedef struct MouseEvent {
    int button;
    int state;
    int x;
    int y;
} MouseEvent;

static void dispatch_mouse_event (const MouseEvent *me);

static void submouse(int btn, int state, int x, int y)
{
  MouseEvent me;

  me.button= button;
  me.state= state;
  me.x= x;
  me.y= y;
  dispatch_mouse_event (&me);
}

static void dispatch_mouse_event (const MouseEvent *me)
{
  // if subwindow has not been created, create subwindow
  if(windowlevel == 0)newwindow();
  else{ //process whatever the mouse clicked
  }
}
 
Old 09-13-2014, 10:45 AM   #12
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The '-pedantic' did it, that's why I never use it.
 
Old 09-13-2014, 03:22 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by piobair View Post
See bug report http://gcc.gnu.org/ml/gcc-bugs/2004-12/msg00415.html
The declaration of a function should match the prototype of that declaration. I am seeing that bug error in gcc 4:4.7.2-1 but not 4:4.8.2-4
In other words, it is a compiler error, not a coding error.
The compiler error in that bug report is that the compiler fails to reject or at least warn about the code.
Quote:
https://gcc.gnu.org/ml/gcc-bugs/2004-12/msg00418.html

The C standard says this is invalid code which is why 4.0.0 errors out.
 
Old 09-13-2014, 11:09 PM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Standards come and go, compilers may or may not follow their changes; eventually, it's the programmer who is responsible for his code.
 
  


Reply



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
error: multiple types in one declaration muby Programming 8 02-03-2011 05:13 PM
[SOLVED] C++ multiple variable declaration error, even after I set #ifndef preprocessor chinho Programming 4 01-18-2011 04:37 AM
declaration error trying to use flex with c++ hedpe Programming 1 03-30-2009 07:15 PM
ERROR previos declaration raixun Programming 2 08-28-2008 04:40 AM
error: ‘for’ loop initial declaration nasim751 Linux - Software 2 06-07-2008 05:35 AM

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

All times are GMT -5. The time now is 12:58 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