spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

static function 'int spdlog::details::to12h(const tm &)' declared but not defined

Open godxin999 opened this issue 1 year ago • 5 comments

I create spdlog.cppm to export some functions I want to use. The file is as follows.

module;

#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/details/registry.h>

export module spdlog;

export namespace spdlog {
    using spdlog::logger;
    using spdlog::sink_ptr;
    using spdlog::async_logger;
    using spdlog::async_overflow_policy;
    using spdlog::drop_all;
    using spdlog::register_logger;
    using spdlog::init_thread_pool;
    using spdlog::thread_pool;

    namespace sinks {
        using spdlog::sinks::stdout_color_sink_mt;
        using spdlog::sinks::rotating_file_sink_mt;
    }
    
    namespace level {
        using spdlog::level::level_enum;
    }

    namespace details {
        using spdlog::details::registry;
    }
}

Then I create my log system module but the code below in my definition file log_system.cpp can not be compiled successfully.

module log_system;

import spdlog;
import <memory>;
import <initializer_list>;

LogSystem::LogSystem(){
    spdlog::init_thread_pool(8192,1);

    auto console_sink=std::make_shared<spdlog::sinks::stdout_color_sink_mt>();

    auto file_sink=std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log.txt",1024*1024*5,3);

    const std::initializer_list<spdlog::sink_ptr> sinks{console_sink,file_sink};

    m_logger=std::make_shared<spdlog::async_logger>("logger",sinks.begin(),sinks.end(),spdlog::thread_pool(),spdlog::async_overflow_policy::block);

    spdlog::register_logger(m_logger);
    
    m_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] [thread %t] %v");

    m_logger->set_level(spdlog::level::trace);

    m_logger->info("Logger initialized");
}

LogSystem::~LogSystem(){
    m_logger->flush();
    spdlog::drop_all();   
}

The compiler told me that there is an error in <spdlog/pattern-formatter-inl.h> and the error message told me that static function int spdlog::details::to12h(const tm &) declared but not defined.

Actually I do not know how to solve this problem. Do I need to modify my module file or are there any other solutions?

At last there are some details about my environment.

  • The version of spdlog is 1.13.0
  • The compiler is MSVC\14.37.32822\bin\HostX64\x64\cl.exe

I use xmake to build my project. The xmake.lua file is as follows:

add_rules("mode.debug", "mode.release")
add_languages("c++20")
add_requires("spdlog 1.13.0", {configs = {header_only = true,std_format = true}})

target("myproject")
    set_kind("binary")
    add_headerfiles("src/*.h")
    add_files("src/common/*.cppm")
    add_files("src/*.cpp","src/*.cppm")
    add_packages("spdlog")

godxin999 avatar Feb 09 '24 17:02 godxin999

spdlog::details::to12h() is declared here:

https://github.com/gabime/spdlog/blob/696db97f672e9082e50e50af315d0f4234c82397/include/spdlog/pattern_formatter-inl.h#L141

Since the function body is also defined in the header file, it should not be missing unless the source code has been modified.

tt4g avatar Feb 09 '24 21:02 tt4g

That's why I'm confused. I jumped to the definition position of the function and found that the function has already been defined. This function is only used by I_formatter and r_fomatter. I try to export these two classes but it doesn't have any effect.

godxin999 avatar Feb 10 '24 06:02 godxin999

Can I reproduce the problem without xmake?

tt4g avatar Feb 10 '24 06:02 tt4g

This problem still exists when using cmake.The CMakeLists file is as follows.

cmake_minimum_required(VERSION 3.28)
project(project)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)

add_executable(project)

set(DEPENDS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/depends)
set(spdlog_DIR ${DEPENDS_PATH}/spdlog/lib/cmake/spdlog)

file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cppm ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB common CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/common/*.cppm)

target_sources(project PUBLIC
    ${sources} ${common}
)

target_include_directories(project PUBLIC ${DEPENDS_PATH}/spdlog/include)

find_package(spdlog CONFIG REQUIRED)
target_link_libraries(project PRIVATE spdlog::spdlog_header_only)

godxin999 avatar Feb 10 '24 07:02 godxin999

Hmmm, I am not sure of the cause. But since spdlog is a C++11 library, it may not work well as a C++20 module.

tt4g avatar Feb 10 '24 08:02 tt4g

Any solutions? I've met same problem while using msvc and spdlog with std_format header_only options

Ariaszzzhc avatar Apr 12 '24 14:04 Ariaszzzhc

I've figured out that to12h is marked as static function may cause the issue. Just compile spdlog as lib will resolve this issue.

This maybe a msvc issue.

Ariaszzzhc avatar Apr 12 '24 15:04 Ariaszzzhc