Question

I am trying to import a c++ project from linux to windows (vs2010). My problem arise with the use of dirent.h. I ve downloaded the windows version of dirent from here: dirent. However when I compile my project I am getting the following errors:

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

A little research and I found that I am using some unix functions. My code is:

#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;
}

Any idea which are the correspondant function for closedir readdir and opendir in 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 
Was it helpful?

Solution

You can use FindFirstFile, FindNextFile, and FindClose (I believe these are in windows.h).

See this MSDN article for an example.

OTHER TIPS

You explicitly link to an implementation of dirent.h that wraps the Windows functions in the Unix API. (Calling that "the Windows version" is a bit missing the point.)

What should be happening is that you include that header, and when you are calling readdir() etc., that call is turned (by the code in your dirent.h) into Windows API calls like FindNextFileW() etc. -- no Unix functions actually being involved.

The point is, I don't see your source example actually including dirent.h... unless you do the include somewhere we cannot see it, that means you installed that wrapping header, but you are not using it. Instead you #include some POSIX headers, which in turn reference the appropriate POSIX functions (which your linker does not find).

Mix & mingle of Windows and POSIX API. A sure recipee for disaster.

You need to adjust your #include's accordingly. As your example isn't SSCCE, it's hard to be more specific.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top