编译包含 std::chrono 的 cpp 时出错 Error:static constexpr unsignedfractional_width = {_S_fractional_width()};

rec*_*all 7 c++ std

我在编译以下使用该库的 C++ 代码时遇到问题<chrono>

\n
#include "log.hpp"\n#include <chrono>\n#include <iomanip>\n#include <iostream>\n#include <utility>\n\n#ifdef _WIN32\n#include <windows.h>\n#else\n#define RESET "\\033[0m"\n#define COLOR_BEGIN(code) "\\033[38;2;" << ((code >> 16) & 0xFF) << ";" << ((code >> 8) & 0xFF) << ";" << (code & 0xFF) << "m"\n#endif\n\nnamespace Monolith\n{\n    namespace utils\n    {\n        void log::print_log(std::string &&message)\n        {\n            if (!m_enabled) return;\n            auto now = std::chrono::system_clock::now();\n            std::time_t time_point = std::chrono::system_clock::to_time_t(now);\n            std::tm *timeinfo = std::localtime(&time_point);\n            auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() % 1000;\n\n#ifdef _WIN32\n            HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);\n            SetConsoleTextAttribute(console, static_cast<WORD>(m_color));\n#else\n            std::cout << COLOR_BEGIN(m_color);\n#endif\n            std::cout << "[" << m_entity << "] {" << std::put_time(timeinfo, "%T") << ":" << std::setfill('0') << std::setw(3)\n                      << milliseconds << "} " << message;\n\n#ifdef _WIN32\n            SetConsoleTextAttribute(console, static_cast<WORD>(0xFFFFFF));\n#else\n            std::cout << RESET << std::endl;\n#endif\n        }\n\n        std::string format_args(const char *message, va_list args)\n        {\n            const size_t length = 1024;\n            char *buffer = new char[length];\n            vsnprintf(buffer, length, message, args);\n\n            std::string result(buffer);\n\n            delete[] buffer;\n\n            return result;\n        }\n\n        log::log(std::string entity, unsigned int color) : m_entity(entity), m_color(color)\n        {\n        }\n\n        void log::out(const char *message, ...)\n        {\n            va_list args;\n            va_start(args, message);\n            print_log(format_args(message, args));\n            va_end(args);\n        }\n\n    } // namespace utils\n} // namespace Monolith\n
Run Code Online (Sandbox Code Playgroud)\n

这是日志.hpp

\n
#pragma once\n#ifndef _LOG_HPP_\n#define _LOG_HPP_\n\n#include <iostream>\n#include <stdarg.h>\n\nnamespace Monolith\n{\n    namespace utils\n    {\n        class log\n        {\n          public:\n            log(std::string entity, unsigned int color);\n\n            void out(const char *message, ...);\n\n          private:\n            void print_log(std::string &&message);\n\n            bool m_enabled = true;\n            std::string m_entity;\n            unsigned int m_color;\n        };\n\n    } // namespace utils\n} // namespace Monolith\n\n#endif // !_LOG_HPP_\n
Run Code Online (Sandbox Code Playgroud)\n

完整的错误列表

