简体   繁体   中英

Why can't i properly link opengl libraries? (Windows, mingw, command line)

So basically what i am trying to do, is setting up opengl on a windows pc without VS. This is a pain, since every damn tutorial on this planet uses Visual Studio, but i simply hate it so i will not use it. What i did was downloading all the necessary libraries, setting up a project, creating a main file and than a Makefile. main.cpp:

#define FREEGLUT_STATIC
#include <windows.h>
#include <GL/glut.h>

int main(int argc, char** argv){
    glutInit(&argc,argv);
    glutCreateWindow("hello");
    glutInitWindowSize(400,400);
    glutMainLoop();
    return 0;
}

My Makefile:

INCL_DIR = include
SRC_DIR = src
LIB_DIR = lib
BUILD_DIR = build
OUTPUT_NAME = graph.exe

CXX = g++
CXXFLAGS = -I$(INCL_DIR) 
LDFLAGS = -L$(LIB_DIR) -lfreeglut -lglew32 -lopengl32 -lgdi32 -lwinmm

SRCS := $(wildcard $(SRC_DIR)/*.cpp)
OBJS := $(SRCS:%.cpp=%.o)

$(OUTPUT_NAME): $(OBJS)
    $(CXX) -o $(BUILD_DIR)/$(OUTPUT_NAME) $(OBJS) $(CXXFLAGS)

clean:
    del /f $(SRC_DIR)\*.o
    del /f $(BUILD_DIR)\$(OUTPUT_NAME)

The project hierarchy: VSCode 中的项目层次结构图

What happens when i run make:

PS C:\programming\projects\opengl_base> make
g++ -Iinclude    -c -o src/main.o src/main.cpp
g++ -o build/graph.exe src/main.o -Iinclude 
src/main.o:main.cpp:(.text+0x23): undefined reference to `__glutInitWithExit'
src/main.o:main.cpp:(.text+0x46): undefined reference to `__glutCreateWindowWithExit'
src/main.o:main.cpp:(.text+0x68): undefined reference to `__glutCreateMenuWithExit'
src/main.o:main.cpp:(.text+0xad): undefined reference to `glutInitWindowSize'
src/main.o:main.cpp:(.text+0xb2): undefined reference to `glutMainLoop'
collect2.exe: error: ld returned 1 exit status
make: *** [graph.exe] Error 1

What i understand is, that the libraries simply don't get linked for some reason. But why? Please help me

So to anyone who stumbles upon this question later on: whenever you try to link more libraries, and they depend on eachother, mess a little bit with the order of them.

Your line to link the exe file has compiler flags instead of linker flags init. To fix this in Makefile change:

        $(CXX) -o $(BUILD_DIR)/$(OUTPUT_NAME) $(OBJS) $(CXXFLAGS)

to:

        $(CXX) -o $(BUILD_DIR)/$(OUTPUT_NAME) $(OBJS) $(LDFLAGS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM