首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OS小巧OpenGL/X11C代码编译错误

OS小巧OpenGL/X11C代码编译错误
EN

Stack Overflow用户
提问于 2013-11-08 12:34:12
回答 2查看 1.1K关注 0票数 0

我在编译c代码时遇到了问题,c代码在更新之前就已经开始工作了。

代码语言:javascript
复制
#include <GL/gl.h>
#include <GL/glu.h>

#include <GL/glx.h>

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xutil.h>
#include <X11/Xlib.h>
#include <GL/glx.h>

char WINDOW_NAME[] = "Graphics Window";
char ICON_NAME[] = "Icon";
Display *display;
int screen;
Window main_window;
unsigned long foreground, background;

static int snglBuf[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 12, None};
static int dblBuf[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 12,GLX_DOUBLEBUFFER, None};

Bool doubleBuffer = True;
XVisualInfo *vi;
GLXContext cx;
Colormap cmap;
XSetWindowAttributes swa;
Bool recalcModelView = True;
int dummy;

void connectX()
{
   display = XOpenDisplay(NULL);
   if (display == NULL) {fprintf(stderr, "Cannot connect to X\n");
                         exit(1);}
   screen = DefaultScreen(display);
   if (!glXQueryExtension(display, &dummy, &dummy)){
         fprintf(stderr, "X server has no OpenGL GLX Extension\n");
         exit(1);}
   vi = glXChooseVisual(display, screen, dblBuf);
   if (vi == NULL){
      fprintf(stderr, "Double Buffering not available\n");
      vi = glXChooseVisual(display, screen, snglBuf);
      if (vi == NULL) fprintf(stderr, "No RGB Visual with depth buffer\n");
      doubleBuffer = False;
   }
  if (doubleBuffer == True) fprintf(stderr, "We have double buffering\n");
  if (vi->class != TrueColor){
            fprintf(stderr, "Need a TrueColor visual\n");
            exit(1);
          }
  cx = glXCreateContext(display, vi , None, True);
  if (cx == NULL){
       fprintf(stderr, "No rendering context\n");
       exit(1);
      }
   else printf(stderr, "We have a rendering context\n");

  cmap = XCreateColormap(display, RootWindow(display, vi->screen),
                         vi->visual, AllocNone);
  if (cmap == NULL){
      fprintf(stderr, "Color map is not available\n");
      exit(1);
     }
  swa.colormap = cmap;
  swa.border_pixel = 0;
  swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask |
                   KeyPressMask;
}

Window openWindow(int x, int y, int width, int height,
                  int border_width, int argc, char** argv)
{
    XSizeHints size_hints;
    Window new_window;
    new_window = XCreateWindow(display, RootWindow(display, vi->screen),
                 x,y, width, height, border_width, vi->depth, InputOutput,
                 vi->visual, CWBorderPixel | CWColormap | CWEventMask,
                 &swa);
   size_hints.x = x;
   size_hints.y = y;
   size_hints.width = width;
   size_hints.height = height;
   size_hints.flags = PPosition | PSize;
   XSetStandardProperties(display, new_window, WINDOW_NAME, ICON_NAME,
                          None, argv, argc, &size_hints);
   XSelectInput(display, new_window, (ButtonPressMask | KeyPressMask |
                                       StructureNotifyMask | ExposureMask));
   return (new_window);
}


void init(void)
{
  const GLubyte *renderer = glGetString(GL_RENDERER);
  const GLubyte *vendor = glGetString(GL_VENDOR);
  const GLubyte *version = glGetString(GL_VERSION);
  const GLubyte *glslversion = glGetString(GL_SHADING_LANGUAGE_VERSION);
  GLint major, minor;

  //glGetIntegerv(GL_MAJOR_VERSION, &major);
  //glGetIntegerv(GL_MINOR_VERSION, &minor);
  printf("GL_VENDOR           : %s\n", vendor);
  printf("GL_RENDERER         : %s\n", renderer);
  printf("GL_VERSION (string) : %s\n", version);
  //printf("GL_VERSION (int)    : %d.%d\n", major, minor);
  printf("GLSL Version        : %s\n", glslversion);

}

