odygrd / quill

Asynchronous Low Latency C++ Logging Library
MIT License
1.14k stars 130 forks source link

Necessary singletons are not exposed when compiling shared library with hidden visibility #463

Closed ZijunH closed 1 month ago

ZijunH commented 1 month ago

Consider the following toy example:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)

project(foo)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED true)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

set(CMAKE_BUILD_TYPE RelWithDebInfo)

add_subdirectory(quill)

add_library(logger_lib SHARED logger_lib.cpp)
target_link_libraries(logger_lib PUBLIC
  quill::quill
)

add_executable(logger logger.cpp)
target_link_libraries(logger PUBLIC
  logger_lib
)

logger_lib.cpp

#include "quill/Backend.h"
#include "quill/Frontend.h"
#include "quill/LogMacros.h"
#include "quill/Logger.h"
#include "quill/sinks/ConsoleSink.h"

[[gnu::visibility("default")]] quill::Logger* start_logger() {
    quill::Backend::start();

    auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1");
    return quill::Frontend::create_or_get_logger("root", std::move(console_sink));
}

logger.cpp

#include "quill/Backend.h"
#include "quill/Frontend.h"
#include "quill/LogMacros.h"
#include "quill/Logger.h"
#include "quill/sinks/ConsoleSink.h"

quill::Logger* start_logger();

int main(){
  LOG_INFO(start_logger(), "This is a log info example {}", 123);
}

When compiled, the the logger does not print anything to the terminal. This is because the singletons (the static instance()) functions are not properly exposed in the shared library.

This is a similar issue to #222. Manually exporting the various static instance() functions by [[gnu::visibility("default")]] solves the issue on linux.

odygrd commented 1 month ago

Hey, thanks for reporting. I think I removed those since the library is header only i wasn't expecting it to build as shared library anymore. I will get it fixed over the next few days

odygrd commented 1 month ago

this is fixed with af9eb900d7a9062b9887306b5dddbee9b59ff3d1