Search code examples
c++windowscmakeboost-filesystemctime

ctime Error when including boost::filesystem


I am working on small project. The essential part is, that a video should be captured, saved and moved to another folder. For the video part I am using 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. At the moment I am working on windows, and I am using CMake.

The problem is, that in the moment I include the "boost/filesystem" the compiler starts failing. After the use of google and analyzing the error messages I think that there is a problem with headers <ctime> or <time>. The file that is using <ctime> is xmltest that is passed by tinyxml2.

But to be honest I am very much clueless how and why this happens.

I already tried to switch the order of the includes as mentioned here. I tried to exclude the file that uses ctime, but this is not possible. I tried to work around the problem, so I don't have to use Boost, but this is hard due to the platform independency.

The log file:

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

The way I include 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})

The include of Boost in 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);
}

Part in xmltest where <ctime> is used:

#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();

EDIT After some Testing I tried to use <ctime> in diffrent files without the include of boost. In this case I also receive the same problems as mentioned above.


Solution

  • So I fixed the issue, in the ffmpeg-library is file that is called time.h or Time.h. The misstake was that accidentally linked the directory that contained this file.

    I don't know why but with the include of boost::filesystem this was triggered and the time-file of ffmpeg was used.

    I resolved this problem, through removing the linked directory of ffmpeg.