Skip to content

Build issue when compiling with gcc (any modules-capable version) in a C++ modules project #5103

@sebsjames

Description

@sebsjames

Description

This may be a gcc bug or an nlohmann::json bug - I suspect gcc though, because clang handles the code that I'm going to show. Nevertheless, I wanted to flag it here, as I know that modules support is complex and it might be useful to do so (I'll also report it to the gcc bug list).

To reproduce, you can build my minimal project at https://github.com/sebsjames/gcc15_modules_test with g++.

This compiles a couple of C++ modules, in one of which is #included nlohman::json.

The problem file is test1.cppm. This #includes has_tools.h, which imports a module called mplot.tools.

module;

#include <has_tools.h> // not ok if tools is in this #included file and comes before nlohmann
#include <nlohmann/json.hpp>
// #include <has_tools.h> // ok if #included AFTER nlohmann json

export module test1;

// import mplot.tools; // ok if imported here

export namespace test
{
    class tt
    {
    public:
        tt() { this->init(); }
        ~tt() {}
        float get_value() const { return this->value; }
    protected:
        void init() { this->value = 2.0f; }
        float value = 0.0f;
    };
}

I have a workaround, which is to change the order of the included files, but I don't think that should be necessary.

Reproduction steps

Prerequisites: cmake version 3.28.5 or higher, Ninja and g++ 15 or the 16 pre-release from their master branch. Then:

git clone --recursive https://github.com/sebsjames/gcc15_modules_test.git # pulls in nlohmann from develop branch
cd gcc15_modules_test
mkdir bgcc
cd bgcc
CXX=g++-15 cmake .. -GNinja
ninja

Expected vs. actual results

The error output when g++ tries to compile that module is:

[11:46:26 gcc15_modules_test] ./build_g++.sh
In file included from /opt/gcc-15/include/c++/15.2.1/algorithm:63,
                 from json/include/nlohmann/json.hpp:21,
                 from test1.cppm:4:
/opt/gcc-15/include/c++/15.2.1/bits/stl_algo.h:5633:5: error: redefinition of ‘template<class _ForwardIterator, class _Compare> constexpr _ForwardIterator std::__min_element(_ForwardIterator, _ForwardIterator, _Compare)’
 5633 |     __min_element(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~~~
compilation terminated due to -Wfatal-errors.
[11:46:29 gcc15_modules_test]

Expected is to compile a program called main, as clang does:

[13:46:57 gcc15_modules_test] mkdir bclang
[13:47:00 gcc15_modules_test] cd bclang/
[13:47:02 bclang] CXX=clang++-18 cmake .. -GNinja
-- The CXX compiler identification is Clang 18.1.3
-- The C compiler identification is GNU 14.2.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++-18 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/seb/src/gcc15_modules_test/bclang
[13:47:16 bclang] ninja
[8/8] Linking CXX executable main
[13:47:19 bclang] ./main 
instance value: 2
[13:47:21 bclang] 

Minimal code example

See https://github.com/sebsjames/gcc15_modules_test

Error messages

Compiler and operating system

GCC 15.x and master. Ubuntu Linux 24.04

Library version

branch: develop (for modules support)

Validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions