g++glfw cannot link.

Asked 2 years ago, Updated 2 years ago, 171 views

Regarding the presentation console screen, when I link to the library, I get a large amount of undecided errors.Why is this? As you can see in the answer, I looked up how to use the library link, but I don't
Also, I tried to reproduce the minimum environment, but I cannot link it.
Utilization Library
stb
opengl
glew
glfw
freetype

mingw32
Enter a description of the image here
Source code: https://www.dropbox.com/s/pa3z0ih18lfcqxt/AAEditor.zip?dl=0

Reference Site C: https://www.glfw.org/docs/3.3/build_guide.html
Reference Site A:
https://stackoverflow.com/questions/18475234/undefined-references-to-imp-glew-functions-with-mingw-gcc

Reference Site B: http://glew.sourceforge.net/install.html

Windows 10 home
VSCode

glfw:https://www.glfw.org/
glew:http://glew.sourceforge.net/

Add int main() function Main.cpp
Note: When I replaced the file with glew32.lib in the glew/lib/x64/ file, the following command error changed:

Redownload glfw and navigate through the folder Mingw to the directory of the presented image
$(CXX)$^-o$@-L/lib-lglfw3-lopengl32
$(CXX)$^-o$@-lglfw3dll-lopengl32

Minimum code: https://www.glfw.org/documentation.html
I tried compiling only with , but I got the same undefined error.

Also, I checked the URL below which I cannot link with gcc, but it was not applicable.
https://teratail.com/questions/89964

I compiled a simple source file called intmain() {printf("Hello World");}, but the compilation went as usual and the characters were displayed.

I tried using the minimum environment, but the same error occurred and it is undefined.
The compilation went through.
I changed the order of linking, but the result was the same.
Viewed: Include Library
You will also need to change the "GLEW_BUILD preprocessor constant" to "GLEW_STATIC" from the reference site B.Otherwise, a build error will occur." From the sentence

#define GLEW_BUILD GLEW_STAIC
#define GLEW_STAIC GLEW_BUILD

Tried the

I implemented it like a presentation code based on the reference site, but I still get a lot of the same error.Also, I'm translating it into Japanese, but I don't understand this sentence because it's ambiguous.
"Add rules to Makefile to define this preprocessor token.#define..."What does that mean?

>make
g++-MMD-MP-DGLEW_STATIC-IC:/glew-2.1.0/include-IC:/glfw-3.3.6/include-IC:/glm-IC:/stb-IC:/freetype-2.3.5-1/include-IC:/freetype-2.3.5-1/include/freetype2-c source/main.object
g++ object/Window.o object/Help.o object/Simple.o object/Shader.o object/Model.o object/Actor.o object/Primitive_Shape.o object/Text.o object/Resource.o object/Init.o object/Main.o object/Camera.o object/Transform.o object/Sprite.o object/Shape.o object/Render.o -o test -LC:/glew-2.1.0/lib/Release/Win32 -LC:/glfw-3.3.6/lib-mingw -LC:/freetype-2.3.5-1/lib -lglew32s -lopengl32-lglfw3-lgdi32-lfreetype
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x26a): undefined reference to `glfwTerminate'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x2e9): undefined reference to `glfwMakeContextCurrent'

// Omitted because it is long

collect2.exe:error:ld returned1 exit status
Makefile: 14: recipe for target 'GL' failed
make:*** [GL] Error 1
#defineGLEW_STATIC
# ifdef GLEW_STATIC
#  define GLEWAPI external
# else
#  ifdef GLEW_BUILD
#    define GLEWAPI external_declspec (dllexport)
#  else
#    define GLEWAPI external_declspec (dlimport)
#  endif
#endif


# include "../header/Window.hpp"

# include <iostream>
# include <limits.h>
# include <chrono>
# include <thread>
# include <time.h>
#include <stdio.h>
#include <wchar.h>

//#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "../header/Window.hpp"
#include "../header/Init.hpp"
#include "../header/Window_use.hpp"
#include <string>


