ctime包含boost :: filesystem时发生错误

时间:2019-07-10 09:34:16

标签: c++ windows cmake boost-filesystem ctime

我正在做一个小项目。最重要的部分是,应该捕获,保存视频并将其移动到另一个文件夹。 对于我正在使用的视频部分,FFmpeg可以正常工作。为了安全起见,我正在使用tinyXML2。为了复制和移动视频文件,我喜欢使用boost。 目前,我正在Windows上工作,并且正在使用CMake。

问题是,在我包括“ boost / filesystem”的那一刻,编译器开始失败。使用google并分析错误消息后,我认为标头<ctime><time>有问题。正在使用<ctime>的文件是xmltest传递的tinyxml2

但是,老实说,我对这种情况的发生方式和原因一无所知。

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

日志文件:

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

我包含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})

Util.h中包含Boost的内容:

#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。在这种情况下,我还会收到与上述相同的问题。

1 个答案:

答案 0 :(得分:0)

所以我解决了这个问题,在import os import wx import time from threading import Thread from urllib.request import FancyURLopener from urllib.request import urlopen import wx.lib.newevent urlretrieve = FancyURLopener().retrieve progress_event, EVT_PROGRESS_EVENT = wx.lib.newevent.NewEvent() class DownloadThread(Thread): def __init__(self, parent, url, fsize): """Constructor""" Thread.__init__(self) self.fsize = fsize self.url = url self.parent = parent self.start() def run(self): local_fname = os.path.basename(self.url) urlretrieve(self.url, local_fname, self.reporthook) def reporthook(self,blocknum, blocksize, totalsize): readsofar = blocknum * blocksize if totalsize > 0: percent = int((readsofar / totalsize) * 100) else: # total size is unknown percent = 0 evt = progress_event(count=percent) #Send back current count for the progress bar try: wx.PostEvent(self.parent, evt) except: # The parent frame has probably been destroyed pass class MyPanel(wx.Panel): def __init__(self, parent): """Constructor""" wx.Panel.__init__(self, parent) self.lbl = wx.StaticText(self, label="Download URL:") self.dl_txt = wx.TextCtrl(self) btn = wx.Button(self, label="Download") self.gauge = wx.Gauge(self, -1, range=100) self.main_sizer = wx.BoxSizer(wx.VERTICAL) dl_sizer = wx.BoxSizer(wx.HORIZONTAL) dl_sizer.Add(self.dl_txt, 1, wx.EXPAND|wx.ALL, 5) dl_sizer.Add(btn, 0, wx.ALL, 5) self.main_sizer.Add(dl_sizer, 0, wx.EXPAND) self.main_sizer.Add(self.lbl, 0, wx.ALL|wx.CENTER, 5) self.main_sizer.Add(self.gauge, 0, wx.ALL|wx.EXPAND, 5) self.SetSizer(self.main_sizer) #Bind to the progress event issued by the thread self.Bind(EVT_PROGRESS_EVENT, self.OnProgress) btn.Bind(wx.EVT_BUTTON, self.OnDownload) def OnDownload(self, event): url = self.dl_txt.GetValue() self.lbl.SetLabel(os.path.basename(url)) try: header = urlopen(url) fsize = int(header.headers['Content-Length']) # start thread d_thread = DownloadThread(self, url, fsize) while d_thread.isAlive(): time.sleep(0.1) wx.GetApp().Yield() continue # Finished re-set self.dl_txt.SetValue("") self.lbl.SetLabel("Download URL:") self.gauge.SetValue(0) except Exception as e: print ("Error: ", e) def OnProgress(self, event): """""" self.gauge.SetValue(event.count) class DownloaderFrame(wx.Frame): def __init__(self): """Constructor""" wx.Frame.__init__(self, None, title="Downloader", size=(800, 400)) panel = MyPanel(self) self.Show() if __name__ == "__main__": app = wx.App(False) frame = DownloaderFrame() app.MainLoop() 中有一个名为ffmpeg-librarytime.h的文件。 错误是意外地链接了包含此文件的目录。

我不知道为什么,但是在包含Time.h的情况下触发了该事件,并使用了ffmpeg的时间文件。

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