Hello,

I have a question. I'm not sure I am using the right terms or anything here, but, I have seen people develop their "projects" (classes etc..) and then creating a .dll file (or .lib) and then importing the files when they need them. But I cannot seem to get my head around it.. So for example:

class Test {

    public: 

        Test();
        Test(int theVar)
        {
            var = theVar;
            cout << "You entered: " << this->var;

        }

    private:

        int var;
};

If I had the class above and wanted to export it as a .dll and then import like this:

#include "myClass.dll/.lib"

int main()
{
   // object initialise 

}

How would I do this? Or, could anyone suggest tutorials which may help me?

BTW: I'm using GCC on a mac. (I think it's different in you can't create/import .dll's instead it's .lib).

Any suggestions would be greatly appriciated.

Here is a pretty complete tutorial for Linux. For Mac, it is a Unix-like system like Linux, and so, the tutorial is equally valid for Mac.

(I think it's different in you can't create/import .dll's instead it's .lib).

You have wrong, here is how it is:

Windows:
.dll : for "Dynamic-Link Library", the DLL is needed to run the executable (i.e., the library code is in the DLL only, not imported into the executable, and that code is loaded dynamically (when loading the executable)).
.lib : for static library (only used by Microsoft and Borland compilers), the lib is only needed when compiling the executable (i.e., the library code is imported into the final executable, making it a standalone executable).

Unix-like systems (Unix, QNX, Linux, Mac, Windows/Cygwin, and just about every other OS that exists today):
lib***.so : for "Shared Object", this is essentially the same as a Windows DLL (although the linking mechanisms are a bit different, but that's an advanced topic).
lib***.a : for "Archive" or also called a static library, this is the same as a .lib, but for all compilers other than Microsoft or Borland (i.e., GCC / Clang / ICC / ... under Windows also use .a for static libraries). The only difference between a .lib and a .a is the binary format used (the .a files use the ELF (POSIX) standard from Unix).

Thanks for this, really helped me and I can now finally start building my library.

Quick question though -

I understand the differences between (.lib and .dll) BUT how difficult would it be to have a .lib and a .dll file so I can work with both Linux/Unix and Windows? So in theory, if my library was called "HelloWorld" I could have: "HelloWorld.lib" -- linux and "HelloWorld.dll" - Windows

Thanks :)

how difficult would it be to have a .lib and a .dll file so I can work with both Linux/Unix and Windows?

You'd have two builds for the two systems. Whether the actual code that gets built needs to be ported to support the different systems depends on what you're doing. That also dictates how hard it is, but it could be as simple as just rebuilding with a different target (if the code is 100% standard C++, which is portable) to anywhere as hard as rewriting the whole thing (if you use a lot of platform dependent code).

This might sound like a stupid question, but, do you need to include all the .h files? For example, if I'm working on this example:

Person.h

class Person {

  public:

    // public

 protected:

   // protected

}; 
Teacher.h
class Teacher : public Person {

  public:

    // public

  protected:

  // protected
}; 

and in main.cpp I just #include "Teacher.h" but where my project is, I still need to have.. Person.h and Teacher.h?

I thought it would be possible to have a generic class: Application.h for example, where I could include all the methods that I needed, this doesn't seem to work.

Sorry for basic question!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.