\n
\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80(recall\xe3\x89\xbfrecall)-[/home/dev/monolith_engine]\n\xe2\x94\x94\xe2\x94\x80$ ./cmake_build.sh debug\n-- The C compiler identification is Clang 14.0.6\n-- The CXX compiler identification is Clang 14.0.6\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Check for working C compiler: /usr/bin/cc - skipped\n-- Detecting C compile features\n-- Detecting C compile features - done\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - done\n-- Check for working CXX compiler: /usr/bin/c++ - skipped\n-- Detecting CXX compile features\n-- Detecting CXX compile features - done\n-- Found Vulkan: /usr/lib/x86_64-linux-gnu/libvulkan.so (found version "1.3.250")  missing components: glslc glslangValidator\n-- Configuring done (0.6s)\n-- Generating done (0.0s)\n-- Build files have been written to: /home/dev/monolith_engine/build\n[ 20%] Building CXX object CMakeFiles/monolith.dir/src/utils/log.cpp.o\n[ 40%] Building CXX object CMakeFiles/monolith.dir/src/main.cpp.o\n[ 60%] Building CXX object CMakeFiles/monolith.dir/src/utils/timer.cpp.o\n[ 80%] Building CXX object CMakeFiles/monolith.dir/src/mh_vulkan/renderer/mh_vk_renderer.cpp.o\nIn file included from /home/dev/monolith_engine/src/utils/log.cpp:2:\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: error: call to consteval function 'std::chrono::hh_mm_ss::_S_fractional_width' is not a constant expression\n        static constexpr unsigned fractional_width = {_S_fractional_width()};\n                                                      ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: note: undefined function '_S_fractional_width' cannot be used in a constant expression\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2275:2: note: declared here\n        _S_fractional_width()\n        ^\nIn file included from /home/dev/monolith_engine/src/utils/timer.cpp:1:\nIn file included from /home/dev/monolith_engine/src/utils/timer.hpp:5:\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: error: call to consteval function 'std::chrono::hh_mm_ss::_S_fractional_width' is not a constant expression\n        static constexpr unsigned fractional_width = {_S_fractional_width()};\n                                                      ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: note: undefined function '_S_fractional_width' cannot be used in a constant expression\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2275:2: note: declared here\n        _S_fractional_width()\n        ^\n1 error generated.\nmake[2]: *** [CMakeFiles/monolith.dir/build.make:104: CMakeFiles/monolith.dir/src/utils/log.cpp.o] Error 1\nmake[2]: *** Waiting for unfinished jobs....\n1 error generated.\nmake[2]: *** [CMakeFiles/monolith.dir/build.make:118: CMakeFiles/monolith.dir/src/utils/timer.cpp.o] Error 1\nIn file included from /home/dev/monolith_engine/src/mh_vulkan/renderer/mh_vk_renderer.cpp:1:\nIn file included from /home/dev/monolith_engine/src/mh_vulkan/renderer/mh_vk_renderer.hpp:6:\nIn file included from /home/dev/monolith_engine/src/mh_vulkan/renderer/../../core.hpp:31:\nIn file included from /home/dev/monolith_engine/src/mh_vulkan/renderer/../../utils/benchmark_timer.hpp:8:\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: error: call to consteval function 'std::chrono::hh_mm_ss::_S_fractional_width' is not a constant expression\n        static constexpr unsigned fractional_width = {_S_fractional_width()};\n                                                      ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: note: undefined function '_S_fractional_width' cannot be used in a constant expression\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2275:2: note: declared here\n        _S_fractional_width()\n        ^\nIn file included from /home/dev/monolith_engine/src/main.cpp:1:\nIn file included from /home/dev/monolith_engine/src/core.hpp:31:\nIn file included from /home/dev/monolith_engine/src/utils/benchmark_timer.hpp:8:\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: error: call to consteval function 'std::chrono::hh_mm_ss::_S_fractional_width' is not a constant expression\n        static constexpr unsigned fractional_width = {_S_fractional_width()};\n                                                      ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: note: undefined function '_S_fractional_width' cannot be used in a constant expression\n/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2275:2: note: declared here\n        _S_fractional_width()\n        ^\n1 error generated.\nmake[2]: *** [CMakeFiles/monolith.dir/build.make:90: CMakeFiles/monolith.dir/src/mh_vulkan/renderer/mh_vk_renderer.cpp.o] Error 1\n1 error generated.\nmake[2]: *** [CMakeFiles/monolith.dir/build.make:76: CMakeFiles/monolith.dir/src/main.cpp.o] Error 1\nmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/monolith.dir/all] Error 2\nmake: *** [Makefile:91: all] Error 2\n\n
Run Code Online (Sandbox Code Playgroud)\n

该项目是使用 CMake 以clang( clang++) 作为编译器构建的。但是,我在库中遇到错误<chrono>

\n
/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/chrono:2320:48: error: call to consteval function 'std::chrono::hh_mm_ss::_S_fractional_width' is not a constant expression\n        static constexpr unsigned fractional_width = {_S_fractional_width()};\n                                                      ^\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试过使用 GNU 编译器和 Clang 编译器,甚至在 and 之后删除了 GCC 和 G apt update++ apt upgrade。但是,错误仍然存​​在。

\n

任何人都可以提供有关为什么会发生此错误以及如何解决该错误的见解吗?我感谢任何帮助或指导。

\n

rec*_*all 4

更新CMakeList.txt以使用-stdlib=libc++-std=c++20修复它

这是CMakeList.txt

cmake_minimum_required(VERSION 3.21)

set(PROJECT_NAME monolith)

project(${PROJECT_NAME})

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -std=c++20")

set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/debug)
else()
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/release)
endif()

file(GLOB_RECURSE SOURCE_FILES
    ${SOURCE_DIR}/*.cpp
)

file(GLOB_RECURSE HEADER_FILES
    ${INCLUDE_DIR}/*.hpp
)

find_package(Vulkan REQUIRED)
find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)

add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES})

target_link_libraries(${PROJECT_NAME} PRIVATE Vulkan::Vulkan glfw glm::glm)
Run Code Online (Sandbox Code Playgroud)