implicit function declarations (and other ridiculous error messages)
Author |
Message |
HyperFlexed

|
Posted: Fri Aug 06, 2010 11:23 pm Post subject: implicit function declarations (and other ridiculous error messages) |
|
|
I had a large body of code, and decided to break it up into smaller logical chunks. The program worked perfectly before I separated it into multiple files, but now the errors I'm getting make no sense at all. The code can be found here: http://github.com/hyperflexed/gnuquit
When I try compiling:
code: | gcc -std=gnu99 main.c quit-attempt.c quit-io.c -o main -g -Wall |
I get:
code: | In file included from quit-attempt.h:30,
from main.c:30:
quit-io.h:24: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
main.c: In function 'main':
main.c:62: warning: implicit declaration of function 'parseLogFile'
main.c:62: warning: initialization makes pointer from integer without a cast
In file included from quit-attempt.h:30,
from quit-attempt.c:20:
quit-io.h:24: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token |
as for the error in quit-io.h:24, I have no idea what it's talking about. I don't see why any of those tokens would come before whichever '*' it's referring to. I'm not missing any semi-colons or commas as far as I can see.
the "implicit declaration of function parseLogFile' makes no sense either, as it's called at main.c:62, and I've made sure to include quit-io.h in main.c. quit-io.h it has a function prototype for parseLogFile, and the full definition is in quit-io.c.
I'm not sure what 'initialization makes pointer from integer without a cast' is referring to, but seeing as it's the only error I can't interpret, my guess is that it's the root cause of these issues. I don't believe I have any circular includes as I've made use of include guards for every include.
if anyone has time to have a peek at the lines causing errors, I'd greatly appreciate it. I've spent hours trying to work this out, and I can't even use gdb because it won't compile.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Sat Aug 07, 2010 11:48 am Post subject: RE:implicit function declarations (and other ridiculous error messages) |
|
|
"initialization makes pointer from integer without a cast" is a warning, not an error. The code would compile if it was just that.
So there's just a parser error. Maybe there's a problem with your typedef / structs; maybe some code above placed the parser into an unexpected state... those problems could be difficult to track down.
Also
code: |
#ifndef QUIT_ATTEMPT_H
#define QUIT_ATTEMPT_H
#include "quit-attempt.h"
#endif
|
The #ifndef typically goes inside the header file itself, so that you don't have to worry about this everywhere and just include whatever you need. <stdio.h> certainly has its own set of inclusion definitions.
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
DemonWasp
|
Posted: Sat Aug 07, 2010 1:46 pm Post subject: RE:implicit function declarations (and other ridiculous error messages) |
|
|
In C, it's generally highly likely that the first error spat out by the parser is the root cause of some or all of the other errors. You cannot trust the other errors reported to be accurate. Also, line numbers are more like suggestions than facts.
Always fix the first error listed first, re-testing after each fix.
There is a problem in quit-io.h, possibly at or before line 24. If you post quit-io.h, chances are we can spot it pretty easily.
|
|
|
|
|
 |
HyperFlexed

|
Posted: Sat Aug 07, 2010 1:51 pm Post subject: Re: implicit function declarations (and other ridiculous error messages) |
|
|
code: | /**
This file is part of GNUQuit.
GNUQuit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNUQuit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUQuit. If not, see <http://www.gnu.org/licenses/>.
**/
#ifndef QUIT_ATTEMPT_H
#define QUIT_ATTEMPT_H
#include "quit-attempt.h"
#endif
char **logLineSplit(char *logLine);
quit_attempt_t **parseLogFile(FILE *logfile, int *numQuitAttempts, int *qaArrSize); |
line 24 is the one that says:
code: | quit_attempt_t **parseLogFile(FILE *logfile, int *numQuitAttempts, int *qaArrSize); |
|
|
|
|
|
 |
