Compiling with GLFW3, linker errors 'undefined reference'
Asked Answered
B

2

7

Having a minimal example using GLFW3:

#include <GLFW/glfw3.h>

int main(int argc, const char * argv[]) {
    glfwInit();
}

...results in a ton of linker errors: (small excerpt)

Undefined symbols for architecture x86_64:
  "_CFArrayAppendValue", referenced from:
      _addJoystickElement in libglfw3.a(cocoa_joystick.m.o)
  "_CFArrayApplyFunction", referenced from:
      __glfwInitJoysticks in libglfw3.a(cocoa_joystick.m.o)
      _addJoystickElement in libglfw3.a(cocoa_joystick.m.o)
  "_CFArrayCreateMutable", referenced from:
      __glfwInitJoysticks in libglfw3.a(cocoa_joystick.m.o)

  ...

  "_objc_msgSend_fixup", referenced from:
  l_objc_msgSend_fixup_count in libglfw3.a(cocoa_monitor.m.o)
  l_objc_msgSend_fixup_objectAtIndex_ in libglfw3.a(cocoa_monitor.m.o)
  l_objc_msgSend_fixup_objectForKey_ in libglfw3.a(cocoa_monitor.m.o)
  l_objc_msgSend_fixup_alloc in libglfw3.a(cocoa_init.m.o)
  l_objc_msgSend_fixup_release in libglfw3.a(cocoa_init.m.o)
  l_objc_msgSend_fixup_alloc in libglfw3.a(nsgl_context.m.o)
  l_objc_msgSend_fixup_release in libglfw3.a(nsgl_context.m.o)
  ...

OpenGL.framework and libglfw3.a are both linked.

enter image description here

What is the reason for this? Compiling a glfw2 application before worked like a charm.

Brower answered 22/8, 2013 at 21:55 Comment(3)
glfw3 probably made the switch from Carbon (C) to Cocoa (Objective C) for its render context / window management interface. With each version of OS X, less and less core OS functionality is exposed through the Carbon interface and it gets worse if you are writing a 64-bit Carbon-based application. Try adding the Cocoa framework to your build configuration.Huffy
Did you solve the problem somehow?Telegraphy
@FrancescoBoi I had to link to the IOKit, CoreVideo and Cocoa frameworks. See the answers to this question.Brower
H
13

In Mac OS X, glfw3 uses Cocoa (NSGL) for its OpenGL context management. glfw2 used Carbon (CGL/AGL, depending on fullscreen or windowed mode).

NSGL is more robust, but it is built upon an Objective C framework (Cocoa). In order for your software to work correctly with glfw3, you should include the Cocoa framework.

Huffy answered 22/8, 2013 at 22:25 Comment(3)
Hey, thanks for your answer, that did the trick. Additionally to the Cocoa framework, the IOKit framework was also required though.Brower
I also just had this issue and in addition to the Cocoa and IOKit I also had to add the CoreVideo kit and then it was working for me.Ragouzis
@TrevorPeyton: Ah, I see. When I originally wrote this answer, it was to explain only the unresolved symbols in the original question. It appears there are more dependencies introduced with GLFW3 than I originally thought.Huffy
A
28

If you are using the static library version of GLFW, add it and the Cocoa, OpenGL, IOKit and CoreVideo frameworks to the project as dependencies.

Adnate answered 29/12, 2013 at 5:16 Comment(4)
Thanks. Just for reference, I'll post the command line that just did the trick on my system: gcc -Iglfw3/include/ -Lglfw3/lib/ -lglfw3 -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo main.c.Wethington
In my case, I only needed -l glfw3 and that solved it.Morley
In keys if someone is interesting of using GLFW with Swift 3Adnate
I added everything suggested except OpenGL (since I am building w Vulkan) and it worked.Pertinent
H
13

In Mac OS X, glfw3 uses Cocoa (NSGL) for its OpenGL context management. glfw2 used Carbon (CGL/AGL, depending on fullscreen or windowed mode).

NSGL is more robust, but it is built upon an Objective C framework (Cocoa). In order for your software to work correctly with glfw3, you should include the Cocoa framework.

Huffy answered 22/8, 2013 at 22:25 Comment(3)
Hey, thanks for your answer, that did the trick. Additionally to the Cocoa framework, the IOKit framework was also required though.Brower
I also just had this issue and in addition to the Cocoa and IOKit I also had to add the CoreVideo kit and then it was working for me.Ragouzis
@TrevorPeyton: Ah, I see. When I originally wrote this answer, it was to explain only the unresolved symbols in the original question. It appears there are more dependencies introduced with GLFW3 than I originally thought.Huffy

© 2022 - 2024 — McMap. All rights reserved.