1

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?

Community
  • 1
  • 1
Dezi
  • 172
  • 1
  • 12

2 Answers2

2

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.

onqtam
  • 4,356
  • 2
  • 28
  • 50
  • Does that mean I have to change all the files in the library to make my code work? Or if I wanted to include them in only one source file, how would you do that so I can still use them in the same way? – Dezi Jul 14 '15 at 16:28
  • 1
    @Dezi If you put all the code that uses the problematic headers (only those headers that define symbols as non-inline) in one translation unit you should be able to use the library without modification. – onqtam Jul 14 '15 at 16:34
  • That would make my code really ugly :( Also I would really want to leave the FastPFor code as it is you I can easily get new versions without haven to modify them every time. Isn't there at least a hacky version to do this? – Dezi Jul 14 '15 at 16:37
  • 1
    @Dezi I couldn't find any way. – onqtam Jul 14 '15 at 16:46
  • Ok thanks anyways! I found a way to move it into one file for now and it isn't that ugly. It might even be better code style than before. – Dezi Jul 14 '15 at 17:04
0

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...

havogt
  • 2,572
  • 1
  • 27
  • 37
  • That would be a lot of work. I managed to move the code that needs the include into one file for now so I hope I won't have to do that. – Dezi Jul 14 '15 at 17:07