Thread: How can I develop c++ library in header file without having multiple definition issue

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gil900
    Should I include ipp file inside header file and then in main.cpp and the ipp file I should include the header?
    That would result in circular inclusion.

    Quote Originally Posted by gil900
    I meant to include in main.cpp the ipp file and in ipp file include the header
    Yes, you could do that, but I'm not sure if you can actually separate an inline function's declaration from its definition: you might have to define them in the header.

    You could also look at this blog post on Boost's approach, but note it has more to do with templates than inline.
    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

  2. #17
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    I sent some message here to say thank you and the system deleted it.. hope you still can see it because I wrote there somthing important

    Thanks for the help

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm afraid I cannot see such a message.
    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. #19
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    I said that what i liked in this forum that you can ask any questions or maybe write very strange ideas without get blocked due to ranking system. This is good thing to ask questions and write ideas without any feer. It is not the case in SOF

    So thank you

  5. #20
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    Hi, I thought about it again.

    I think according to my design experience that it is wrong to do this:


    ModuleA.ipp -- includes --> ModuleA.h
    ModuleB.ipp -- includes --> ModuleB.h

    main.cpp:
    Code:
    #include "MouduleA.ipp"
    #include "MouduleB.ipp"
    The reason is that any code from ModuleA cant use access code from ModuleB because the declarations for ModuleB are not yet defined.

    And this is not for Object Oriented approach.
    I even dont agree at all with Object Oriented approach. It is an issue for non OO approach.

    The idea I suggest is:

    ModuleA.h
    ModuleB.h

    ModuleA.ipp
    ModuleB.ipp

    main.cpp:
    Code:
    #include "MouduleA.h"
    #include "MouduleB.h"
    
    #include "MouduleA.ipp"
    #include "MouduleB.ipp"

    This is the way I used to write code in a Tool I sell, written in Autoit.

    In Autoit I did exactly the same but with "Globals" naminng convention:

    #include "MouduleA.Globals.au3"
    #include "MouduleB.Globals.au3"

    #include "MouduleA.au3"
    #include "MouduleB.au3"


    Is it known approach in C++ world?

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gil900
    The reason is that any code from ModuleA cant use access code from ModuleB because the declarations for ModuleB are not yet defined.
    You only need a declaration of a function to call it, or a declaration of a class to create a pointer or reference to it, hence ModuleA can just include the header. Now, if you need to create an object of a class, then you actually need the class definition, but then class definitions usually go in headers too, unless you're using the pimpl idiom.

    As for your idea of including both .h and .ipp files in a .cpp file: it'll typically be unnecessary because you would want to include the corresponding .h file in a .ipp file* to help catch mismatches in declarations and definitions. However, because of inclusion guards including the .h file twice indirectly doesn't matter, and could be good for consistency (i.e., include the header everywhere you use the components declared in it; include the implementation file when you need it).

    * or vice versa, but as I noted you can't do both due to circular inclusion. If you do include the .ipp in the header, the .ipp file becomes "hidden" so it's merely an organisational technique to separate the implementation physically, but at the cost of always having the full definitions included by including the header. Also, if you have circular references, then you wouldn't be able to do this because you would end up with circular inclusion, i.e., you would have to not include the .ipp file in the header so that each .ipp file can include the other header.
    Last edited by laserlight; 04-14-2019 at 03:06 PM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-10-2017, 06:27 AM
  2. inline function in header, multiple definition
    By cyberfish in forum C++ Programming
    Replies: 5
    Last Post: 06-14-2009, 01:21 AM
  3. using definition from header file without including it
    By amitbern in forum C Programming
    Replies: 7
    Last Post: 08-02-2007, 04:48 AM

Tags for this Thread