nlohmann / json

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

`#pragma once` not supported with C++20 modules in clang #3974

Closed davidstone closed 11 months ago

davidstone commented 1 year ago

Description

If compiling with modules enabled with clang, #pragma once is used across all headers -- in other words, only the first header with #pragma once in it will be included, and the rest ignored. Headers in nlohmann use #pragma once, and thus the library is difficult or impossible to be used in a modular codebase.

The upstream bug is https://github.com/llvm/llvm-project/issues/38554.

The workaround is to use traditional #ifndef include guards instead.

Reproduction steps

module;

#include <nlohmann/json.hpp>

export module a;

is sufficient to expose the issue if not using the "single header" version. Otherwise, combining it with any other header using #pragma once is sufficient.

Expected vs. actual results

The library should compile, but it errors out with bogus messages.

Minimal code example

No response

Error messages

No response

Compiler and operating system

clang

Library version

b2306145e1789368e6f261680e8dc007e91cc986

Validation

davidstone commented 11 months ago

The upstream bug in clang is fixed.