Can't get GLFW to link

二次信任 提交于 2019-12-03 07:13:48

The problem is that the glfw libraries are being specified to the linker before the object file that depends on them. ld searches libraries to resolve dependencies only for the dependencies that it knows about at the point in list of files it's processing. So when ld is searching libglfw.a it doesn't know about the glfwInit dependency in main.o yet. ld (by default) doesn't go back an search the library again.

Try:

$(EXECUTABLE):
    $(CXX) $(CXXFLAGS) $(LIB_DIR) -o $@ $(OBJECTS)  $(LDFLAGS)

Also the libraries should probably be specified in a LDLIBS (or LIBS) variable - LDFLAGS is conventionally use for linker options:

CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDLIBS = -lglfw -lGL -lGLU

OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include

SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello

all: init $(OBJECTS) $(EXECUTABLE)

$(EXECUTABLE):
    $(CXX) $(LDFLAGS) $(LIB_DIR) -o $@ $(OBJECTS) $(LDLIBS)

$(OBJ_DIR)/%.o: %.cpp
    $(CXX) $(INC_DIR) -c $< -o $@

init:
    @mkdir -p "$(OBJ_DIR)"

clean:
    @rm -rf $(OBJ_DIR) $(EXECUTABLE)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!