[swift-users] Swift can’t compile code with OpenGL function pointers

Kelvin Ma kelvinsthirteen at gmail.com
Thu Mar 30 11:42:18 CDT 2017


I’m also guessing I have to disable the default function loader GLFW
provides? What do I do about GL defined types such as GLenuum and GLint?

On Thu, Mar 30, 2017 at 11:40 AM, Kelvin Ma <kelvinsthirteen at gmail.com>
wrote:

> There are hundreds of gl functions… I have to rewrite the signatures for
> all of them??
>
> On Thu, Mar 30, 2017 at 11:31 AM, Joe Groff <jgroff at apple.com> wrote:
>
>>
>> > On Mar 30, 2017, at 7:47 AM, Kelvin Ma via swift-users <
>> swift-users at swift.org> wrote:
>> >
>> > OpenGL functions are loaded at runtime by a function loader (like
>> GLFW). They’re defined in a header but obviously they don’t have
>> definitions at compile time so it causes a slew of linker errors when I try
>> to build
>> >
>> >     error: undefined reference to 'glEnable'
>> >     error: undefined reference to 'glBlendFunc'
>> >     error: undefined reference to 'glClearColor'
>> >     clang: error: linker command failed with exit code 1 (use -v to see
>> invocation)
>> >     ...
>> >
>> > How do I build programs that use OpenGL functions?
>>
>> If the functions are exported by the OpenGL library you're linking
>> against, then you may need to just use -lGL to link against it. That's not
>> likely to be portable, though, since implementations vary in what they
>> statically export. The macro metaprogramming used by GLFW and other
>> libraries to dynamically load GL entry points is probably not going to get
>> picked up by Swift's C importer, so you'd need to roll your own solution.
>> Something like this might work as a start:
>>
>> func loadGLFunction<T>(name: String) -> T {
>> #if os(macOS)
>>   return unsafeBitCast(dlsym(RTLD_DEFAULT, name), to: T.self)
>> #elseif os(Linux)
>>   return unsafeBitCast(glXGetProcAddress(name), to: T.self)
>> #elseif os(Windows)
>>   return unsafeBitCast(wglGetProcAddress(name), to: T.self)
>> #endif
>> }
>>
>> enum GL {
>>   static let begin: @convention(c) (GLenum) -> Void =
>> loadGLFunction("glBegin")
>>   static let end: @convention(c) () -> Void = loadGLFunction("glEnd")
>>   /*etc*/
>> }
>>
>> -Joe
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170330/54d739ee/attachment.html>


More information about the swift-users mailing list