使用g ++编译使用fltk和OpenGL的文件会在MacO上产生错误

问题描述

我正在尝试编译2个.cpp文件,这些文件在MacO上使用OpenGL和fltk。 在使用linux(Fedora)的设备上,我使用以下命令: g++-9 -std=c++17 window.cpp prova.cpp -o test -lfltk -lGL -lglut -lglu -lfltk_gl 一切都很好。

在Mac_Os终端上,我尝试使用: g++ -framework gluT -framework OpenGL -lglu -lfltk -lglut -lGL -framework Cocoa window.cpp prova.cpp,我得到无数的错误(我在Linux上没有)和警告:

      (Define GL_SILENCE_DEPRECATION to silence these warnings) [-Wdeprecated-declarations]
inline void gl_rectf(int x,int y,int w,int h) {glrecti(x,y,x+w,y+h);}
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl.h:2588:13: note: 
      'glMatrixMode' has been explicitly marked deprecated here
extern void glMatrixMode (GLenum mode) OPENGL_DEPRECATED(10.0,10.14);
            ^
prova.cpp:211:5: warning: 'glLoadIdentity' is deprecated: first deprecated in macOS 10.14 - OpenGL API deprecated. (Define
      GL_SILENCE_DEPRECATION to silence these warnings) [-Wdeprecated-declarations]
    glLoadIdentity();

现在,我还尝试使用通过自制软件安装的g ++-10进行以下操作:

g++-10 -framework OpenGL -framework gluT window.cpp prova.cpp

但是我明白了

    1 | #include <FL/Fl.H>
      |          ^~~~~~~~~
compilation terminated.
prova.cpp:9:10: Fatal error: FL/gl.h: No such file or directory
    9 | #include <FL/gl.h>
      |          ^~~~~~~~~

(这可能是问题所在),所以我将fltk lib包括在内:

g++-10 -framework OpenGL -framework gluT -I /usr/local/Cellar/fltk/1.3.5/include window.cpp prova.cpp

现在获得临时输出

Undefined symbols for architecture x86_64:
  "__Z12glutIdleFuncPFvvE",referenced from:
      __Z14CreateMyWindowiPPc in ccApCsrQ.o
  "__Z13glutSolidConeddii",referenced from:
      __Z11displayConev in ccyWlxbb.o
  "__Z15glutSwapBuffersv",referenced from:
      __Z11displayConev in ccyWlxbb.o
  "__Z16glutCreateWindowPKc",referenced from:
      __Z14CreateMyWindowiPPc in ccApCsrQ.o
  "__Z18glutinitwindowSizeii",referenced from: // .... and so on 

这是macOS中的一种错误(我认为),当您不包含框架时会出现这种错误

有人知道这是什么问题吗? (这2个.cpp文件在Linux上可以正常运行,所以这是MacO的问题)

解决方法

带有-I /usr/local ....选项,可以解决包含文件的问题,请尝试添加

-L /usr/local/Cellar/fltk/1.3.5/lib   (look up the exact folder name with .a and .so files)

可选,它告诉g ++-10程序包中的链接程序在目录中查找库文件本身。另外,如果要显式链接到FLTK,则可能必须使用-lfltk