LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-12-2023, 03:06 PM   #1
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
bool function declaration in header file. Codeblocks tells me I forgot to include stdbool.h


Code:
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>
#include "lib-jmstring.h"
No includes in the header file for this source file.

Codeblocks

Code:
error: unknown type 'bool'
Code:
note: 'bool' is defined in header '<stdbool.h>'; did you forget to '#include <stdbool.h>'?
Current git. https://gitlab.com/jmgibson1981/mylibraries

Each of the errors in my build messages references the header file.
 
Old 04-12-2023, 04:56 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by jmgibson1981 View Post
Current git. https://gitlab.com/jmgibson1981/mylibraries

Each of the errors in my build messages references the header file.
I don't see any reference to 'bool' in any of your header or source files there, so there must be some more code that is part of the project?
 
Old 04-12-2023, 05:13 PM   #3
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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);
I had 3 of these, 1 error for each. It went away when I manually added the stdbool.h to the header file itself but to my knowledge I shouldn't need to add anything to the header files other than the definitions of the functions.

Last edited by jmgibson1981; 04-12-2023 at 05:19 PM.
 
Old 04-12-2023, 06:06 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by jmgibson1981 View Post
Also for reference the only failure was in the header file.

Code:
bool function_name(char * str);
I had 3 of these, 1 error for each. It went away when I manually added the stdbool.h to the header file itself but to my knowledge I shouldn't need to add anything to the header files other than the definitions of the functions.
All types used in the function declarations must be defined before the function is declared.

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?

Last edited by astrogeek; 04-12-2023 at 06:35 PM. Reason: ptoy
 
Old 04-12-2023, 06:33 PM   #5
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.

Last edited by jmgibson1981; 04-12-2023 at 06:36 PM.
 
Old 04-12-2023, 06:47 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by jmgibson1981 View Post
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.
Calling it "pass thru" may be misleading, but each file is included as it is encountered and the result is called a translation unit, so the std includes which appear before the local header is included will be visible to the local header in the same translation unit.

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.

Last edited by astrogeek; 04-12-2023 at 07:07 PM.
 
Old 04-12-2023, 07:51 PM   #7
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Ok. I'll define in each file as needed. So much to learn. Thank you.
 
Old 04-13-2023, 10:15 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
This thread "seems strange" and I feel that the actual cause of the problem is somewhere beyond what you have showed us.
 
Old 04-13-2023, 02:17 PM   #9
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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>
#include "/home/jason/Documents/Git/mylibraries/lib-jmstring.h"

int main()
{
    char * str = "230.00";
    if (str_is_us_dollar(str)) {
      printf("valid\n");
    } else {
      printf("invalid\n");
    }
    return(0);
}
 
Old 04-13-2023, 02:24 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.

Last edited by sundialsvcs; 04-13-2023 at 02:29 PM.
 
1 members found this post helpful.
Old 04-13-2023, 03:26 PM   #11
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.

Last edited by jmgibson1981; 04-13-2023 at 03:37 PM.
 
Old 04-13-2023, 05:10 PM   #12
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
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.
The alternative is putting all the includes in each C file, but includes in the header is the usual approach. https://go.dev/talks/2012/splash.article#TOC_5.

Code:
#ifndef libjmstringc

#define libjmstringc
FYI, one usually doesn't have #ifndef guards in .c files (because you generally don't #include them anywhere).
 
1 members found this post helpful.
Old 04-13-2023, 05:36 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by jmgibson1981 View Post
I was under the impression I wasn't supposed to put includes in the header file.
It would probably be useful to you and informative to those offering help to understand where that impression came from. If you read that, or perhaps misunderstood something else to have that meaning, it is probably important to understand the context and if necessary adjust your understanding of what was actually intended.

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).

Last edited by astrogeek; 04-13-2023 at 05:58 PM. Reason: tpoys
 
Old 04-13-2023, 06:07 PM   #14
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.

Last edited by jmgibson1981; 04-13-2023 at 06:12 PM.
 
Old 04-13-2023, 06:25 PM   #15
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by jmgibson1981 View Post
I had it backwards I guess.
Not necessarily, it isn't really about "right" and "wrong" ways of doing things, but rather finding out "how" each part affects others and organizing your own code to fit your use case.

Quote:
Originally Posted by jmgibson1981 View Post
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.
Examples in books and online will often not show the includes just to shorten the example code. I would suggest having a look at a few header files on your system for a variety of real world examples.

Quote:
Originally Posted by jmgibson1981 View Post
Just now changed all my includes to the headers and everything is fine. Just including the header file win my source file.
The important thing to take away from this is not that you "fixed" it, but rather that you reorganized your code in a way that was more useful to your case. Take some time to pause and consider the effect of that change and work towards being able to make good choices for the task at hand.
 
  


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
Cannot get past GDISK- Caution: Invalid backup GPT header, but valid main header, regenerating backup header from main header. murde Linux - Newbie 2 05-30-2020 08:23 PM
[SOLVED] what include files and libraries do i need to run gtkglext in codeblocks terrorofdeath Linux - Newbie 12 05-13-2013 03:08 PM
[SOLVED] QWebView, forward declaration, "error: forward declaration of 'struct QWebView'" Squall90 Programming 1 12-28-2010 05:40 AM
[SOLVED] Codeblocks plugins and header files location konzo Programming 3 01-29-2010 11:26 PM

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

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