nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.46k stars 6.59k forks source link

Getting a weak-vtables warning with clang on ubuntu 22.04 #4087

Open tanja84dk opened 11 months ago

tanja84dk commented 11 months ago

Description

I noticed that when ever I try to compile with clang ( 11 - 15 as I have installed) on with my normal warning flags on linux then I always ends up with a warning from the exceptions header about weak-vtables, I do know that some would say I have alot of warning flags activated but that is because that is what I saw some time ago that fmtlib uses and chose to use it myself also to catch errors and posible bad practices from my side

I do use CPM-cmake to pull in libraries purely because that is able to store them in a local cache so I don't have to pull the libraries over and over for each project and it does use the github release

Reproduction steps

Tested with clang 11, 12, 13, 14, 15 on ubuntu jammy (22.04)

The shell command is taken from cmake log

/usr/bin/clang++-15  -I/home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include -g -Werror -Wall -Wextra -pedantic -Wconversion -Wundef -Wdeprecated -Wweak-vtables -Wshadow -Wno-gnu-zero-variadic-macro-arguments -Wzero-as-null-pointer-constant -std=gnu++17 -MD -MT CMakeFiles/tanja84dk_testproject.dir/src/main.cpp.o -MF CMakeFiles/tanja84dk_testproject.dir/src/main.cpp.o.d -o CMakeFiles/tanja84dk_testproject.dir/src/main.cpp.o -c /buildtest/Cpp-Tanja84dk-DockerClient/src/main.cpp

I'm using CPM-cmake to pull in libraries and in essence its just pulling the 3.11.2 release from the release tab by tag

cmake_minimum_required(VERSION 3.16)
project(tanja84dk_testproject LANGUAGES CXX VERSION 0.1.0
)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(cmake/CPM.cmake)

include(CheckCXXCompilerFlag)

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  set(PEDANTIC_COMPILE_FLAGS -pedantic-errors -Wall -Wextra -pedantic
      -Wold-style-cast -Wundef
      -Wredundant-decls -Wwrite-strings -Wpointer-arith
      -Wcast-qual -Wformat=2 -Wmissing-include-dirs
      -Wcast-align
      -Wctor-dtor-privacy -Wdisabled-optimization
      -Winvalid-pch -Woverloaded-virtual
      -Wconversion -Wundef
      -Wno-ctor-dtor-privacy -Wno-format-nonliteral)
  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
      set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
         -Wno-dangling-else -Wno-unused-local-typedefs)
  endif ()
  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
      set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wdouble-promotion
          -Wtrampolines -Wzero-as-null-pointer-constant -Wuseless-cast
          -Wvector-operation-performance -Wsized-deallocation -Wshadow)
  endif ()
  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
      set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wshift-overflow=2
          -Wnull-dereference -Wduplicated-cond)
  endif ()
  set(WERROR_FLAG -Werror)
endif ()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion -Wundef
    -Wdeprecated -Wweak-vtables -Wshadow
    -Wno-gnu-zero-variadic-macro-arguments)
  check_cxx_compiler_flag(-Wzero-as-null-pointer-constant HAS_NULLPTR_WARNING)
  if (HAS_NULLPTR_WARNING)
    set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wzero-as-null-pointer-constant)
  endif ()
  set(WERROR_FLAG -Werror)
endif ()

if (MSVC)
  set(PEDANTIC_COMPILE_FLAGS /W4)
  set(WERROR_FLAG /WX)
endif ()

set(tanja84dk_testproject_SOURCES
    src/main.cpp
)

set(tanja84dk_testproject_LIBRARIES
    nlohmann_json
)

CPMAddPackage(
    NAME            nlohmann_json
    GIT_REPOSITORY  https://github.com/nlohmann/json
    VERSION         3.11.2
    GIT_TAG         v3.11.2
)

add_executable(tanja84dk_testproject ${tanja84dk_testproject_SOURCES})

set_target_properties(tanja84dk_testproject
    PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
        LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
        CXX_STANDARD 17
    FOLDER
        Application
)

target_link_libraries(tanja84dk_testproject PRIVATE ${tanja84dk_testproject_LIBRARIES})

target_compile_options(tanja84dk_testproject
    PRIVATE
        ${WERROR_FLAG}
        ${PEDANTIC_COMPILE_FLAGS})

target_compile_features(tanja84dk_testproject
    PRIVATE
        cxx_std_17
)

### Expected vs. actual results

Expected it to build successfully specially because I didn't do anything specially in the small test but it warned/errored with the weak-vtables in the include/nlohmann/detail/exceptions.hpp

### Minimal code example

```Shell
#include <iostream>
#include <nlohmann/json.hpp>

int main() { return 0; }

Error messages

[main] Building folder: Cpp-Tanja84dk-DockerClient 
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /buildtest/Cpp-Tanja84dk-DockerClient/build --config Debug --target all --
[build] [1/2  50% :: 2.195] Building CXX object CMakeFiles/tanja84dk_testproject.dir/src/main.cpp.o
[build] FAILED: CMakeFiles/tanja84dk_testproject.dir/src/main.cpp.o 
[build] /usr/bin/clang++-15  -I/home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include -g -Werror -Wall -Wextra -pedantic -Wconversion -Wundef -Wdeprecated -Wweak-vtables -Wshadow -Wno-gnu-zero-variadic-macro-arguments -Wzero-as-null-pointer-constant -std=gnu++17 -MD -MT CMakeFiles/tanja84dk_testproject.dir/src/main.cpp.o -MF CMakeFiles/tanja84dk_testproject.dir/src/main.cpp.o.d -o CMakeFiles/tanja84dk_testproject.dir/src/main.cpp.o -c /buildtest/Cpp-Tanja84dk-DockerClient/src/main.cpp
[build] In file included from /buildtest/Cpp-Tanja84dk-DockerClient/src/main.cpp:2:
[build] In file included from /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/json.hpp:35:
[build] In file included from /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/adl_serializer.hpp:14:
[build] In file included from /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/detail/conversions/from_json.hpp:23:
[build] /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/detail/exceptions.hpp:36:7: error: 'exception' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
[build] class exception : public std::exception
[build]       ^
[build] /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/detail/exceptions.hpp:134:7: error: 'parse_error' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
[build] class parse_error : public exception
[build]       ^
[build] /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/detail/exceptions.hpp:187:7: error: 'invalid_iterator' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
[build] class invalid_iterator : public exception
[build]       ^
[build] /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/detail/exceptions.hpp:205:7: error: 'type_error' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
[build] class type_error : public exception
[build]       ^
[build] /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/detail/exceptions.hpp:222:7: error: 'out_of_range' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
[build] class out_of_range : public exception
[build]       ^
[build] /home/buildclient/.cache/CPM/nlohmann_json/b3708972f6694fe462e4112e47aa04f10d2390b4/nlohmann_json/include/nlohmann/detail/exceptions.hpp:239:7: error: 'other_error' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
[build] class other_error : public exception
[build]       ^
[build] 6 errors generated.
[build] ninja: build stopped: subcommand failed.
[proc] The command: /usr/bin/cmake --build /buildtest/Cpp-Tanja84dk-DockerClient/build --config Debug --target all -- exited with code: 1
[driver] Build completed: 00:00:02.244
[build] Build finished with exit code 1

Compiler and operating system

Clang 11, 12, 13, 14, 15 on ubuntu 22.04

Library version

3.11.2

Validation