简体   繁体   中英

Cross compiling error on Visual Studio

Here is a snapshot of a function that I am porting to my Windows machine from here using Visual Studio.

bool MinidumpFileWriter::Open(const char *path) {
  assert(file_ == -1);
#if __linux__
  file_ = sys_open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
#else
  file_ = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
#endif

  return file_ != -1;
}

Currently, this works fine on my Linux machine. Now when I try to port it to my Windows machine like this:

bool MinidumpFileWriter::Open(const char *path) {
  assert(file_ == -1);
#if __linux__
  file_ = sys_open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  return file_ != -1;
#elif _Win32
  HANDLE hFile;
  hFile = CreateFile(path,               // file to open
                     GENERIC_READ,          // open for reading
                     FILE_SHARE_READ,       // share for reading
                     NULL,                  // default security
                     OPEN_EXISTING,         // existing file only
                     FILE_ATTRIBUTE_NORMAL, // normal file
                     NULL);     
  if (hFile == INVALID_HANDLE_VALUE){ 
      return false; 
  }
  else{
      return true;
  }
#else
  file_ = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  return file_ != -1;
#endif  
}

The open function in the '#else' macro gives me an error that it is not recognized. From my understanding of Operating System macros, Visual Studio should not worry about what is inside the directives, and compile the Windows part of code only. But it isn't happening like that. Why is it?

I think you have a "case" problem with your macro in Windows, it should be _WIN32 or WINNT, check here .

You might also want to check these:

http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx

they can be handy when porting code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM