Reuse C++ Header files - c++

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
Can a header file with only class declaration be compiled into a DLL (Similar to class library containing an Interface in C#)?
For the above scenario, is there a better approach to reuse the common Header file among projects?
Should the above method work (re-check my code?)

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.

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.

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.

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();

Related

precompiled header clang redefinition of error [duplicate]

I'm writing a library based on OpenVPN3, which is the C++ OpenVPN client implementation in header only, no cpp files. Therefore, I'm having to rely on having only one cpp file, which is the client itself, which includes a header that includes tons of other headers.
The problem is that, because of this, I cannot separate code into multiple cpp files. I'd like to be able for people to use my library, be it precompiled or compiled by them, but they cannot include the same headers on more than 1 cpp file or in the linking process there will be lots of redefinitions. There are also some static variables in the headers, for example.
If someone want to take a look at the number of things added into the 'master' header file: https://github.com/lucaszanella/libopenvpn3/blob/9b3440a736d90b671e9376d2d9e4911475e07112/src/OpenVPNClient.hpp
I know that there are some libraries like Asio that are also header-only and they're used without any problems by everyone.
Some techniques for not redefining a class or a function are to forward declare them but give no definition, but the problem here is that the person who's using my library is going to have to access its methods and everything. Is it possible to separate my methods from the ones used by my library on the headers?
you can put #pragma once on the most top of the file to avoid the same class clash due to being defined twice
If you do not want to duplicate import you can like tadman said:
at the top of the file
#ifndef HEADER
#define HEADER
//code goes here
#endif

How to avoid class/variable redefinition in C++ header only libraries

I'm writing a library based on OpenVPN3, which is the C++ OpenVPN client implementation in header only, no cpp files. Therefore, I'm having to rely on having only one cpp file, which is the client itself, which includes a header that includes tons of other headers.
The problem is that, because of this, I cannot separate code into multiple cpp files. I'd like to be able for people to use my library, be it precompiled or compiled by them, but they cannot include the same headers on more than 1 cpp file or in the linking process there will be lots of redefinitions. There are also some static variables in the headers, for example.
If someone want to take a look at the number of things added into the 'master' header file: https://github.com/lucaszanella/libopenvpn3/blob/9b3440a736d90b671e9376d2d9e4911475e07112/src/OpenVPNClient.hpp
I know that there are some libraries like Asio that are also header-only and they're used without any problems by everyone.
Some techniques for not redefining a class or a function are to forward declare them but give no definition, but the problem here is that the person who's using my library is going to have to access its methods and everything. Is it possible to separate my methods from the ones used by my library on the headers?
you can put #pragma once on the most top of the file to avoid the same class clash due to being defined twice
If you do not want to duplicate import you can like tadman said:
at the top of the file
#ifndef HEADER
#define HEADER
//code goes here
#endif

Multiple definiton of function when including library

I try to include headers from a library in different files of my project and I get multiple definition errors on some functions of the library.
After reading the answer to this question I think the problem is that the functions are implemented directly in the header files of the library.
In particular I want to include the files codecfactory.h and deltautil.h from FastPFor. I don't know if it is relevant for my problem but I include it into my cmake project with this code in my CMakeLists.txt:
include_directories(../../FastPFor/headers)
add_library(FastPFor STATIC ../../FastPFor/src/bitpacking.cpp
../../FastPFor/src/bitpacking.cpp
../../FastPFor/src/bitpackingaligned.cpp
../../FastPFor/src/bitpackingunaligned.cpp
../../FastPFor/src/horizontalbitpacking.cpp
../../FastPFor/src/simdunalignedbitpacking.cpp
../../FastPFor/src/simdbitpacking.cpp
${HEADERS}
)
Everything works fine if I just include the files once. But as soon as I use them in two .cpp files I get these kinds of errors:
CMakeFiles/dbgen.bin.dir/queries/Query5.cpp.o: In function `vsencoding::BitsWriter::BitsWriter(unsigned int*)':
Query5.cpp:(.text+0x8420): multiple definition of `vsencoding::BitsWriter::BitsWriter(unsigned int*)'
CMakeFiles/dbgen.bin.dir/queries/Query13Naive.cpp.o:Query13Naive.cpp:(.text+0x7a50): first defined here
Is there any way I can fix this without having to change the FastPFor code but only my own?
The question you linked to says it all - there is no way to solve this without modifying the headers (or just include them in only one source file).
For instance this line defines a non-inline constructor in a header. Including it in more than one translation unit would result in a violation of the ODR rule.
One way to workaround this would be to change your project to header only style, i.e. moving your implementation to header files. In this way you can keep (more ore less) the structure of your project.
However, this is definitely not a nice solution... The whole project needs to be compiled after every small change to one of the headers...

When using a dll library, is it necessary to include all of the headers used to build the dll?

I have built a shared library (.dll) from a C++ source code.
Now when I want to use this library in another program, do I need to include all of the header files that were used originally for building the library, or including just the headers that belong to used functions in my (new) program suffices? There are many functions in the library that are not used directly in my new program, but, needless to say, all of them are used by the functions in the program.
(So they are indirectly used in the program too, and it would be strange if their inclusion is not necessary.)
Header files are nothing magical, they're just a convenient way of ensuring your .cpp file has access to all the declarations and definitions it needs. So only include those you actually need in each of your files(1).
The above refers to putting #include statements in your files—only do this for header files you actually need to use. Of course, the header files you include can #include other header files themselves.
Like any other well-behaved (i.e., actually usable) DLL, your DLL must supply a set of public interface header files. Under normal circumstances, you must have all these public interface header files available for building a program using that DLL. That doesn't mean you should #include them all; quite on the contrary, you should not. The compiler (preprocessor, actually) will #include what it needs.
Note that when the DLL itself was built, there were probably many more header files involved, but these were private to the implementation and not part of the public interface. You do not need them to build a program using the DLL, and in the general case you don't even have access to them.
So, when designing your own DLL, you must stricly differentiate between public and internal header files. The public header files must be made available to clients, the internal ones need not (unless the client is supposed to build the DLL themselves). Of course, this means a public header file may never #include an internal one.
(1) Note that header file inclusion is purely textual—when reading your source file, the preprocessor effectively copies the contents of the header file and pastes it in place of the #include statement, then goes on parsing. You you did this copy & paste yourself instead, the file would build and run just as well. Of course, it wouldn't pick up later changes in the header file, which is the primary reason why header files are used in the first place.

In the C++ Boost libraries, why is there a ".ipp" extension on some header files

In the C++ Boost libraries, why is there a ".ipp" extension on some header files?
It seems like they are header files included by the ".hpp" file of the same name.
Is this convention common outside of Boost?
What is the justification for having a special file type?
Explanation from one of the template gurus:
If you want to split up your template sources into interface and
implementation (there are lots of good reasons to do that, including
controlling instantiation), you can't very well use the same name
(foo.hpp) twice, and foo.cpp wouldn't be appropriate for either one.
foo.ipp clearly delineates the file as an implementation file intended to
be #included in foo.hpp.
I believe "ipp" stands from "implementation" file. i.e, they hold actually code (for inline functions & templates) rather than just declaration (which are in the header --.H or .HPP -- files)