错误lnk2001:closedir,readdir,opendir

时间:2014-04-15 08:37:36

标签: c++ unix dirent.h

我正在尝试将一个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;
}

是什么想法是关闭了readdir和opendir在windows中的对应函数?

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 

2 个答案:

答案 0 :(得分:1)

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

有关示例,请参阅this MSDN article

答案 1 :(得分:1)

您明确链接到包含Unix API中Windows功能的dirent.h实现。 (称之为&#34; Windows版本&#34;有点遗漏这一点。)

应该发生的事情是你包含那个标题,当你调用 readdir()等时,该调用被转换(通过代码您的dirent.h进入Windows API调用,如FindNextFileW()等。 - 实际上没有涉及Unix函数。

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

混合&amp;混合了Windows和POSIX API。一个肯定的灾难食谱。

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