【发布时间】:2017-03-03 20:20:39
【问题描述】:
我已经完成了一个 C 程序,它获取屏幕像素的 RGB 值 (0-255),知道它的位置 (x,y)。它可以在 Linux 中运行,但是当我尝试在 Visual Studio (Windows) 中编译它时,由于库 X11/Xlib.h、X11/Xutil.h、unistd.h 不存在而崩溃。
这是代码:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
void getimage(XColor c, Display* d, int* rgb, int x, int y) {
XImage* image;
image = XGetImage(d, RootWindow(d, DefaultScreen(d)),
x, y, 1, 1, AllPlanes, XYPixmap);
c.pixel = XGetPixel(image, 0, 0);
XFree(image);
XQueryColor(d, DefaultColormap(d, DefaultScreen(d)), &c);
rgb[0] = c.red / 256;
rgb[1] = c.green / 256;
rgb[2] = c.blue / 256;
}
int main () {
int rgb[3], x, y;
XColor c;
Display* d = XOpenDisplay((char*)NULL);
getimage(c, d, rgb, x, y);
}
如何实现它以在 Windows 中运行?
【问题讨论】:
-
那些是标题,而不是库。头文件是文本源文件,告诉编译器什么是可用的;除非您自己实现了库,否则它们是无用的。这些特定的头文件和库在很大程度上特定于类 Unix 系统。有适用于 Windows 的 Unix 仿真层,例如 Cygwin。或者,您可以使用 Windows 提供的头文件和库编写具有等效功能的代码;结果将是非常不同。还有抽象差异的跨平台库; wxWidgets 就是一个例子。
-
这是编译器错误,不是崩溃。
-
如果您的编译器因缺少标头而崩溃,您应该在调试器中运行它并提交包含所有详细信息的错误报告。
-
感谢您的回答!最后,我安装了cygwin。标题现在可以工作,但我发现了另一个问题: Display *d = XOpenDisplay((char *)NULL);不起作用,因为函数的参数不正确。在 Linux 中,(char *)NULL 是默认显示的环境变量,但在 windows 中不是。你知道在这里写什么吗?
标签: c windows visual-studio xlib unistd.h