Thread: Dynamic Library, Header Files Visibility.

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    22

    Dynamic Library, Header Files Visibility.

    Hello!

    I'm creating a game engine using the SDL library and i stepped into a problem.

    Say i have this definition:

    SMengine.h
    Code:
    #include <SDL.h>
    
    namespace SMengine
    {
        class Init
        {
            private:
                SDL_Window *w;
    
            public:
                __declspec(dllexport) Init();
                __declspec(dllexport) ~Init();
        };
    }
    And after compiling the above code to a dll library i want the end user to be able to use it without seeing the #include <SDL.h>
    definitions.

    End user's code:
    Code:
    #include <SMengine.h>
    
    int main()
    {
        //do what you have to do.
    }
    SMengine.h includes SDL.h so the end user also includes SDL.h .

    The end user needs SDL.h in order to compile his code but he doesn't need to know what's in there.

    He should only be able to see the SMengine class, the Constructor and the Deconstructor.

    So is there a way to hide the definitions of SDL.h from the end user?

    Thank you!
    Last edited by babaliaris; 01-15-2018 at 12:33 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,684
    I am not a C++ programmer; but, I think you can hide includes inside of namespaces.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    22
    Quote Originally Posted by stahta01 View Post
    I am not a C++ programmer; but, I think you can hide includes inside of namespaces.

    Tim S.
    Well i tried it but i get a bunch of weird errors when i compile it.

    SMengine.h
    Code:
    #pragma once
    
    
    
    
    namespace myspace
    {
    #include <iostream>
    #include <SDL.h>
    }
    
    
    class SMengine
    {
    
    
    public:
        __declspec(dllexport) SMengine();
        __declspec(dllexport) ~SMengine();
    
    
    };
    SMengine.cpp
    Code:
    #include "SMengine.h"
    
    
    
    
    
    
    SMengine::SMengine()
    {
        if (myspace::SDL_Init(SDL_INIT_EVERYTHING) < 0)
            myspace::std::cout << "Something went wrong" << myspace::std::endl;
    
    
        else
            myspace::std::cout << "Its time to roll!!!" << myspace::std::endl;
    
    
        myspace::std::cin.get();
    }
    
    
    
    
    SMengine::~SMengine()
    {
    }

    Errors In Visual Studio:
    Code:
    1>------ Build started: Project: SMengine, Configuration: Debug Win32 ------
    1>SMengine.cpp
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(20): error C2039: 'fabs': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(25): error C2039: 'fabsf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(30): error C2039: 'fabsl': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(34): error C2039: 'div_t': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(34): error C2873: 'div_t': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(34): error C2039: 'ldiv_t': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(34): error C2873: 'ldiv_t': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(35): error C2039: 'abort': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(35): error C2873: 'abort': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(35): error C2039: 'abs': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(35): error C2873: 'abs': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(36): error C2039: 'atof': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(36): error C2873: 'atof': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(36): error C2039: 'atoi': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(36): error C2873: 'atoi': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(36): error C2039: 'atol': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(36): error C2873: 'atol': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(37): error C2039: 'bsearch': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(37): error C2873: 'bsearch': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(37): error C2039: 'calloc': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(37): error C2873: 'calloc': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(37): error C2039: 'div': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(37): error C2873: 'div': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(38): error C2039: 'exit': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(38): error C2873: 'exit': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(38): error C2039: 'free': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(38): error C2873: 'free': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(39): error C2039: 'labs': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(39): error C2873: 'labs': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(39): error C2039: 'ldiv': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(39): error C2873: 'ldiv': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(39): error C2039: 'malloc': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(39): error C2873: 'malloc': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(40): error C2039: 'mblen': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(40): error C2873: 'mblen': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(40): error C2039: 'mbstowcs': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(40): error C2873: 'mbstowcs': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(40): error C2039: 'mbtowc': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(40): error C2873: 'mbtowc': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(41): error C2039: 'qsort': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(41): error C2873: 'qsort': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(41): error C2039: 'rand': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(41): error C2873: 'rand': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(41): error C2039: 'realloc': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(41): error C2873: 'realloc': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(42): error C2039: 'srand': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(42): error C2873: 'srand': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(42): error C2039: 'strtod': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(42): error C2873: 'strtod': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(42): error C2039: 'strtol': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(42): error C2873: 'strtol': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(43): error C2039: 'strtoul': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(43): error C2873: 'strtoul': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(44): error C2039: 'wcstombs': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(44): error C2873: 'wcstombs': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(44): error C2039: 'wctomb': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(44): error C2873: 'wctomb': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(46): error C2039: 'lldiv_t': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(46): error C2873: 'lldiv_t': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(49): error C2039: 'getenv': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(49): error C2873: 'getenv': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(50): error C2039: 'system': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(50): error C2873: 'system': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(53): error C2039: 'atoll': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(53): error C2873: 'atoll': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(53): error C2039: 'llabs': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(53): error C2873: 'llabs': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(53): error C2039: 'lldiv': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(53): error C2873: 'lldiv': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(54): error C2039: 'strtof': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(54): error C2873: 'strtof': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(54): error C2039: 'strtold': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(54): error C2873: 'strtold': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(55): error C2039: 'strtoll': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(55): error C2873: 'strtoll': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(55): error C2039: 'strtoull': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(55): error C2873: 'strtoull': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(57): error C2039: '_Exit': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(57): error C2873: '_Exit': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(57): error C2039: 'at_quick_exit': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(57): error C2873: 'at_quick_exit': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(57): error C2039: 'quick_exit': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cstdlib(57): error C2873: 'quick_exit': symbol cannot be used in a using-declaration
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(20): error C2039: 'pow': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(25): error C2039: 'acosf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(30): error C2039: 'acoshf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(35): error C2039: 'asinf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(40): error C2039: 'asinhf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(45): error C2039: 'atanf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(50): error C2039: 'atanhf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(55): error C2039: 'atan2f': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(60): error C2039: 'cbrtf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(65): error C2039: 'ceilf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(71): error C2039: 'copysignf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(76): error C2039: 'cosf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(81): error C2039: 'coshf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(86): error C2039: 'erff': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(91): error C2039: 'erfcf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(96): error C2039: 'expf': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(101): error C2039: 'exp2f': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(106): error C2039: 'expm1f': is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\cmath(106): fatal error C1003: error count exceeds 100; stopping compilation
    1>Done building project "SMengine.vcxproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    This is so weird. These symbols at the errors are from the math.h library, how does this make sense
    Last edited by babaliaris; 01-15-2018 at 01:11 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,684
    Why did you try to hide iostream header?
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    22
    Quote Originally Posted by stahta01 View Post
    Why did you try to hide iostream header?
    No reason actually, but it seems to be the problem

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    Quote Originally Posted by babaliaris
    So is there a way to hide the definitions of SDL.h from the end user?
    Yes, by using the pimpl (pointer to implementation) idiom. That is, instead of declaring the SDL_Window member in the SMengine::Init class, you will do so in another class that exists purely to implement the SMengine::Init class. The SMengine::Init class will then have a pointer to an object of this implementation class through which it will implement all its member functions. Hence, the <SDL.h> header will only be included in your source file that contains the definition of the implementation class and its implementation. The user of SMengine::Init include a header that has no trace of SDL, although it will have a forward declaration of the implementation class as well as the definition of the SMengine::Init class.

    You should not try to "hide includes inside of namespaces": it doesn't hide anything.
    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

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    22
    Quote Originally Posted by laserlight View Post
    Yes, by using the pimpl (pointer to implementation) idiom. That is, instead of declaring the SDL_Window member in the SMengine::Init class, you will do so in another class that exists purely to implement the SMengine::Init class. The SMengine::Init class will then have a pointer to an object of this implementation class through which it will implement all its member functions. Hence, the <SDL.h> header will only be included in your source file that contains the definition of the implementation class and its implementation. The user of SMengine::Init include a header that has no trace of SDL, although it will have a forward declaration of the implementation class as well as the definition of the SMengine::Init class.

    You should not try to "hide includes inside of namespaces": it doesn't hide anything.
    Thank you, I'm on it right now!

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    22
    I think i got it.

    SMengine.h
    Code:
    #pragma once
    
    
    class SMengine
    {
    
    
    private:
        class Impl;
        Impl *impl;
    
    
    public:
        __declspec(dllexport) SMengine();
        __declspec(dllexport) ~SMengine();
    
    
    };

    SMengine.cpp
    Code:
    #include "SMengine.h"
    #include <SDL.h>
    #include <iostream>
    
    
    
    
    
    
    class SMengine::Impl
    {
    public:
        SDL_Window *window;
    };
    
    
    
    
    
    
    SMengine::SMengine() : impl( new Impl() )
    {
    
    
        //Initialize window.
        impl->window = NULL;
    
    
        if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
            std::cout << "Something went wrong :(" << std::endl;
    
    
        else
            std::cout << "Everything is fine!!!" << std::endl;
    
    
    
    
        std::cout << std::endl;
        std::cin.get();
    }
    
    
    
    
    SMengine::~SMengine()
    {
        delete impl;
    }

    Client.cpp
    Code:
    #include "SMengine.h"
    
    
    int main(int argc, char *argv[])
    {
        SMengine e;
    
    
        return 0;
    }
    Also i noticed this:

    Before i do this, the client had to link with the SDL Library and SMengine in order to compile his code but now he doesn't need to do that. Once the SMengine library is compiled (and linked with SDL) he just have to compile his code only by linking with the SMengine. I don't really understand that. Is it because when the client was including SDL from SMengine he had to link with SDL because he was including source code from SDL?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    3,684
    Quote Originally Posted by babaliaris View Post
    I think i got it.

    Also i noticed this:

    Before i do this, the client had to link with the SDL Library and SMengine in order to compile his code but now he doesn't need to do that. Once the SMengine library is compiled (and linked with SDL) he just have to compile his code only by linking with the SMengine. I don't really understand that. Is it because when the client was including SDL from SMengine he had to link with SDL because he was including source code from SDL?
    The SDL library breaks a few rules; including requiring the main function signature to be just right. IIRC, it replaces the main function in some use cases.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    Quote Originally Posted by babaliaris
    Before i do this, the client had to link with the SDL Library and SMengine in order to compile his code but now he doesn't need to do that. Once the SMengine library is compiled (and linked with SDL) he just have to compile his code only by linking with the SMengine. I don't really understand that. Is it because when the client was including SDL from SMengine he had to link with SDL because he was including source code from SDL?
    Yes, the pimpl idiom is intended for these scenarios where you want to have the flexibility of changing the implementation without requiring users of your library (in this case SDL, even though it isn't yours per se) to recompile, with the tradeoff of some overhead (e.g., the compiler cannot inline those "extra" function calls that are made to call the implementation functions).
    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. .h header files and library interface
    By tindala in forum C Programming
    Replies: 8
    Last Post: 05-12-2012, 05:14 AM
  2. Replies: 2
    Last Post: 08-28-2011, 10:05 PM
  3. Replies: 1
    Last Post: 06-08-2011, 11:06 PM
  4. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  5. Header and Library Files
    By jrahhali in forum C++ Programming
    Replies: 9
    Last Post: 08-23-2004, 03:51 PM

Tags for this Thread