简体   繁体   中英

Undefined reference with glfw3 using g++ on windows

I'm trying to create an opengl project for this tutorial , and I can't get my program to compile.

The current problem here is that every glfw functions are undefined, but they exist in glfw3.h.

The glfw3 files are from the glfw download page (x64 version).

Here is a copy of the logs:

make
=== SRC ===
src/glad.c src/main.cpp
=== OBJ ===
bin/glad.o bin/main.o
===== Creating file bin/glad.o ===
C:/cygnus/cygwin-b20/H-i586-cygwin32/bin/mkdir -p bin/
C:/MinGW/bin/g++ -I./include -o bin/glad.o -c src/glad.c
===== Creating file bin/main.o ===
C:/cygnus/cygwin-b20/H-i586-cygwin32/bin/mkdir -p bin/
C:/MinGW/bin/g++ -I./include -o bin/main.o -c src/main.cpp
C:/MinGW/bin/g++  -L./libs -o build/program.exe bin/glad.o bin/main.o -lglfw3
bin/main.o:main.cpp:(.text+0xc): undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
make: *** [program.exe] Erreur 1

I'm using the following main.cpp file:

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

int main(int argc, char** argv){

    glfwInit();

    return 0;
}

And the following makefile:

CXX = C:/MinGW/bin/g++
FAKEPATH = C:/cygnus/cygwin-b20/H-i586-cygwin32/bin/

CXXFLAGS  = -W -Wall -ansi -Wno-deprecated
LDLIBS = -lglfw3

TARGET  = program.exe

LIB_DIR = -L./libs
INC_DIR = -I./include

SRC = $(shell $(FAKEPATH)find src/ -type f \( -iname \*.cpp -o -iname \*.c \))
OBJ = $(patsubst src/%,bin/%,$(addsuffix .o, $(basename $(SRC))))


all: directories $(TARGET)

test:
    echo $(FAKEPATHEXISTS)

directories:
    @$(FAKEPATH)echo === SRC ===
    @$(FAKEPATH)echo $(SRC)
    @$(FAKEPATH)echo === OBJ ===
    @$(FAKEPATH)echo $(OBJ)
    @$(FAKEPATH)mkdir -p bin
    @$(FAKEPATH)mkdir -p build

compileAndRun: all
    build/$(TARGET)

$(TARGET): $(OBJ)
    $(CXX) $(LDFLAGS) $(LIB_DIR) -o build/$@ $^ $(LDLIBS)

bin/%.o: src/%.c
    @$(FAKEPATH)echo ===== Creating file $@ ===
    $(FAKEPATH)mkdir -p $(dir $@)
    $(CXX) $(INC_DIR) -o $@ -c $<

bin/%.o: src/%.cpp
    @$(FAKEPATH)echo ===== Creating file $@ ===
    $(FAKEPATH)mkdir -p $(dir $@)
    $(CXX) $(INC_DIR) -o $@ -c $<

clean:
    @$(FAKEPATH)rm -rf bin/

cleaner: clean
    @$(FAKEPATH)rm -rf build/

And finally, my files are sorted like this:

档案

I'm using MinGW for g++ and cygwin for the linux commands.

Any help or try would be really appreciated ! (I really don't know where the problem came from) Thanks !

Note: I already searched on google a lot without finding any working solution (including several stack overflow questions)

I found a solution to this.

The first things I had to do is add #define GLFW_DLL in my main.cpp.

After doing this I had the error Undefined reference to '_imp__glfwInit' . To solve this error I had to remove the *.a files in my /libs, and then I moved my .dll file to the root of the project (To avoid a missing dll error).

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