tags:

views:

76

answers:

5

Guys, I have a Visual C++ solution with 2 projects AlgorithmA & AlgorithmB and both share a common header file RunAlgo.h with the class declaration. Each project in the solution has its own unique implementation for the header file.

I am trying to compile a DLL out of the common header file RunAlgo.h and add reference to this DLL in the projects AlgorithmA & AlgorithmB. I have then included separate RunAlgo.cpp definition file in both my projects. The problem is that I am getting linker errors while compiling the new DLL project which has only the header file.

So, the question is

  1. Can a header file with only class declaration be compiled into a DLL (Similar to class library containing an Interface in C#)?
  2. For the above scenario, is there a better approach to reuse the common Header file among projects?
  3. Should the above method work (re-check my code?)
A: 

To 1.:

No, free-standing header files never end up in a dll. Header files are included in implementation files and that's how they are compiled. Header files are usually distributed along with a dll or library if you want to allow third parties to link against it.

To 2.:

Why don't you declare an abstract base class with the interface for the algorithm and provide two different implementations by defining two subclasses (AlgorithmA and AlgorithmB) deriving from the base class? I don't get why you want to different DLLs.

To 3.:

No, it shouldn't. See point 1.

Johannes Rudolph
For the problem that I am working on, we have multiple algorithms to solve it. To maintain consistency, I want to create a standard Interface and distribute it as a DLL to the algorithm developers so that they can inherit it and create their own implementations.Now for an Interface DLL, I tried to create a DLL project with only headers files with the abstract base classes declarations. I left the definitions empty as I wanted the algorithm developers to define them. Now the problem is that I am unable to compile the Interface DLL project with only abstract base class declarations.
Amrish
A: 

I think that to make the header file stand-alone (an interface), you'll need to make all the methods pure virtual.

Eric
+1  A: 

Can a header file with only class declaration be compiled into a DLL

No, headers typically include only declarations. Declarations when compiled don't produce any machine code, so resulting DLL would be empty.

For the above scenario, is there a better approach to reuse the common Header file among projects?

Reusing header is fine. In fact, every library has it's set of headers that you need to include in projects using that library.

I don't know much Visual C++, but I think you could make third project, containing common parts (i.e. RunAlgo.h header), and mark it as a dependency for AlgorithmA and AlgorithmB projects.

el.pescado
+1  A: 

1 & 3: No, that doesn't make sense in C++.

Libraries (dynamic or otherwise) are only used during linking. During compilation declarations must be visible to the compiler in source-code form. This is why, for example, you have to explicitly #include standard library headers in addition to linking against the standard library.

2: What you're already doing is basically the only solution. Put the common header files in their own directory, and add that directory to the include path of each of the two projects.

To give an overview, I am working on defining the base class for an optimization problem. There are multiple implementations being developed, I want to maintain consistensy and I thought of creating a DLL with the interface declaration and pass it to the various development teams to inherit from. If I am right, the only solution in C++ is to pass over the BaseClass.H file to every team.
Amrish
Unfortunately, yes.
A: 

Use 2 namespaces in C++ to write 2 different implementation with the same header file

namespace ImplementationA
{
}
namespace ImplementationB
{
}

When you want to use 1st implementation

using implementationA;

or

implementationA::function1();
Betamoo