DemonWasp
|
Posted: Sat Aug 07, 2010 6:00 pm Post subject: RE:implicit function declarations (and other ridiculous error messages) |
|
|
Alright, so I took the time to fix your code (and it runs alright once corrected). I could give you the source code, but there's only a few things that you need to do:
1. Refactor every one of your #includes.
You should have the #ifndef / #define stuff inside each header file. It looks like this:
stuff.h
code: |
#ifndef STUFF_H
#define STUFF_H
#include "otherstuff.h"
#endif STUFF_H
|
stuff.c
file-that-needs-stuff.h
You'll find that this is simpler and easier and less bug-prone (your way, one single typo could mean hours of compiler / linker problems because you're checking or defining the wrong variable).
2. Remove circular definitions.
Right now, you have the following:
- quit-io needs quit-attempt's type definitions
- quit-attempt needs quit-io's function definitions
Since the includes happen before the type definitions and the function definitions, neither has access to the other. This is the cause of the bizarre expected '=', ',', ';', 'asm' or '__attribute__' before '*' token errors.
The simplest way to resolve this is to make a new file:
quit-types.h
code: |
typedef struct datetime {
unsigned int year;
unsigned int month;
unsigned int day;
unsigned int hour;
unsigned int minute;
unsigned int second;
} datetime_t;
typedef struct quit_attempt {
int id;
datetime_t start;
datetime_t end;
} quit_attempt_t;
|
Then remove the type definitions from quit-attempt.h and replace them with #include "quit-types.h". This will allow the code in both quit-attempt.h and quit-io.h to use the types declared in quit-types.h.
3. Remove the static keyword.
I'm not sure what you were trying to do with your use of static in some function definitions, but rest assured it is unnecessary. Remove it.
The above should fix all compilation issues you currently have and produce a working executable.
|
|
|
|
|
 |
HyperFlexed

|
Posted: Sat Aug 07, 2010 7:38 pm Post subject: Re: RE:implicit function declarations (and other ridiculous error messages) |
|
|
I've refactored my code (latest version at: http://github.com/hyperflexed/gnuquit), but I have 2 errors left.
in main.c I had to "#define QUIT_TYPES_H" before including to prevent multiple inclusions. This is the only thing I've done that flies in the face of your advice.
still, I have 2 errors left:
code: | johnny@picard:~/coding/gnuquit$ gcc -std=gnu99 main.c quit-attempt.c quit-io.c -o main
In file included from quit-attempt.h:20,
from quit-attempt.c:18:
quit-io.h:31: error: expected ')' before '*' token
In file included from quit-io.c:18:
quit-io.h:31: error: expected ')' before '*' token
quit-io.c:46: error: expected ')' before '*' token |
Code:
quit-attempt.h
quit-attempt.c
quit-io.c
quit-io.h
The error looks a little friendlier at this point, and I will continue chipping away but it doesn't seem as though all my problems are yet solved. The compiler seems to be choking exclusively on the quit_attempt_t **parseLogFile function.
|
|
|
|
|
 |
DemonWasp
|
Posted: Sat Aug 07, 2010 9:01 pm Post subject: Re: implicit function declarations (and other ridiculous error messages) |
|
|
You seem to have missed several of my points above.
First, forget the way you're currently using #ifndef and #define. That is the wrong way.
Place those guards around the contents of header files, so that when the header file is included it will only appear the first time.
Second, each of your header files should #include anything and everything they rely on. You can check that this is true by something like:
gcc -std=gnu99 quit-attempt.c
This should return no compiler errors, only linker errors (because it can't find function definitions).
Third, always always always include system includes (the angle-bracket ones) BEFORE project includes (your files, the ones with quotes). Always.
I have attached the code that I got to compile as an example.
Description: |
|
 Download |
Filename: |
quit-code.zip |
Filesize: |
488.58 KB |
Downloaded: |
292 Time(s) |
|
|
|
|
|
 |
HyperFlexed

|
Posted: Sat Aug 07, 2010 10:57 pm Post subject: Re: implicit function declarations (and other ridiculous error messages) |
|
|
Now I get it.
when I saw you said:
code: | #ifndef STUFF_H
#define STUFF_H
#include "otherstuff.h"
#endif STUFF_H |
I didn't pick up on the implied:
code: | #ifndef STUFF_H
#define STUFF_H
#include "otherstuff.h"
// AND the rest of your code for the header
#endif STUFF_H |
I see how this would make for cleaner includes, and protect against multiple definitions if the code were ever modified in the future. The entire header is protected against duplication, not just the includes in the header. I owe you many thanks Forgive me if I don't learn so quickly, I'm eager, but quite inexperienced with programming.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DemonWasp
|
Posted: Mon Aug 09, 2010 11:13 am Post subject: RE:implicit function declarations (and other ridiculous error messages) |
|
|
Glad you've got it all figured out now. Includes are a non-trivial thing in C, and one of the things that tends to make programming in C just a little more difficult than in more forgiving languages.
|
|
|
|
|
 |
|
|