bool function declaration in header file. Codeblocks tells me I forgot to include stdbool.h
Code:
#include <string.h> Codeblocks Code:
error: unknown type 'bool' Code:
note: 'bool' is defined in header '<stdbool.h>'; did you forget to '#include <stdbool.h>'? Each of the errors in my build messages references the header file. |
Quote:
|
I was trying to test some. For the moment I changed them to int based 1 / 0 for true false. The sledgehammer approach I suppose.
For what it's worth I tried using _Bool but it didn't register that either. Also for reference the only failure was in the header file. Code:
bool function_name(char * str); |
Quote:
If you declare a function that returns a bool type then type bool must be defined before the function is declared. If you declare that function in a header file then stdbool.h must be included in the same scope as the header before the function is declared. Including it in the header file is one way to do that. Added: My comments above considered only preprocessor/compiler errors. If as you clearly say, it is Codeblocks itself reporting the error then maybe it looks at each file in isolation (I do not use codeblocks so do not know what tests it performs). Either way, is there a reason you would not want to add that include to the header file? |
But it was declared in the source file that the header belonged to? Shouldn't it have picked it up from there?
Especially as I had #include "header.h" after the standard libraries? This is what I'm lost on. I figured the previous includes of the standard library stuff would pass through to any headers as my header is last on the list. |
Quote:
But as you say it is a Codeblocks error it makes me think it is not a preprocessor/compiler error, but codeblocks itself which is producing the error and codeblocks might look at it differently, producing the error before the preprocessor sees the code. Can you clarify that? I think the interdependence of headers and other files is usually called the coupling, and the idea is generally to have low coupling. That is, they should not be highly dependent the presence or ordering of other files but should stand alone as much as possible, and hence include the things they themselves depend on. Std headers are idempotent largely for this reason so including them more than once does not affect the result. |
Ok. I'll define in each file as needed. So much to learn. Thank you.
|
This thread "seems strange" and I feel that the actual cause of the problem is somewhere beyond what you have showed us.
|
Fair enough. Here is the most recent changes where I made bool disappear.
https://gitlab.com/jmgibson1981/myli...7e9da17cfaac92 The only thing not included in the git was this. Code:
#include <stdio.h> |
You have compensated for the underlying "problem," through a slew of source-code changes that your successors will one day curse you for, because you did not in fact find it and solve it.
You "made 'the immediate problem' go away," but, because you did not in fact solve it: you made it perpetuate. If I were a manager over any of your teams, I would immediately reject this branch and require you to do it all over again ... "properly." The presence and proper use of a bool data-type is entirely appropriate for this situation, and for the future of this code. You should not now be allowed(!) to "gloss over it." Go out there now and find the "root cause" of this problem. Don't offer a pull-candidate to me until you've found it. |
I fully intend to change it back. I was testing the functionality. I couldn't find the reason after a day or so. Hence my post here. I was under the impression I wasn't supposed to put includes in the header file. Now that I know that I will be reverting.
*EDIT* it is now done, also found other places to make the same change. |
Quote:
Code:
#ifndef libjmstringc |
Quote:
A common use, in my experience the most common use, of header files is to provide the public interface of some particular module, library or application and this appears to be your use case. As such they should normally include everything they require without dependencies on the application source (again, part of the idea of "low or loose coupling" to other modules or files). |
I got it from my own searching. When I had put the bool.h in the header it worked. But when I would google for the proper location of includes the only examples I ever found were at the top of the .c file. Even when I searched specifically for includes in headers it showed a header file with no includes and they were all in the .c file.
Just now changed all my includes to the headers and everything is fine. Just including the header file win my source file. I had it backwards I guess. I'm amazed anything worked before now. |
Quote:
Quote:
Quote:
|
All times are GMT -5. The time now is 08:06 AM. |