Search code examples
c++macoslinkerldglfw

Linkers: file was built for archive which is not the architecture being linked (x86_64) GLFW


Hello, this problem make me crazy and i need your help. I am on a IOS High Sierra v10.13.6 and i am trying to try OpenGL on VSC on mac without XCode. So i have downloaded the library GLFW for the right OS. I have tried the basic exemple from the GLWF website.

#include "../GLFW/glfw3.h"
// g++ -v main.cpp -o main.o -L/Library/Developer/CommandLineTools/usr/include -lglfw3 -framework OpenGL
// file was built for archive which is not the architecture being linked (x86_64):
int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

i have the following directory: enter image description here

When i do this command:

g++ -v main.cpp -o main.o -L/Library/Developer/CommandLineTools/usr/include -lglfw3 -framework OpenGL

I have the following error in the terminal:

Undefined symbols for architecture x86_64:
  "_glfwCreateWindow", referenced from:
      _main in main-ca9141.o
  "_glfwInit", referenced from:
      _main in main-ca9141.o
  "_glfwMakeContextCurrent", referenced from:
      _main in main-ca9141.o
  "_glfwPollEvents", referenced from:
      _main in main-ca9141.o
  "_glfwSwapBuffers", referenced from:
      _main in main-ca9141.o
  "_glfwTerminate", referenced from:
      _main in main-ca9141.o
  "_glfwWindowShouldClose", referenced from:
      _main in main-ca9141.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I understand that i came from this warning:

ld: warning: ignoring file /Library/Developer/CommandLineTools/usr/include/libglfw3.a, file was built for archive which is not the architecture being linked (x86_64): /Library/Developer/CommandLineTools/usr/include/libglfw3.a

I have checked the library with the lipo command and it say that his architecture is x86_64! I don't know why it does not work, can you help me?

Thanks in advance.


Solution

  • i want to say that i have found the solution for my problems. You have to double check that the library libglfw3.a is compiled in the good architecture witch is the x86_64.

    Here the official link to download the GLFW library in the right OS

    Also here is the command you need to run for make it compile:

    You need to make sure to specify the path to the library through -L

    g++ main.cpp -o main -L/Library/Developer/CommandLineTools/usr/include -lglfw3 -framework Cocoa -framework OpenGL -framework IOKit
    

    Hope it will help for the futur dev who are stuck in the same problem