# ## ## ## ## ## ##   / #### #### #### #### #### ####     the constructor #### ### #### # # ## ## ## ## ## ## ## ## ## ## ## ## 
FrameWork::Window::Window(GLFWwindow* win, glm::ivec2 s,const char* t)
{
    //windowSize  = size;
    window = win;
    size = s;
    title = std::string(t);
    prevMouseVector = glm::dvec2(0,0);
PRG:=GL
SRC_DIR: = source
INC_DIR:=-IC:/glew-2.1.0/include-IC:/glfw-3.3.6/include-IC:/glm-IC:/stb-IC:/freetype-2.3.5-1/include-IC:/freetype-2.3.5-1/include/freetype2 
LIB_DIR:=-LC:/glew-2.1.0/lib/Release/Win32-LC:/glfw-3.3.6/lib-mingw-LC:/freetype-2.3.5-1/lib
LIB_LST: = -lglew32s-lopengl32-lglfw3-lgdi32-lfreetype 
OBJ_DIR: = object
DEP_DIR: = object
DEP: =$(wildcard$(DEP_DIR)/*.d)
SRC: =$(wildcard$(SRC_DIR)/*.cpp)
OBJ: =$(addprefix$(OBJ_DIR)/,$(patsubst%.cpp, %.o, $(notdir$(SRC)))))
CXXFLAGS: =-MMD-MP-DGLEW_STATIC$(INC_DIR)

$(PRG): $(OBJ)
    $(CXX)$^-o$@$(LIB_DIR)$(LIB_LST)
 

$(OBJ_DIR)/%.o:source/%.cpp
    $(CXX)$(CXXFLAGS)-c$<-o$@
-include$(DEP)
    
US>clean:
    rm-f./$(OBJ_DIR)/*.o./$(OBJ_DIR)/*.d$(PRG)
    

c++ windows mingw

2022-09-29 22:46

4 Answers

The error message is the same.

But it's different. In Revision 16,

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../mingw32/bin/ld.exe:object/Camera.o:Camera.cpp: (.text+0x81): undefined reference to `_imp__glewGenVertexArray'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Camera.o:Camera.cpp: (.text+0x9c): undefined reference to `_imp__glewGenBuffers'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Camera.o:Camera.cpp: (.text+0x113): undefined reference to `_imp__glewBindVertexArray'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Camera.o:Camera.cpp: (.text+0x128): undefined reference to `_imp__glewBindBuffer'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Camera.o:Camera.cpp: (.text+0x145): undefined reference to `_imp___glewBufferData'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Camera.o:Camera.cpp: (.text+0x18e): undefined reference to `_imp___glewEnableVertexAttribArray'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Camera.o:Camera.cpp: (.text+0x1a0): undefined reference to `_imp__glewVertexAttribPointer'

and _imp___glewGenVertexArrays are requested.For Revision 17,

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x51d): undefined reference to `glfwGetWindowUserPointer'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x54d): undefined reference to `glfwGetWindowUserPointer'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x5a1): undefined reference to `glfwGetFramebufferSize'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x5d1): undefined reference to `glfwGetWindowUserPointer'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x63f): undefined reference to `glfwGetWindowUserPointer'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x6b0): undefined reference to `glfwGetWindowUserPointer'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x74f): undefined reference to `glfwGetCursorPos'

and glfwGetWindowUserPointer.

In Windows, the .LIB file has two formats:

  • static link library
    The required code is included in this file to create an executable.
  • import library
    Contains the DLL name required at runtime.A DLL is required at runtime even if the link is successful.

There is a difference that
GLEW_STATIC appears, but if you specify GLEW_STATIC at compile time, you must pass the .LIB file from the former static link library to the linker.Conversely, if you do not specify GLEW_STATIC, you will probably need to pass the import library .LIB file to the linker.

If GLFW needs a similar issue and is now requesting glfwGetWindowUserPointer, you must pass the static link library .LIB to the linker.Alternatively, you will need to compile it to request the .LIB file for the import library, using GLFW.

The reason why I asked you to list the source and version is to see how it should be specified.

and so on.

-L, but I copied the .lib of each library to the lib folder of gcc, so I don't think I need to specify it.

This operation is bad.The directory containing the static link library or the directory containing the import library should be specified in -L so that it can be switched.

By the way, do you want a dynamic link that requires DLLs at runtime or a static link that does not require GLWhether it's GLEW or GLFW, which one do you want?
(It depends on GLEW/GLFW, others support both, or only one library)


2022-09-29 22:46

To summarize Undefined References to_imp__glew*functions with minGW gcc introduced by Cubick,

  • If you want to statically link glew (not applicable if you want to dynamic link)
  • Define the macro GLEW_STATIC before #include "glew.h". You may want to use Makefile
  • You must not define the macro GLEW_BUILD

Makefile should be rebuilt after make clean by doing the following:

CXXFLAGS=-std=c++11-MMd-MP-DGLEW_STATIC

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


2022-09-29 22:46

There seems to be a lot of trial and error going on, but if you do the following two things, you will get a warning, but the link will end and you will be able to exe.

-lglew32s-lopengl32-lglfw3-lgdi32-lfreetype

The first point is that I found the following two articles and the comments said that the order of definition also affects the order of definition, so I tried various things and found the above.
-lfreetype can be placed anywhere.
Linker problems with GLEW and CodeBlcks
GLFWMinGW link error

Second, undefined reference to 'WinMain@16' didn't disappear even after taking the above action, so I searched and got to the following.
"undefined reference to `WinMain@16'"
in mingw32 Mingw32 pitfalls in Unicode ~_tWinMain/wWinMain problems, etc. ~

Mingw32 seems to be a problem, so MSYS2/MinGW-w64 (64bit/32bit) may not be necessary, but for now, as a solution to the initial environment of the question.

However, the warning Warning:corrupt.drectve at end of def file appears when linking, so you will still need to take action (.def file creation?) to complete it.

The executable GL.exe is ready and can be started.
I put in the int main() function to exit without any action just for confirmation, so I finished it as soon as I started it, but I think the executable file that works is ready for now.
Well, I didn't call any functions of the library project, so I will be able to check them out from now on, but I think I can proceed.

To build your environment, follow these steps:

Download mingw-get-setup.exe from SourceForge and install it in C:\MinGW.
Only C:\MinGW\bin is added for PATH.
MinGW-Minimalist GNU for Windows
MinGW Installation Instructions
MinGW Installation Instructions

Downloaded glew-2.1.0-win32.zip from the The OpenGL Extension Wrangler Library and expanded to C:\glew-2.1.0.
Add C:\glew-2.1.0\bin\Release\Win32 to PATH (maybe you don't need this?)

Download and deploy 32-bit Windows binaries glfw-3.3.6.bin.WIN32.zip from GLFW>Download to C:\glfw-3.3.3.6.
Add C:\glfw-3.3.6\lib-mingw to PATH

Download and deploy glm-0.9.9.8.zip from g-truc/glm/GLM0.9.9.8 to C:\glm.

Download and deploy stb-master.zip from notes/stb to C:\stb.

Downloaded Binaries freetype-2.3.5-1-bin.zip from FreeType for Windows and expanded to C:\freetype-2.3.5-1.
Add C:\freetype-2.3.5-1\bin to PATH

The Makefile has been modified as follows:
Instead of moving/aggregating the files in each library, I try to add the designation in the Makefile separately.

PRG:=GL
SRC_DIR: = source
INC_DIR:=-IC:/glew-2.1.0/include-IC:/glfw-3.3.6/include-IC:/glm-IC:/stb-IC:/freetype-2.3.5-1/include-IC:/freetype-2.3.5-1/include/freetype2 
LIB_DIR:=-LC:/glew-2.1.0/lib/Release/Win32-LC:/glfw-3.3.6/lib-mingw-LC:/freetype-2.3.5-1/lib
LIB_LST: = -lglew32s-lopengl32-lglfw3-lgdi32-lfreetype 
OBJ_DIR: = object
DEP_DIR: = object
DEP: =$(wildcard$(DEP_DIR)/*.d)
SRC: =$(wildcard$(SRC_DIR)/*.cpp)
OBJ: =$(addprefix$(OBJ_DIR)/,$(patsubst%.cpp, %.o, $(notdir$(SRC)))))
CXXFLAGS=-std=c++11-MMD-MP-DGLEW_STATIC$(INC_DIR)

$(PRG): $(OBJ)
    $(CXX)$^-o$@$(LIB_DIR)$(LIB_LST)
 

$(OBJ_DIR)/%.o:source/%.cpp
     $(CXX)$(CXXFLAGS)-c$<-o$@
-include$(DEP)
    
US>clean:
    rm-f./$(OBJ_DIR)/*.o./$(OBJ_DIR)/*.d$(PRG)

It seems that the sense of smell that finds the cause doesn't grow well.
Makefile:14:***multiple target patterns.Stop. is probably due to blank spaces at the beginning of the following three lines that should start with tab characters, as commented by yourself.
OpenCV pingw-make32Makefile:12:***multiple target patterns.Stop
What happens over and over again is to convert tab characters to blank at every checkpoint, such as save or compile, in the VSCode editor and extension settings?

$(CXX)$^-o$@$(LIB_DIR)$(LIB_LST)
$(CXX)$(CXXFLAGS)-c$<-o$@
rm-f./$(OBJ_DIR)/*.o./$(OBJ_DIR)/*.d$(PRG)

Any half-baked changes that are not tabbed at the beginning of any of the above three lines will result in the following errors:

Makefile:14:*** multiple target patterns.Stop.

Makefile: 18:*** multiple target patterns. Stop.

Makefile: 22:*** missing separator. Stop.

The error code changed when I replaced 64-bit version? with // There must have been a hint that I omitted because it is long.
Regardless of 32-bit/64bit, it is probably caused by the absence of the intmain() function pointed out in 2. at the beginning of my answer.

If you change the above Makefile blank characters to tab characters even if the 32-bit version remains, you will get the following error:
//Is there an undefined reference to 'WinMain@16' that appears to have been omitted because it is long?

g++-MMD-MP-DGLEW_STATIC-IC:/glew-2.1.0/include-IC:/glfw-3.3.6/include-IC:/glm-IC:/stb-IC:/freetype-2.3.5-1/include-IC:/freetype-2.3.5-1/include-IC:/window type.object
g++ object/Window.o object/Help.o object/Simple.o object/Shader.o object/Model.o object/Actor.o object/Primitive_Shape.o object/Text.o object/Resource.o object/Init.o object/Camera.o object/Transform.o object/Sprite.o object/Shape.o object/Render.o -o GL -LC:/glew-2.1.0/lib/Release/Win32 -LC:/glfw-3.3.6/lib-mingw -LC:/freetype-2.3.5-1/lib -lglew32s -lopengl32 -lglfw3 -lgdi32-lfreetype
Warning:corrupt.directve at end of default file
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../mingw32/bin/ld.exe:c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../libmingw32.a(main.o):(.text.startup+0xc0): undeference@Windeference16
collect2.exe:error:ld returned1 exit status
Makefile: 14: recipe for target 'GL' failed
mingw32-make:*** [GL] Error 1

Even if you don't change the source code you originally created, you put sample code of in the main.cpp folder of the project.You can do "ru".


2022-09-29 22:46

g++ object/Window.object/Help.object/Simple.object/Shader.object/Model.object/Actor.object/Primitive_Shape.object/Text.object/Resource.object/Init.object/Main.object/Camera.object.object.object/Section.object.object.object.object.object.object.object.object.object.object.object.object.object.object.object.object.object.object.object.object.object.Skama.object.object.object.object.object.object.object.object.object.objecto-otest-LC:/glew-2.1.0/lib/Release/Win32-LC:/glfw-3.3.6/lib-mingw-LC:/freetype-2.3.5-1/lib-lglew32s-lopengl32-lglfw3-lgdi32-lfreetype
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:object/Window.o: Window.cpp: (.text+0x26a): undefined reference to `glfwTerminate'

If you only respond to the , the gcc of the mingw may change whether the library designation (-l) or the object file to be linked comes before the command line.
(Linux will be the same regardless of which comes first.I think Cygwin was the same as Mingw)

Try the Makefile

$(PRG):$(OBJ)
    $(CXX)$^-o$@$(LIB_DIR)$(LIB_LST)

$(PRG):$(OBJ)
    $(CXX)$(LIB_DIR)$(LIB_LST)$^-o$@

Try it.


2022-09-29 22:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.