mac下安裝配置freeglut,設定xcode

矛盾論發表於2015-01-15

主要參考文章:

http://lazyfoo.net/tutorials/OpenGL/01_hello_opengl/mac/index.php

FreeGLUT doesn't have any prebuild frameworks we can use with the OS X compilers. Fortunately, Mac OS X is a type of Unix and we can use unix commands to get it to build and install freeGLUT on OS X. Unfortunatey, OS X has some quirks we need to deal with.

1) The first issue is that newer versions of OS X don't have X11. You'll have to install XQuartz, a quartz based port of X.org. Download the dmg and install it.

2) Download the latest source from the freeGLUT website.

Extract the archive, open a terminal window, cd to the extracted folder.

3) Configure the installation using this command in the extracted folder
env CPPFLAGS="-I/opt/X11/include" LDFLAGS="-L/opt/X11/lib" ./configure
When you installed XQuartz, it installs X11 to /opt/X11/lib. This command configures the build and tells it to look in /opt/X11/include for headers and /opt/X11/lib for library files.

4) In freeGLUT 2.8.0 there's a bug in the source code that causes to break in OS X. In the freeGLUT source directory you extracted go to progs -> demos -> smooth_opengl3 and open up smooth_openg13.c.

Between lines 101 and 102 there's a bunch type definitions that are already in glext.h that cause a conflict.
typedef void (APIENTRY *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);typedef void (APIENTRY *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);typedef void (APIENTRY *PFNGLBUFFERDATAPROC) (GLenum target, ourGLsizeiptr size, const GLvoid *data, GLenum usage);typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);typedef void (APIENTRY *PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);typedef void (APIENTRY *PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
All you have to do is sandwich it in some ifdef macros to make it stop complaining.
#ifndef __glext_h_typedef void (APIENTRY *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);typedef void (APIENTRY *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);typedef void (APIENTRY *PFNGLBUFFERDATAPROC) (GLenum target, ourGLsizeiptr size, const GLvoid *data, GLenum usage);typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);typedef void (APIENTRY *PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);typedef void (APIENTRY *PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);#endif
5) Then in the terminal enter the make command (make sure the terminal is in the directory you extracted)
make all
Now using root privileges, install the library with the command:
sudo make install
6) Now that you've installed the development libraries, it's time to start up your IDE/compiler.
make all 出現錯誤:

smooth_opengl3.c:122:1: error:unknown type name 'PFNGLGENBUFFERSPROC'

PFNGLGENBUFFERSPROC gl_GenBuffers;

^

smooth_opengl3.c:123:1: error:unknown type name 'PFNGLBINDBUFFERPROC'

PFNGLBINDBUFFERPROC gl_BindBuffer;

^

smooth_opengl3.c:124:1: error:unknown type name 'PFNGLBUFFERDATAPROC'

PFNGLBUFFERDATAPROC gl_BufferData;

^

smooth_opengl3.c:125:1: error:unknown type name 'PFNGLCREATESHADERPROC'

PFNGLCREATESHADERPROC gl_CreateShader;

^

smooth_opengl3.c:126:1: error:unknown type name 'PFNGLSHADERSOURCEPROC'

PFNGLSHADERSOURCEPROC gl_ShaderSource;

^

smooth_opengl3.c:127:1: error:unknown type name 'PFNGLCOMPILESHADERPROC'

PFNGLCOMPILESHADERPROC gl_CompileShader;

^

smooth_opengl3.c:128:1: error:unknown type name 'PFNGLCREATEPROGRAMPROC'

PFNGLCREATEPROGRAMPROC gl_CreateProgram;

^

smooth_opengl3.c:129:1: error:unknown type name 'PFNGLATTACHSHADERPROC'

PFNGLATTACHSHADERPROC gl_AttachShader;

^

smooth_opengl3.c:130:1: error:unknown type name 'PFNGLLINKPROGRAMPROC'

PFNGLLINKPROGRAMPROC gl_LinkProgram;

^

smooth_opengl3.c:131:1: error:unknown type name 'PFNGLUSEPROGRAMPROC'

PFNGLUSEPROGRAMPROC gl_UseProgram;

^

smooth_opengl3.c:132:1: error:unknown type name 'PFNGLGETSHADERIVPROC'

PFNGLGETSHADERIVPROC gl_GetShaderiv;

^

smooth_opengl3.c:133:1: error:unknown type name 'PFNGLGETSHADERINFOLOGPROC'

PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;

^

smooth_opengl3.c:134:1: error:unknown type name 'PFNGLGETPROGRAMIVPROC'

PFNGLGETPROGRAMIVPROC gl_GetProgramiv;

^

smooth_opengl3.c:135:1: error:unknown type name 'PFNGLGETPROGRAMINFOLOGPROC'

PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;

^

smooth_opengl3.c:136:1: error:unknown type name 'PFNGLGETATTRIBLOCATIONPROC'

PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;

^

smooth_opengl3.c:137:1: error:unknown type name 'PFNGLVERTEXATTRIBPOINTERPROC'

PFNGLVERTEXATTRIBPOINTERPROC gl_VertexAttribPointer;

^

smooth_opengl3.c:138:1: error:unknown type name

      'PFNGLENABLEVERTEXATTRIBARRAYPROC'

PFNGLENABLEVERTEXATTRIBARRAYPROC gl_EnableVertexAttribArray;

^

smooth_opengl3.c:139:1: error:unknown type name 'PFNGLGETUNIFORMLOCATIONPROC'

PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;

^

smooth_opengl3.c:140:1: error:unknown type name 'PFNGLUNIFORMMATRIX4FVPROC'

PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;

^

fatal error: too many errors emitted, stopping now [-ferror-limit=]

20 errors generated.

make[4]: *** [smooth_opengl3-smooth_opengl3.o] Error 1

make[3]: *** [all-recursive] Error 1

make[2]: *** [all-recursive] Error 1

make[1]: *** [all-recursive] Error 1

有問題,於是把修改的巨集去掉。重新make all。沒有報錯。make install.

再參考:

http://lazyfoo.net/tutorials/OpenGL/01_hello_opengl/mac/xcode/index.php

1)Start up XCode and create a command line tool project.

Remove the main.cpp file autogenerated for the project as we're not using it. Go download the source for lesson 01. Add the source files inside to your project.

2)Click on your project to reveal the Build Settings tab. Click on the Build Settings tab. Under in the Linking section in the Other Linker Flags add -lGLUT. This will make the project link with the freeGLUT Unix library we installed.

3)In the Search Paths section add
"/opt/X11/include" "/usr/local/include"
to the Header Search Paths and
"/opt/X11/lib" "/usr/local/lib"
to the Library Search Paths

This makes it so XCode will find the header/library files for XQuartz (/opt/X11/) and freeGLUT (/usr/local/).

4)Click on the Build Phases Tab, open up Link Binary with Libraries and add the OpenGL framework.

Now build. If there are any errors, make sure you didn't skip a step.

Now that you have OpenGL and freeGLUT compiling, it time to go onto part 2 of the tutorial.

該步很順利,"/opt/X11/lib" "/usr/local/lib"
to the Library Search Paths,圖片顯示有誤


相關文章