opengl - 使用 ocamlbuild 构建 SDL 和 OpenGL 应用程序

标签 opengl compiler-errors ocaml sdl ocamlbuild

我正在尝试使用 ocamlsdl 和 lablgl 创建一个 OCaml 程序,但我不知道如何编译它。相关信息如下:

注意:ocamlfind 未找到 ocamlsdl。当我转到 .opam 的 lib 目录时,我找到一个 ocamlsdl 文件夹,但它只有一个名为 opam.config 的空文件。我注意到一件奇怪的事情是 main.byte 编译并且不起作用,但 main.native 根本不编译。

$opam list 
# Installed packages for 4.01.0:
...
lablgl               1.05  Interface to OpenGL
ocamlsdl            0.9.1  Interface between OCaml and SDL


$ocamlfind list
lablgl              (version: 1.05)
lablgl.glut         (version: 1.01)
lablgl.togl         (version: 1.01)
labltk              (version: [distributed with Ocaml])
...
ocamlbuild          (version: [distributed with Ocaml])
ocplib-endian       (version: 0.8)
...
sdl                 (version: 0.9.1)
sdl.sdlgfx          (version: n/a)
sdl.sdlimage        (version: n/a)
sdl.sdlmixer        (version: n/a)
sdl.sdlttf          (version: n/a)


