Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined reference for std::function when not using file_event_handlers in daily_file_sink constructor call #2374

Closed
kslattery opened this issue May 12, 2022 · 3 comments

Comments

@kslattery
Copy link

kslattery commented May 12, 2022

I have the following code to create a daily_file_sink with an overridden calc_filename():

    spdlog::init_thread_pool(4096,1);
    _thread_pool = spdlog::thread_pool();
    std::vector<spdlog::sink_ptr> sinks;
    sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink<std::mutex, custom_file_name_calculator>>(_output_dir, 0, 0));
    auto logger = std::make_shared<spdlog::async_logger>("mydailylogger", begin(sinks), end(sinks), std::move(_thread_pool), spdlog::async_overflow_policy::block);
    spdlog::register_logger(logger);

When this is linked, I get the following errors:

CMakeFiles/my_project.dir/src/RuntimeFileLogger.cpp.o: In function `void __gnu_cxx::new_allocator<spdlog::sinks::daily_file_sink<std::mutex, custom_file_name_calculator> >::construct<spdlog::sinks::daily_file_sink<std::mutex, custom_file_name_calculator>, std::string&, int, int>(spdlog::sinks::daily_file_sink<std::mutex, custom_file_name_calculator>*, std::string&, int&&, int&&)':
/usr/include/c++/5/ext/new_allocator.h:120: undefined reference to `std::function<void (std::string const&, _IO_FILE*)>::function()'
/usr/include/c++/5/ext/new_allocator.h:120: undefined reference to `std::function<void (std::string const&, _IO_FILE*)>::function()'
collect2: error: ld returned 1 exit status

Note that I'm not specifying every parameter for the daily_file_sink constructor, but relying on the default values specified here:

    daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0,
        const file_event_handlers &event_handlers = {})

The problem seems to come from the fact that in places like file_helper::open(), we have code like this:

    if (event_handlers_.before_open)
    {
        event_handlers_.before_open(filename_);
    }

So although the before_open() call would never be made when using the default event_handlers = {}, the linker is still looking for the symbol which simply does not exist.

I worked around this by changing my code to be this:

    spdlog::init_thread_pool(4096,1);
    _thread_pool = spdlog::thread_pool();
    spdlog::file_event_handlers handlers;
    handlers.after_open = [](spdlog::filename_t, std::FILE*) {};
    handlers.before_close = [](spdlog::filename_t, std::FILE*) {};
    std::vector<spdlog::sink_ptr> sinks;
    sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink<std::mutex, custom_file_name_calculator>>(_output_dir, 0, 0, false, 0, handlers));
    auto logger = std::make_shared<spdlog::async_logger>("mydailylogger", begin(sinks), end(sinks), std::move(_thread_pool), spdlog::async_overflow_policy::block);
    spdlog::register_logger(logger);

I have no idea why I was getting the undefined reference errors for after_open() and before_close(), but not for before_open() and after_close(). Something to do with having two parameters instead of one?

One possible fix would be to change the defaulted value for the constructor:

    daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0,
        const file_event_handlers &event_handlers = {[](spdlog::filename_t) {}, [](spdlog::filename_t, std::FILE*) {},[](spdlog::filename_t, std::FILE*) {},[](spdlog::filename_t) {}})
@gabime
Copy link
Owner

gabime commented May 12, 2022

In which compiler version does it happen?

@kslattery
Copy link
Author

$ gcc --version
gcc (Ubuntu 5.5.0-12ubuntu1~14.04) 5.5.0 20171010
Copyright (C) 2015 Free Software Foundation, Inc.
$ ld --version
GNU ld (GNU Binutils for Ubuntu) 2.24

I'm compiling with -std=c++14.

@gabime
Copy link
Owner

gabime commented May 12, 2022

Thanks. If you happen to find a fix for this please share open a pr.

kslattery pushed a commit to kslattery/spdlog that referenced this issue May 13, 2022
gabime added a commit that referenced this issue May 13, 2022
…dlers

Add default file-event_handler callbacks. #2374
@gabime gabime closed this as completed May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants