包含 boost::filesystem 时出现 ctime 错误

ctime Error when including boost::filesystem

我正在做一个小项目。重要的部分是,应捕获、保存视频并将其移动到另一个文件夹。 对于视频部分,我使用 FFmpeg this works without any problems. To safe information I am using tinyXML2. For copying and moving of the video files I like to use boost。 目前我正在研究 windows,我正在使用 CMake。

问题是,在我包含 "boost/filesystem" 的那一刻,编译器开始失败。使用 google 并分析错误消息后,我认为 headers <ctime><time> 有问题。使用 <ctime> 的文件是由 tinyxml2.

传递的 xmltest

但老实说,我完全不知道这是怎么发生的,为什么会发生。

我已经尝试过如前所述 here 切换包含的顺序。 我试图排除使用 ctime 的文件,但这是不可能的。 我试图解决这个问题,所以我不必使用 Boost,但由于平台独立性,这很难。

日志文件:

C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(19): error C2039: 'clock_t': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(19): error C2873: 'clock_t': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(23): error C2039: 'asctime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(23): error C2873: 'asctime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(24): error C2039: 'clock': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(24): error C2873: 'clock': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(25): error C2039: 'ctime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(25): error C2873: 'ctime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(26): error C2039: 'difftime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(26): error C2873: 'difftime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(27): error C2039: 'gmtime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(27): error C2873: 'gmtime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(28): error C2039: 'localtime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(28): error C2873: 'localtime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(29): error C2039: 'mktime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(29): error C2873: 'mktime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(30): error C2039: 'strftime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(30): error C2873: 'strftime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(31): error C2039: 'time': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.21.27702\include\ctime(31): error C2873: 'time': symbol cannot be used in a using-declaration

我包含 Boost 和 tinyxml 的方式:

find_package(TinyXML2)
find_package(Boost COMPONENTS filesystem system REQUIRED)

...

add_executable(programm ${project_sources}
    ${project_headers}
    ${TINYXML_INC})

target_link_libraries(programm PUBLIC
    Boost::filesystem
    Boost::system
    ${TinyXML2_LIBRARIES})

target_include_directories(programm PRIVATE
    ${Boost_INCLUDE_DIRS}
    ${Boost_LIBRARY_DIRS}
    ${TinyXML2_INCLUDE_DIR})

target_link_directories(programm PUBLIC
    ${Boost_INCLUDE_DIRS}
    ${Boost_LIBRARY_DIRS}
    ${TinyXML2_INCLUDE_DIR})

Boost 包含在 Util.h 中:

#include <string>
#include <boost/filesystem.hpp>

namespace util {
    bool existens_file(std::string* path);
    int existens_directory(std::string* path);

    bool create_directory(std::string* path);
}

xmltest 中使用 <ctime> 的部分:

#include "tinyxml2.h"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <ctime>

...

XMLDocument* doc = new XMLDocument();
clock_t startTime = clock();
doc->LoadFile( argv[1] );
clock_t loadTime = clock();
int errorID = doc->ErrorID();
delete doc; doc = 0;
clock_t deleteTime = clock();

编辑 经过一些测试后,我尝试在不同的文件中使用 <ctime> 而没有 包含 boost。在这种情况下,我也遇到了与上述相同的问题。

所以我解决了这个问题,在 ffmpeg-library 中是名为 time.hTime.h 的文件。 错误是不小心链接了包含此文件的目录。

我不知道为什么,但是随着 boost::filesystem 的包含,这被触发了,并且使用了 ffmpeg 的 time-file。

我通过删除 ffmpeg 的链接目录解决了这个问题。