0

我正在尝试让 spdlog 用于我的嵌入式项目。(是的,我知道 spdlog 不是为嵌入式项目设计的,但我想试一试;))

我已经设法将 c++ std::thread、std::mutex 等转发到适当的 FreeRTOS 对象。所以这不再是问题了。

实际代码非常简单。数据应写入日志文件。实际的文件写入应通过嵌入式 FATFS 库完成

#include <spdlog/logger.h>

void foo()
{
  // create a file rotating logger with 5mb size max and 3 rotated files
  auto file_logger = spdlog::rotating_logger_mt("file_logger", "myfilename", 1024 * 1024 * 5, 3);
}

但是,这会导致编译器错误

../User/ThirdParty/spdlog/details/os-inl.h: In function 'size_t spdlog::details::os::filesize(FILE*)':
../User/ThirdParty/spdlog/details/os-inl.h:231:16: error: '::fileno' has not been declared
  231 |     int fd = ::fileno(f);
      |                ^~~~~~
../User/ThirdParty/spdlog/details/os-inl.h: In function 'bool spdlog::details::os::in_terminal(FILE*)':
../User/ThirdParty/spdlog/details/os-inl.h:432:21: error: 'fileno' was not declared in this scope; did you mean 'file'?
  432 |     return ::isatty(fileno(file)) != 0;
      |                     ^~~~~~
      |                     file

现在我已经有了系统调用的包装器

int _open(char *path, int flags, ...)
{
    /* Pretend like we always fail */
    return -1;
}

int _close(int file)
{
    return -1;
}


__attribute__((weak)) int _read(int file, char *ptr, int len)
{
    
return -1;
}

__attribute__((weak)) int _write(int file, char *ptr, int len)
{
    return -1;
}

为什么我仍然收到错误?我必须添加吗

--wrap=_read
--wrap=_write

等到链接器标志?

你能帮帮我吗?

4

1 回答 1

0

这是编译器错误而不是链接器错误。我添加了一个声明

int fileno(FILE *stream)
{
    errno = ENOENT;
    return -1;
}

在 spdlog 包含之前。这解决了这个问题

不幸的是 spdlog 现在不支持裸机平台:O

第 231 行:

// OpenBSD doesn't compile with :: before the fileno(..)

#if defined(__OpenBSD__)
    int fd = fileno(f);
#else
    int fd = ::fileno(f);
#endif
于 2021-05-20T21:31:16.077 回答