Can't get GLFW to link

后端 未结 1 1614
暖寄归人
暖寄归人 2021-02-09 00:09

I am trying to compile the below test program:

#include 

int main(int argc, char** argv) {
    if(!glfwInit()) {
        return -1;
    }

             


        
1条回答
  •  被撕碎了的回忆
    2021-02-09 00:42

    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)
    

    0 讨论(0)
提交回复
热议问题