$ cat main.ml
let main () =
    Sdl.init [`VIDEO];
    Sdlvideo.set_video_mode 200 200 [];
    Sdltimer.delay 2000;
    Sdl.quit ()

let _ = main ()


$ cat _tags
<*.ml> or "main.native" or "main.byte": package(lablgl), package(sdl)


$ ocamlbuild -use-ocamlfind main.byte
Finished, 3 targets (3 cached) in 00:00:00.


$ ./main.byte
2015-05-14 10:02:01.208 ocamlrun[5426:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1000) creating CGSWindow on line 259'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff90d64b06 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff8a3503f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff90d648dc +[NSException raise:format:] + 204
    3   AppKit                              0x00007fff8e84cb49 _NSCreateWindowWithOpaqueShape2 + 655
    4   AppKit                              0x00007fff8e84b340 -[NSWindow _commonAwake] + 2002
    5   AppKit                              0x00007fff8e809d82 -[NSWindow _commonInitFrame:styleMask:backing:defer:] + 1763
    6   AppKit                              0x00007fff8e808ecf -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1568
    7   AppKit                              0x00007fff8e80889f -[NSWindow initWithContentRect:styleMask:backing:defer:] + 45
    8   libSDL-1.2.0.dylib                  0x000000010a132077 -[SDL_QuartzWindow initWithContentRect:styleMask:backing:defer:] + 279
    9   libSDL-1.2.0.dylib                  0x000000010a12fb49 QZ_SetVideoMode + 2633
    10  libSDL-1.2.0.dylib                  0x000000010a125a89 SDL_SetVideoMode + 937
    11  dllsdlstub.so                       0x000000010a0f6d40 ml_SDL_SetVideoMode + 112
    12  ocamlrun                            0x0000000109cf1bcb caml_interprete + 40843
    13  ocamlrun                            0x0000000109cf9304 caml_main + 1124
    14  ocamlrun                            0x0000000109d1175c main + 12
    15  libdyld.dylib                       0x00007fff922437e1 start + 0
)
libc++abi.dylib: terminate called throwing an exception
Abort trap: 6


$ ocamlbuild -use-ocamlfind main.native
+ ocamlfind ocamlopt -linkpkg -package lablgl -package sdl main.cmx -o main.native
Undefined symbols for architecture x86_64:
  "_CFBundleCopyBundleURL", referenced from:
      -[SDLMain setupWorkingDirectory:] in libSDLmain.a(SDLMain.o)
  "_CFBundleGetInfoDictionary", referenced from:
      _main in libSDLmain.a(SDLMain.o)
  "_CFBundleGetMainBundle", referenced from:
      -[SDLMain setupWorkingDirectory:] in libSDLmain.a(SDLMain.o)
      _main in libSDLmain.a(SDLMain.o)
  "_CFRelease", referenced from:
      -[SDLMain setupWorkingDirectory:] in libSDLmain.a(SDLMain.o)
  "_CFURLCreateCopyDeletingLastPathComponent", referenced from:
      -[SDLMain setupWorkingDirectory:] in libSDLmain.a(SDLMain.o)
  "_CFURLGetFileSystemRepresentation", referenced from:
      -[SDLMain setupWorkingDirectory:] in libSDLmain.a(SDLMain.o)
  "_CPSEnableForegroundOperation", referenced from:
      _main in libSDLmain.a(SDLMain.o)
  "_CPSGetCurrentProcess", referenced from:
      _main in libSDLmain.a(SDLMain.o)
  "_CPSSetFrontProcess", referenced from:
      _main in libSDLmain.a(SDLMain.o)
  "_NSAllocateMemoryPages", referenced from:
      -[NSString(ReplaceSubString) stringByReplacingRange:with:] in libSDLmain.a(SDLMain.o)
  "_NSApp", referenced from:
      _main in libSDLmain.a(SDLMain.o)
  "_NSDeallocateMemoryPages", referenced from:
      -[NSString(ReplaceSubString) stringByReplacingRange:with:] in libSDLmain.a(SDLMain.o)
  "_OBJC_CLASS_$_NSApplication", referenced from:
      l_OBJC_$_CATEGORY_NSApplication_$_SDLApplication in libSDLmain.a(SDLMain.o)
      objc-class-ref in libSDLmain.a(SDLMain.o)
  "_OBJC_CLASS_$_NSAutoreleasePool", referenced from:
      objc-class-ref in libSDLmain.a(SDLMain.o)
  "_OBJC_CLASS_$_NSMenu", referenced from:
      objc-class-ref in libSDLmain.a(SDLMain.o)
  "_OBJC_CLASS_$_NSMenuItem", referenced from:
      objc-class-ref in libSDLmain.a(SDLMain.o)
  "_OBJC_CLASS_$_NSObject", referenced from:
      _OBJC_CLASS_$_SDLMain in libSDLmain.a(SDLMain.o)
  "_OBJC_CLASS_$_NSProcessInfo", referenced from:
      objc-class-ref in libSDLmain.a(SDLMain.o)
  "_OBJC_CLASS_$_NSString", referenced from:
      l_OBJC_$_CATEGORY_NSString_$_ReplaceSubString in libSDLmain.a(SDLMain.o)
      objc-class-ref in libSDLmain.a(SDLMain.o)
  "_OBJC_METACLASS_$_NSObject", referenced from:
      _OBJC_METACLASS_$_SDLMain in libSDLmain.a(SDLMain.o)
  "___CFConstantStringClassReference", referenced from:
      CFString in libSDLmain.a(SDLMain.o)
      CFString in libSDLmain.a(SDLMain.o)
      CFString in libSDLmain.a(SDLMain.o)
      CFString in libSDLmain.a(SDLMain.o)
      CFString in libSDLmain.a(SDLMain.o)
      CFString in libSDLmain.a(SDLMain.o)
      CFString in libSDLmain.a(SDLMain.o)
      ...
  "__objc_empty_cache", referenced from:
      _OBJC_METACLASS_$_SDLMain in libSDLmain.a(SDLMain.o)
      _OBJC_CLASS_$_SDLMain in libSDLmain.a(SDLMain.o)
  "__objc_empty_vtable", referenced from:
      _OBJC_METACLASS_$_SDLMain in libSDLmain.a(SDLMain.o)
      _OBJC_CLASS_$_SDLMain in libSDLmain.a(SDLMain.o)
  "_objc_msgSend", referenced from:
      -[SDLMain application:openFile:] in libSDLmain.a(SDLMain.o)
      -[SDLMain applicationDidFinishLaunching:] in libSDLmain.a(SDLMain.o)
      -[NSString(ReplaceSubString) stringByReplacingRange:with:] in libSDLmain.a(SDLMain.o)
      _main in libSDLmain.a(SDLMain.o)
  "_objc_msgSend_fixup", referenced from:
      l_objc_msgSend_fixup_length in libSDLmain.a(SDLMain.o)
      l_objc_msgSend_fixup_alloc in libSDLmain.a(SDLMain.o)
      l_objc_msgSend_fixup_release in libSDLmain.a(SDLMain.o)
      l_objc_msgSend_fixup_objectForKey_ in libSDLmain.a(SDLMain.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking
Command exited with code 2.
Compilation unsuccessful after building 4 targets (3 cached) in 00:00:00.

也许问题是我没有链接 SDL 框架,但我不知道如何使用 ocamlbuild 来做到这一点。关于如何编译它有什么想法吗?

最佳答案

关于字节码堆栈跟踪,看起来 SDLMain 没有链接。您应该确保可执行文件以 -custom 模式链接,这可以通过添加以下内容来实现:

<main.byte> : custom

到您的_tags 文件。原因解释here

然后看一下输出:

> ocamlobjinfo $(opam config var lib)/sdl/sdl.cmxa | grep Extra
Extra C object files: -lsdlstub -L/usr/local/lib -lSDLmain -lSDL

似乎需要链接到 OSX Cocoa 框架并没有记录在 cmxa 中(也许这应该向上游报告)。因此,您应该在目录中添加以下内容:

> cat myocamlbuild.ml
open Ocamlbuild_plugin
let () =
  dispatch begin function
  | After_rules ->
    flag ["link"; "ocaml"; "link_cocoa"] (S [A "-cclib"; A "-framework Cocoa"])
  | _ -> ()
 end

您的 _tags 文件应如下所示:

> cat _tags 
<*.{ml,byte,native}> : package(sdl), link_cocoa
<main.byte> : custom

使用这个我可以在我的机器上编译您的示例。

关于opengl - 使用 ocamlbuild 构建 SDL 和 OpenGL 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30239496/

相关文章:

c++ - SSBO CPU 映射返回正确的数据,但到 GPU 上的 SSBO 的数据为 'different'

c++ - 编写我的第一个模板类- “Undefined symbols”错误

ocaml - 引用模块类型签名

exception - 在 OCaml 中打破循环

c++ - GLut.h/osfinfo.c/dbgheap.c/mlock.c 中 0x76fe15de 的未处理异常

c++ - 将动画应用于 openGL 中的对象

qt - 带有 GLFW 和 Qt 的 OpenGL 游戏引擎?

compiler-errors - Android Studio编译错误消失

java - 遇到 "Exception in thread "main“java.util.NoSuchElementException”分配错误

ocaml - 为什么 OCaml 对多态变体使用子类型化?