12

I am trying to compile a C program for my CS class. I have Command Line tools installed on my Mac, so I probably have OpenGL. The program description was made for Ubuntu and it says for me to compile using:

gcc -Wall -ansi -pedantic -O2 main.o graphic.o imagem.o io.o -o ep2 -lGL -lGLU -lglut

I ran that and it said:

ld: library not found for -lGL

What flags should I use? What do I do?

Thi G.
  • 1,578
  • 5
  • 16
  • 27

2 Answers2

24

In MacOS X you're not using libraries to include system level APIs, but Frameworks. The proper command line to compile this program would be

gcc -Wall -ansi -pedantic -O2 \
    main.o graphic.o imagem.o io.o \
    -o ep2 \
    -framework OpenGL -lGLU -lglut

Note that GLU is probably part of the OpenGL framework as well. And it may be required to install GLUT first.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • 4
    @ThiG.: You don't use `-lGL` on MacOS X. You use the OpenGL framework there. Oh, and in the source code you probably need to replace `#include ` with `#include `. Apple strongly suffers from *not invented here* syndrom. – datenwolf Aug 31 '13 at 18:28
  • +1 - I'd just add that there is also a GLUT framework available, if the OP is unfortunate enough to need it. I don't know if it plays well with core profile. – Brett Hale Aug 31 '13 at 19:13
  • @datenwolf Thks for the reply. Although I tried the way you said and it returned `ld: library not found for -lGLU`. Any idea? – Thi G. Aug 31 '13 at 19:54
  • 1
    @ThiG. GLU is part of the OpenGL framework on OS X. `#include ` in your source code and `-framework OpenGL` in your Makefile are all you need to use it. Likewise, there is a GLUT framework; use `#include ` and `-framework GLUT` on OS X. Make sure to use something like `#ifdef __APPLE__` when determining how to `#include` the headers, the rest of the world tends to follow the convention: `` :) – Andon M. Coleman Aug 31 '13 at 23:12
  • @AndonM.Coleman Thks man!! It worked perfect, no errors this time. XD – Thi G. Aug 31 '13 at 23:18
  • Why does it compile without any warning with `-lGL`? This is nonsense! Lost one hour on this. It works with `-framework OpenGL`. – Bilow Jan 05 '18 at 18:47
  • 1
    @Bilow: Because macOS also uses to ship with a X11/GLX server and `libGL.so` (linked via `-lGL`) is the interface library required for X11/GLX execution path. So on a typical macOS system both libraries can be found, but only one of them (the one in the OpenGL framework) works through the native GUI system. – datenwolf Jan 05 '18 at 20:25
7

I found you have to use

-framework OpenGL -framework GLUT

Reference

Machavity
  • 30,841
  • 27
  • 92
  • 100
Mike Lee
  • 2,440
  • 26
  • 12