Mac os cant find some of the symbols in the code

before the code was comiled no errors were there but once I compiled it there were some errors

code:

#include </Users/stavanmukherjee/Downloads/raylib-master/src/external/glfw/include/GLFW/glfw3.h>
#include <iostream>

int main() {
    // Initialize the GLFW library
    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

    // Set GLFW options
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Create a windowed mode window and its OpenGL context
    GLFWwindow* window = glfwCreateWindow(800, 600, "Hello World", nullptr, nullptr);
    if (!window) {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

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

    // Main loop
    while (!glfwWindowShouldClose(window)) {
        // Render here

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

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

    // Clean up and exit
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

errors:

Undefined symbols for architecture arm64:
  "_glfwCreateWindow", referenced from:
      _main in main.o
  "_glfwDestroyWindow", referenced from:
      _main in main.o
  "_glfwInit", referenced from:
      _main in main.o
  "_glfwMakeContextCurrent", referenced from:
      _main in main.o
  "_glfwPollEvents", referenced from:
      _main in main.o
  "_glfwSetErrorCallback", referenced from:
      _main in main.o
  "_glfwSwapBuffers", referenced from:
      _main in main.o
  "_glfwTerminate", referenced from:
      _main in main.o
      _main in main.o
  "_glfwWindowHint", referenced from:
      _main in main.o
      _main in main.o
      _main in main.o
  "_glfwWindowShouldClose", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture arm64

These are link errors, you need to link to GLFW as noted in the documentation.

An alternative is to use the GLFW-CMake-starter which uses CMake, follow the instructions in the readme.

1 Like

ive tried doing that and it cant find glfw3.h

code:# Compiler

CXX = clang++

Directories

SRC_DIR = /Users/stavanmukherjee/Desktop/standardDefinitionLibrary

INC_DIR = $(SRC_DIR)/hppFiles

OBJ_DIR = $(SRC_DIR)/build/objectFiles

BIN_DIR = $(SRC_DIR)/build/executable

MEDIA_DIR = $(SRC_DIR)/media

Source files

MAIN_SRC = $(SRC_DIR)/main.cpp

HPP_FILES = $(wildcard $(INC_DIR)/*.hpp)

CPP_FILES = $(wildcard $(SRC_DIR)/*.cpp)

OBJ_FILES = $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(CPP_FILES))

Output executable

EXECUTABLE = $(BIN_DIR)/your_executable_name

Compiler and linker flags

CXXFLAGS = -I$(INC_DIR) -std=c++11

LDFLAGS = -L/Users/stavanmukherjee/Desktop/standardDefinitionLibrary/hppFiles -lglfw3 -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo

Targets

all: setup $(EXECUTABLE)

setup: clean $(OBJ_FILES)

$(EXECUTABLE): $(OBJ_FILES)

mkdir -p $(BIN_DIR)

$(CXX) $(OBJ_FILES) -o $(EXECUTABLE) $(LDFLAGS)

cp -r $(MEDIA_DIR)/* $(BIN_DIR)

$(OBJ_DIR)/main.o: $(MAIN_SRC) $(HPP_FILES)

mkdir -p $(OBJ_DIR)

$(CXX) $(CXXFLAGS) -c $(MAIN_SRC) -o $(OBJ_DIR)/main.o

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(HPP_FILES)

mkdir -p $(OBJ_DIR)

$(CXX) $(CXXFLAGS) -c $< -o $@

clean:

rm -rf $(OBJ_DIR) $(BIN_DIR)

.PHONY: all setup clean

error:ld: library ‘glfw3’ not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Where is the location of the libglfw3.a file? This needs to be in a directory which you tell the linker about with -L in your LDFLAGS setting.

You have specified -L/Users/stavanmukherjee/Desktop/standardDefinitionLibrary/hppFiles which looks like it is the location of your include files. However this setting is for the location of your lib files such as libglfw3.a to link to.

If you are instead linking to the dynamic GLFW library libglfw.3.dylib use -lglfw (you still need to set the directory location as above).

Hope this helps.

1 Like