Thread: public.h and private.h within a library project

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    public.h and private.h within a library project

    I am creating a lib.libc project in c. This lib.libc and public.h will then be provided to customers. I am running into some compiler errors and wanted to ask a few questions:

    1. I have a #define __TIMERB0 within the public.h . I then #include public.h within private.h. The private.h has #ifdef based on __TIMERB0. However it greys out my conditional in the private.h signaling that it doesn't recognize it. The question: Should I be able to do this?

    2. Assuming I can do #1. At this point then it would suggest that I could just include private.h within my .c files that are used to create the library given that I have the #include public.h within private.h, correct?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    ... however it greys out ...
    I am not sure what you mean by this. Are you looking at it in an IDE?

    But to answer your question, I would would not have either header file include the other, but include both in the source .c files.

    Code:
    #include <public.h> // Exposed API
    #include "private.h" // Internal (hidden) stuff

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ridgerunnersjw
    1. I have a #define __TIMERB0 within the public.h . I then #include public.h within private.h. The private.h has #ifdef based on __TIMERB0. However it greys out my conditional in the private.h signaling that it doesn't recognize it. The question: Should I be able to do this?
    Yes, you should. Why don't you do some debugging to be sure? Like just have two tiny headers and a source file. Conditional on the #ifdef, you define a function that prints "Hello world!". You then include private.h in the source file and attempt to call this function from the main function. If it works, the code can compile and you can run it to get "Hello world!" as the output. If it doesn't work, you'll get a compile error upon which you can come back here and ask about it.

    By the way, __TIMERB0 is a name reserved to the implementation (as in compiler and standard library implementation), so you should avoid defining it for your own use. The two basic rules are:
    • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
    • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.


    Quote Originally Posted by ridgerunnersjw
    2. Assuming I can do #1. At this point then it would suggest that I could just include private.h within my .c files that are used to create the library given that I have the #include public.h within private.h, correct?
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    I am using Code Composer Studio IDE.....not sure that <filename> vs "filename" isn't more the norm than the rule....That is I can put "string.h" OR <string.h> and code works either way....I suspect you have to set something up under compiler options to over-ride the norm which is to not really care how the user includes header files. Point being if you didn't do it right you could easily make "public.h" exposed as well or worse <private.h> gets exposed.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by ridgerunnersjw View Post
    I am using Code Composer Studio IDE.....not sure that <filename> vs "filename" isn't more the norm than the rule....That is I can put "string.h" OR <string.h> and code works either way....I suspect you have to set something up under compiler options to over-ride the norm which is to not really care how the user includes header files. Point being if you didn't do it right you could easily make "public.h" exposed as well or worse <private.h> gets exposed.
    With <> only the include file path is scanned.
    With "" the current directory is first scanned, then the include file path is scanned.

    I would keep the private file in with your source, and the public file somewhere in the include path.

    See Include Syntax (The C Preprocessor)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Private or Public?
    By Tamim Ad Dari in forum C++ Programming
    Replies: 7
    Last Post: 03-19-2013, 05:09 AM
  2. private or public inheritance?
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2009, 12:57 AM
  3. private/public get/set
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-04-2008, 12:49 AM
  4. Private or Public
    By gtriarhos in forum C# Programming
    Replies: 3
    Last Post: 10-10-2005, 07:14 PM
  5. public and private
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2002, 11:02 AM

Tags for this Thread