windows下MSVC兼容的dirent.h

本文介绍了一个在Windows环境下为MSVC编译器实现的<dirent.h>接口,该接口用于目录读取操作。通过下载特定的dirent.h文件并将其包含在MSVC的include路径中,可以解决跨平台项目开发中因Windows平台缺乏此接口而带来的问题。

<dirent.h>是一个应用程序接口,主要用于文件系统的目录读取操作,主要提供了几个目录数据读取函数,参见opengroup.org<dirent.h>

int            closedir(DIR *);//关闭目录
DIR           *opendir(const char *);//打开目录
struct dirent *readdir(DIR *);//读取目录
int            readdir_r(DIR *, struct dirent *, struct dirent **);
void           rewinddir(DIR *);
void           seekdir(DIR *, long int);
long int       telldir(DIR *);

<dirent.h>是个unix系统下常见的接口,但windows平台的MSVC编译器并没有提供这个接口( Cygwin5 and MingW现在已经提供了dirent接口),对于跨平台的项目开发就会带来一些麻烦,如果在MSVC下编译时可能因为windows平台缺少这个接口就要为windows平台另外写一些代码。
开源世界真是个好地方,非常多乐于分享的人为开源世界贡献了很多有用的东西,当然现在贡献多的主要是歪果仁童鞋啦。
在网上一搜索,已经有人在windows下为MSVC实现了dirent.h接口(在这里感谢一下外国的活雷锋们)。于是很多事情y就可以一下子变得简单了。
打开下面的链接,就可以下载到windows平台的dirent.h及相关的测试代码

Dirent API for Microsoft Visual Studio
代码最新的github托管地址是:https://github.com/tronkko/dirent

使用也非常简单把dirent.h加到你的MSVC include目录下就好了。比如:

C:\Program Files\Microsoft Visual Studio 9.0\VC\include

如果你不想对在MSVC的include目录下添加文件,而是想把它放在自己的项目代码中,也可以,但必须以下面的方式include:

#include <dirent.h> 

不能是

#include "dirent.h" 

这样可以确保,当编译器提供了dirent.h的时候(比如mingw),优先使用编译自带的dirent.h

(pyopenjtalk_test_env) d:\graduate\LangChain\pyopenjtalk-0.4.1>pip install pyopenjtalk --no-build-isolation ^ More? --config-settings="--global-option=--cmake-options" ^ More? --config-settings="--global-option=-DCMAKE_GENERATOR_PLATFORM=Win32" Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting pyopenjtalk Using cached https://pypi.tuna.tsinghua.edu.cn/packages/58/74/ccd31c696f047ba381f9b11a504bf1199756c3f30f3de64e3eeb83e10b4a/pyopenjtalk-0.4.1.tar.gz (1.4 MB) Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [94 lines of output] -- Building for: Visual Studio 15 2017 -- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.26100. -- The C compiler identification is MSVC 19.16.27054.0 -- The CXX compiler identification is MSVC 19.16.27054.0 -- Check for working C compiler: D:/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- Check for working C compiler: D:/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: D:/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- Check for working CXX compiler: D:/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Looking for include file ctype.h -- Looking for include file ctype.h - found -- Looking for include file dirent.h -- Looking for include file dirent.h - not found -- Looking for include file fcntl.h -- Looking for include file fcntl.h - found -- Looking for include file inttypes.h -- Looking for include file inttypes.h - found -- Looking for include file io.h -- Looking for include file io.h - found -- Looking for include file memory.h -- Looking for include file memory.h - found -- Looking for include file setjmp.h -- Looking for include file setjmp.h - found -- Looking for include file stdint.h -- Looking for include file stdint.h - found -- Looking for include file stdlib.h -- Looking for include file stdlib.h - found -- Looking for include file strings.h -- Looking for include file strings.h - not found -- Looking for include file string.h -- Looking for include file string.h - found -- Looking for include file sys/mman.h -- Looking for include file sys/mman.h - not found -- Looking for include file sys/stat.h -- Looking for include file sys/stat.h - found -- Looking for include file sys/times.h -- Looking for include file sys/times.h - not found -- Looking for include file sys/types.h -- Looking for include file sys/types.h - found -- Looking for include file unistd.h -- Looking for include file unistd.h - not found -- Looking for include file windows.h -- Looking for include file windows.h - found -- Looking for getenv -- Looking for getenv - found -- Looking for getpagesize -- Looking for getpagesize - not found -- Looking for mmap -- Looking for mmap - not found -- Looking for opendir -- Looking for opendir - not found -- Looking for setjmp -- Looking for setjmp - found -- Looking for sqrt -- Looking for sqrt - found -- Looking for strstr -- Looking for strstr - found -- Looking for iconv_open in iconv -- Looking for iconv_open in iconv - not found -- Looking for sqrt in m -- Looking for sqrt in m - not found -- Looking for stddef.h -- Looking for stddef.h - found -- Check size of char -- Check size of char - done -- Check size of int -- Check size of int - done -- Check size of long -- Check size of long - done -- Check size of long long -- Check size of long long - done -- Check size of short -- Check size of short - done -- Check size of size_t -- Check size of size_t - done -- Configuring done -- Generating done -- Build files have been written to: D:/usertemp/pip-install-zc_jb_dc/pyopenjtalk_4f671c6f3a9b4ed9861070f30afc9001/lib/open_jtalk/src/build CMake is in the system path. Version: cmake version 3.12.18081601-MSVC_2 CMake suite maintained and supported by Kitware (kitware.com/cmake). usage: _in_process.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: _in_process.py --help [cmd1 cmd2 ...] or: _in_process.py --help-commands or: _in_process.py cmd --help error: option --cmake-options not recognized [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
06-03
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值