0

I have problem with compiling c code which worked earlier before updating o OS X Mavericks.

#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));
  }
}

I get this error:

ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I've already install XQuartz and I'm using -I/opt/X11/include and -L/opt/X11/lib. OpenGL programs which don't use X11 work perfectly.

GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
  • Have you tried using -v to see the invocation? – rhughes Nov 08 '13 at 12:50
  • 1
    *What* symbols are undefined? That is key to the answer. – trojanfoe Nov 08 '13 at 14:31
  • [link] http://pastebin.com/hGF72yMt – Giorgi Shavgulidze Nov 08 '13 at 16:04
  • 1
    All you did was set the compiler search path for system includes and linker search path for system libraries. Since you have not included anything along the lines of `-framework OpenGL` or `-lX11`, I have to assume you forgot to tell the linker which libraries you are actually using. That is, unless you include your ***full*** compile/link command. – Andon M. Coleman Nov 09 '13 at 06:14
  • It compiles with -lX11 and -lGL but uses OpenGL2.1, not OpenGL 4.1. GL_VENDOR : NVIDIA Corporation GL_RENDERER : NVIDIA GeForce GT 650M OpenGL Engine GL_VERSION (string) : 2.1 NVIDIA-8.18.22 310.40.05f01 GLSL Version : 1.20 – Giorgi Shavgulidze Nov 09 '13 at 15:51

2 Answers2

0

@Giorgi Shavgulidze, I think my issue is similar. I had code that compiled fine with Mountain Lion. I upgraded to Mavericks. I go to recompile the code with Mavericks and Xcode 5.0.2, and glEnable(GL_SMOOTH) is causing an error. I just commented the line out and everything is fine. Why I do not know...

xBACP
  • 531
  • 1
  • 3
  • 17
0

I know this is probably a little late for you, however, I believe this may help someone else.

I recently had essentially the exact same problem. It is important to note the difference between including something and linking to it in regards to compiling c code through methods such as gcc, as Andon mentioned above. A good SO post about this can be found here.

Your postbin shows that you are not linking properly to both OpenGL and X11. To link to them, you need to add links to your gcc command. My understanding is that you can either link to them through another path specific to your computer, via -L, or you can use pre-made links in your gcc command, which seems to be the far simpler option.

In regards to X11, while you do link to the directory holding the X11 libraries via -L/opt/X11/lib, you do not specify which library(s) you want to use. I believe the additional link you would need for X11 is -lX11.

In regards to OpenGL, you would use a different flag. While I have not used OpenGL (only used GL), my understanding is these are the appropriate flags you would have to use: -lGLU -lglut.

It is also important to note that order matters when linking and libraries should always go last.

I have included my gcc command, even though it is slightly different than what you would need, as an example.

gcc -I/opt/X11/include -L/opt/X11/lib -lX11 -lncurses -lGL spitmat8w_3.c -o hello