简介
在编译CUDA random forest有关的项目的时候出现了如上错误,从网上下载了这个头文件然后加入到项目里面还是不行。查了下资料,头文件unistd.h是Linux/Unix的系统调用,包含了许多UNIX系统服务函数原型,如open、read、write、_exit、getpid等函数。在linux下能够编译通过的包含此头文件的程序,在VC下编译时出现了如下问题
fatal error C1083: Cannot open include file: ‘unistd.h’: No such file or directory,所以必须把这个头文件加入到VS2010的头文件库里面,其它的编译器也是如此,对于VS2010来说只要在你的默认库文件夹下(我的电脑是C:\Program Files\Microsoft Visual Studio 10.0\VC\include)添加一个unistd.h文件即可,其内容如下:
#ifndef _UNISTD_H
#define _UNISTD_H
#include <io.h>
#include <process.h>
#endif /* _UNISTD_H */
这样 在重新编译包含unistd.h的程序就能通过了
源码例子:
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_NONSTDC_NO_DEPRECATE 1
//屏蔽vs2013的警告错误
#include <sys/types.h>
#include <stdarg.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <io.h>
#include <process.h>
#include <vld.h>
static const int mkey = 211;
static const unsigned char orbuff[32] = {
0x5f,
0x73,
0x79,
0xa3,
0xc2,
0x61,
0x5f,
0x7,
0x61,
0xbd,
0x6e,
0xe9,
0xa0,
0x10,
0x4c,
0x47,
0x41,
0x63,
0x89,
0x13,
0x77,
0x15,
0x8f,
0x46,
0x12,
0xbb,
0xde,
0x34,
0x80,
0x24,
0x61,
0x82
};
#define LOGI printf
static int dodata(const char *srcdata, char *desdata, int strlen, int key)
{
int i;
for (i = 0; i < strlen; i++)
{
desdata[i] = srcdata[i] ^ key;
}
return 0;
}
#define mlen 1024*100
static unsigned long get_file_size(const char *filename)
{
struct stat buf;
if (stat(filename, &buf) < 0)
{
return 0;
}
return (unsigned long)buf.st_size;
}
static int fuck(char *fpath) {
int fp = -1;
fp = open(fpath, O_RDWR);
char buff[mlen] = { '\0' };
unsigned long len = get_file_size(fpath);
memset(buff, '\0', sizeof(buff));
read(fp, buff, len);
lseek(fp, 0-len, SEEK_CUR);
printf("%d\n", len);
dodata(buff, buff, len, mkey);
write(fp, buff, len);
if (fp > 0) {
close(fp);
}
return 0;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("usage: %s input.txt", argv[0]);
return -1;
}
char* path = argv[1];
fuck(path);
return 0;
}