0

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 
DevSolar
  • 67,862
  • 21
  • 134
  • 209
Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
  • Please, show what is your link line/options. – Diego Sevilla Apr 15 '14 at 08:40
  • I beg your pardon? Diego, what you mean with link line/options? – Jose Ramon Apr 15 '14 at 08:41
  • Compilation implies the compilation phase and the linking phase. The first one produces object modules (.o), and the final phase links all these with system libraries to produce an executable. I would like to see how you generated the final executable. You may be missing some linker option to include the POSIX DLL. – Diego Sevilla Apr 15 '14 at 08:43
  • possible duplicate of [Microsoft Visual Studio: opendir() and readdir(), how?](http://stackoverflow.com/questions/883594/microsoft-visual-studio-opendir-and-readdir-how) – Mats Petersson Apr 15 '14 at 08:44
  • You are trying to use Unix/Linux functionality that doesn't exist (directly, in that form) in Windows. See duplicate question. – Mats Petersson Apr 15 '14 at 08:45
  • What about this API http://www.two-sdg.demon.co.uk/curbralan/code/dirent/dirent.html – Jose Ramon Apr 15 '14 at 08:48

2 Answers2

1

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

See this MSDN article for an example.

jam
  • 3,640
  • 5
  • 34
  • 50
1

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.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • I have add dirent.h to include paths in project properties. – Jose Ramon Apr 15 '14 at 09:11
  • @FereRes: Adding the *path* to the *include path* means that MSVC can *find* the header **if you `#include `**. If you don't include it in your source, adding the path to the project properties won't do anything. (That's the equivalent of the `-I` option for GCC.) Can it be that you are in waters much too deep for your current grasp of the language and its toolchain? ;-) – DevSolar Apr 15 '14 at 09:23
  • It's not that case, I ve already tried to include dirent.h but I am getting the same errors. – Jose Ramon Apr 15 '14 at 09:32
  • @FereRes: Are you sure you are including the *right* `dirent.h`? Check your assumptions. Put an `#error This is the right one!` in the header and see if this makes the compilation abort. If it does, step back and follow the "SSCCE" link in my answer, because otherwise we'll be playing ping-pong all day. ;) – DevSolar Apr 15 '14 at 09:42