void disconnectX()
{
   XCloseDisplay(display);
   exit(0);
}

void doKeyPressEvent(XKeyEvent *pEvent)
{
 int key_buffer_size = 10;
 char key_buffer[9];
 XComposeStatus compose_status;
 KeySym key_sym;
 XLookupString(pEvent, key_buffer, key_buffer_size,
               &key_sym, &compose_status);
 switch(key_buffer[0]){
    case 'q':
      exit(0);
      break;
   }
}


void redraw(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glShadeModel(GL_SMOOTH);
  glBegin(GL_POLYGON);
    glColor3f(1.0,0.0,0.0);
    glVertex2f(-0.9,-0.9);
    glColor3f(0.0,1.0,0.0);
    glVertex2f(0.9,-0.9);
    glColor3f(0.0,0.0,1.0);
    glVertex2f(0.0,0.9);
  glEnd();

  if (doubleBuffer) glXSwapBuffers(display,main_window); else
   glFlush();
   fprintf(stderr, "redraw called\n");
}


void doButtonPressEvent(XButtonEvent *pEvent)
{
   fprintf(stderr, "Mouse button pressed\n");
}

int main (int argc, char** argv){
  XEvent event;
  connectX();
  main_window = openWindow(10,20,500,400,5, argc, argv);
  glXMakeCurrent(display, main_window, cx);
  XMapWindow(display, main_window);

  glClear(GL_COLOR_BUFFER_BIT);
  init();

 /*glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glFrustum(-1.0,1.0, -1.0, 1.0,0.0,10.0);*/
while(1){
    do{
    XNextEvent(display, &event);
    switch (event.type){
      case KeyPress:
        doKeyPressEvent(&event);
        break;
      case ConfigureNotify:
        glViewport(0,0, event.xconfigure.width, event.xconfigure.height);
      case Expose:
          redraw();
          break;
       case ButtonPress:
          doButtonPressEvent(&event);
          break;
 }
}
   while (True); //XPending(display));
  }
}

我知道这个错误:

x86_64 clang:错误:链接器命令失败,退出代码1(使用-v查看调用)

我已经安装了XQuartz,并且正在使用-I/opt/X11/include-L/opt/X11/lib。不使用OpenGL的X11程序工作得很好。

EN

回答 2

Stack Overflow用户

发布于 2013-11-13 23:43:39

@Giorgi Shavgulidze,我认为我的问题类似。我的代码和山狮一起编译得很好。我升级为小牛。我用小牛和Xcode 5.0.2重新编译代码,glEnable(GL_SMOOTH)导致了一个错误。我刚说了这句话,一切都很好。为什么我不知道..。

票数 0
EN

Stack Overflow用户

发布于 2017-07-18 16:37:24

我知道这对你来说可能有点晚了,但是我相信这可能会对其他人有所帮助。

我最近基本上也遇到了同样的问题。重要的是要注意到在通过gcc这样的方法编译c代码时,包含和链接到c代码之间的区别,就像上面提到的那样。关于这一点的一个很好的帖子可以找到here

postbin显示您没有正确地链接到OpenGL和X11。要链接到他们,您需要添加链接到您的gcc命令。我的理解是,您可以通过另一条特定于计算机的路径(通过-L )链接到它们,也可以在gcc命令中使用预先创建的链接,这似乎是一个简单得多的选择。

关于X11,虽然您确实通过-L/opt/X11/lib链接到保存X11库的目录,但是您没有指定要使用哪个库。我相信X11需要的额外链接是-lX11

关于OpenGL,您将使用不同的标志。虽然我没有使用OpenGL (只使用GL),但据我理解,这是您必须使用的适当标志:-lGLU -lglut

同样重要的是要注意,在链接和库总是最后的时候,顺序很重要。

作为一个例子,我已经把gcc的命令包括在内了,尽管它与你所需要的略有不同。

代码语言:javascript
复制
gcc -I/opt/X11/include -L/opt/X11/lib -lX11 -lncurses -lGL spitmat8w_3.c -o hello
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19859225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档