John
John

Reputation: 6278

C++ Include a class in multiple projects

I want make one class to include into all my projects so that when I do a change, it updates across all the projects from that copy of the file.

For example:

foo.h

class foo
{
public:
    foo();
};

extern Foo* pFoo;

foo.cpp

Foo* pFoo;


foo::foo()
{
    printf("Foo\n");
}

main.cpp

#include "foo.h"

int main(int argc, char* argv[])
{
    pFoo = new Foo();

    return 0;
}

How can I include "foo.h" into multiple projects cleanly without any hassle?

Upvotes: 2

Views: 923

Answers (2)

Basya
Basya

Reputation: 1485

First of all, you can't just include the header file in multiple projects; the projects ultimately have to link with the implementation as well.

There are many ways to do this; it depends upon how you want to work and what your tools are.

  1. For example, if you are (and you should be) using source code control, some source code control systems will allow you to share a file between projects (svn has external files, StarTeam has shared files....). If you do that, each project will receive a copy of the file(s) when you check it out, and you can build it normally as part of the project.

  2. Another option is to make a library. Once you choose that option, you have to make another choice -- static library (.lib) or dynamic (.dll).

There are advantages and disadvantages to both.

2.a. Static linking seems "simpler" and answers your basic need here -- just to compile this class into your project. However, static linking has limitations. If you build your static library with different options/settings than the project you are linking it into, it may not work. If you have a policy for all your projects (or a certain subset thereof) to use a certain set of options, then this will not be a problem for you.

To create a static library you create a visual studio project of type static library. When it builds, instead of running the linker it will run the librarian.

2.b. DLLs solve (most of) the problems of different compiler options (there are a few things that can affect working with a DLL such as structure packing). Again, you make a project in Visual Studio of type DLL and it will build the DLL for you.

Then, again, you have to make a choice. When you link the DLL into your executable, you have to choose dynamic or static linking. To link statically you include a .lib file in your project -- it should have been created with your DLL. If you link to this .lib, then the program will look for and (try to) load the DLL when it opens.

The most flexible, and most complicated, method is to link dynamically. To do this, in your code you must load the DLL and create pointers to any functions within it that you want to use. This is probably overkill as a solution to the problems you described, and I am not sure how you would access a class (as opposed to a function).

Either way of working with the DLL, you will have to distribute it with your program and make sure it is accessible to the program (in the same file as the executable, in the Windows DLL search path, or, in the case of dynamic loading, you can specify the path specifically and then make sure you install the DLL to that specific location).

If you can manage a static library (2.a), it solves the problem as you asked it, as a static library is basically indirectly including the class in your project -- it is a compiled version of your code ready for linking.

Even if you create a lib or a dll, you will have to work out in your source code control how you will include it in your different projects.

Upvotes: 2

marom
marom

Reputation: 5230

What about a singleton?

class foo
{
    foo()
    {
        printf("foo\n") ;
    }
public:
    static foo &make(void)
    {
        static foo the_instance ;
        return the_istance ;
    }
};

This way you have just one instance (is this what you want) and you are sure about where it's created (that's why constructore is declared private).

Now you have the whole cvlass in a single header file that you can include in your projects without further needs.

Upvotes: -1

Related Questions