airdcpp-web / airdcpp-webclient

Communal peer-to-peer file sharing application for file servers/NAS devices
https://airdcpp-web.github.io
171 stars 31 forks source link

Build fails with "error: type ‘dcpp::File::BufferMode’ violates the C++ One Definition Rule" #425

Closed sbraz closed 1 year ago

sbraz commented 1 year ago

Hi, As initially reported here: https://bugs.gentoo.org/861839, the build fails with certain strict compiler flags:

$ rm CMakeCache.txt; export CXXFLAGS="-flto -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing"; cmake .. && make -j$(nproc)
[…]
[ 57%] Linking CXX shared library libairdcpp.so
/tmp/airdcpp-webclient/airdcpp-core/airdcpp/File.h:87: error: type ‘dcpp::File::BufferMode’ violates the C++ One Definition Rule [-Werror=odr]
   87 |         enum BufferMode {
      | 
/tmp/airdcpp-webclient/airdcpp-core/airdcpp/File.h:87: note: an enum with different value name is defined in another translation unit
   87 |         enum BufferMode {
      | 
/tmp/airdcpp-webclient/airdcpp-core/airdcpp/File.h:95: note: name ‘BUFFER_SEQUENTIAL’ is defined to 0 while another translation unit defines it as 2
   95 |                 BUFFER_SEQUENTIAL,
      | 
/tmp/airdcpp-webclient/airdcpp-core/airdcpp/File.h:89: note: mismatching definition
   89 |                 BUFFER_SEQUENTIAL = POSIX_FADV_SEQUENTIAL,
      | 
lto1: some warnings being treated as errors
lto-wrapper: fatal error: /usr/bin/c++ returned 1 exit status
compilation terminated.
/usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
make[2]: *** [airdcpp-core/CMakeFiles/airdcpp.dir/build.make:1857: airdcpp-core/libairdcpp.so.2.11.2] Error 1
make[1]: *** [CMakeFiles/Makefile2:168: airdcpp-core/CMakeFiles/airdcpp.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

There might be further errors later too.

maksis commented 1 year ago

I can reproduce the error with those flags but I'm a bit confused because it refers to code that is inside #ifdef ... #else so lines 95 and 89 can't both be defined at the same time.

https://github.com/airdcpp-web/airdcpp-webclient/blob/b311dfcf8091db6d41d0b351cc6a44b00e5e084f/airdcpp-core/airdcpp/File.h#L87-L100

Jannik2099 commented 1 year ago

HAVE_POSIX_FADVISE is not defined for all TUs that include this header (quick grep shows it's only defined for the compilation of File.cpp), which leads to different definitions of the enum across the program.

Most of ODR applies to all definitions within the whole program, not just all definitions within one TU.

maksis commented 1 year ago

Thanks, this will be fixed in the next version

https://github.com/airdcpp-web/airdcpp-webclient/commit/5f12f803c2fc0450111152e2ea5f649ed92f4b7f