0

我正在尝试将一个 c++ 项目从 linux 导入到 windows (vs2010)。我的问题是使用 dirent.h 引起的。我已经从这里下载了 windows 版本的 dirent:dirent。但是,当我编译我的项目时,我收到以下错误:

1>DBreading.obj : error LNK2001: unresolved external symbol _closedir
1>DBreading.obj : error LNK2001: unresolved external symbol _readdir
1>DBreading.obj : error LNK2001: unresolved external symbol _opendir

稍作研究,我发现我正在使用一些 unix 函数。我的代码是:

#include <DBreading.h>
#include <Detection.h>

#define _POSIX_SOURCE
#include <sys/stat.h>
#include <unistd.h>
#undef _POSIX_SOURCE

DBreading::DBreading(){}

vector <string> DBreading::listFile(string path){

    vector<string> directories;

    DIR *pDIR;
    const char * c = path.c_str();
    struct dirent *entry;
    if( pDIR=opendir(c) ){
        while(entry = readdir(pDIR)){
        if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
            //cout << entry->d_name << "\n";
            directories.push_back( entry->d_name);
        }
        closedir(pDIR);
    }

    // directories stores all subfolders or sub-files of a given path directory
    return directories;
}

知道windows中closedir readdir和opendir的对应函数是什么吗?

1>------ Rebuild All started: Project: myProject, Configuration: Release Win32 ------
1>  DBreading.cpp
1>DBreading.cpp(46): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(52): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(53): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(90): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(97): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(116): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(122): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(123): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(159): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.cpp(166): warning C4018: '<' : signed/unsigned mismatch
1>  Detection.cpp
1>c:\opencv-2.4.6.1\install\include\opencv2\flann\logger.h(66): warning C4996: 'fopen':    
 This function or variable may be unsafe. Consider using fopen_s instead. To disable   
 deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
  1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdio.h(234) :
 see 
declaration of 'fopen'
1>  main.cpp
1>main.cpp(120): warning C4018: '<' : signed/unsigned mismatch
1>main.cpp(166): warning C4018: '<' : signed/unsigned mismatch
1>main.cpp(182): warning C4018: '<' : signed/unsigned mismatch
1>main.cpp(206): warning C4018: '<' : signed/unsigned mismatch
1>main.cpp(268): warning C4018: '<' : signed/unsigned mismatch
1>DBreading.obj : error LNK2001: unresolved external symbol _closedir
1>DBreading.obj : error LNK2001: unresolved external symbol _readdir
1>DBreading.obj : error LNK2001: unresolved external symbol _opendir
1>C:\Documents and Settings\chrathan.ITI-THERMI\My Documents\Visual Studio    
2010\Projects\myProject\Release\myProject.exe : fatal error LNK1120: 3 unresolved 
4

2 回答 2

1

您可以使用FindFirstFileFindNextFileFindClose(我相信这些在 windows.h 中)。

有关示例,请参阅此 MSDN 文章。

于 2014-04-15T08:45:29.390 回答
1

您显式链接到dirent.h在 Unix API 中包装 Windows 函数的实现。(称其为“Windows 版本”有点不合时宜。)

应该发生的是您包含该标头,并且当您调用 readdir()等时,该调用(通过您的代码dirent.h)转换为 Windows API 调用FindNextFileW()等 - 实际上不涉及 Unix 函数。

关键是,我没有看到您的源示例实际上包含 dirent.h......除非您在我们看不到的地方执行包含,这意味着您安装了该包装头,但您没有使用它。取而代之的是#include一些 POSIX 标头,这些标头又引用了适当的POSIX函数(您的链接器没有找到)。

Windows 和 POSIX API 的混合和混合。灾难的可靠配方。

你需要相应地调整你#include的。由于您的示例不是SSCCE,因此很难更具体。

于 2014-04-15T09:02:09.777 回答