CodeBlocks' GLFW Project is outdated and works only with GLFW 2.7. I am using the latest version, which is 3.0.4, and trying to link it in CodeBlocks statically (I hope that I am using the correct terminology). I would be really happy if someone told me how to do it step by step. I would also like to create an empty project if possible and do everything manually.
This is the code that I am trying to run:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
This is what I have in my "Other linker options":
-lmingw32 -lopengl32 -lgdi32
and I also copied GLFW folder that is containing the header files in CodeBlocks/MinGW/include.
This is pretty much all I did and I get the following build log:
-------------- Build: Debug in Initializing OpenGL (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -o "bin\Debug\Initializing OpenGL.exe" obj\Debug\main.o -lmingw32 -lopengl32 -lgdi32
obj\Debug\main.o: In function `main':
D:/Development/OpenGL/Initializing OpenGL/main.cpp:8: undefined reference to `glfwInit'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:12: undefined reference to `glfwCreateWindow'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:15: undefined reference to `glfwTerminate'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:20: undefined reference to `glfwMakeContextCurrent'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:28: undefined reference to `glfwSwapBuffers'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:31: undefined reference to `glfwPollEvents'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:23: undefined reference to `glfwWindowShouldClose'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:34: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
Jigger your project config so that the mingw invocation looks something like this:
x86_64-w64-mingw32-g++.exe -Llib -o bin\Debug\glfw-proj.exe obj\Debug\main.o -lglfw3 -lopengl32 -lgdi32
Note the -lglfw3.
Procedure:
Project -> Build options...
Select the top-level configuration (i.e., not Debug or Release)
In the Search directories -> Compiler tab add the path to your GLFW headers (GLFW/glfw3.h and friend)
In the Search directories -> Linker tab add the path to your GLFW library files (libglfw3.a and friends)
In the Linker settings tab add 3 Link libraries: glfw3, opengl32, and gdi32.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I have been trying to code with OpenGL without using a IDE, but the manual linking is really confusing me. I have a folder with the libglfw3.a, glfw3.h and a .cpp file with this example code.
#include "glfw3.h"
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I compile it like this.
g++ source.cpp -lglfw3 -o program
Yet I get these errors.
C:\Users\murra\Coding\Manual>g++ source.cpp -lglfw3 -o proc
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x17): undefined reference to `glfwInit'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x56): undefined reference to `glfwCreateWindow'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x64): undefined reference to `glfwTerminate'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x76): undefined reference to `glfwMakeContextCurrent'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x81): undefined reference to `glfwWindowShouldClose'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x96): undefined reference to `_imp__glClear#4'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0xa6): undefined reference to `glfwSwapBuffers'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0xab): undefined reference to `glfwPollEvents'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0xb2): undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
I'm quite new to this stuff so help would be appreciated.
When you link a third party lib, you need to "tell" the compiler the location of the headers as well as the location of the libraries.
Typically, one would use -I<header_dir> and -L<lib_dir> in addition to -l<lib_name>
You may also reduce this to 2 steps by using -I<header_dir> and -L<full_lib_path>
If linking causes errors, IMO there are 3 major reasons.
Incorrect path
Mixing shared lib and static lib flags (example - -MT and -MD on VS)
Additional library dependencies that you have not included.
In your case, the missing symbols seem to be from additional libs. Try -lglfw3 -lglew32 -lopengl32 -lgdi32 as additional options. Obviously these need to be on your computer.
This question already has an answer here:
glfw Errors with glfwWindowHint
(1 answer)
Closed 2 years ago.
I am very new to c++ and I am trying to use opengl with glfw. I am using ubuntu 18 on wsl. I have GL and GLFW on my include directory and I have included it in my code.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
using namespace glm;
int main(){
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Hi\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
}
I am using g++ to compile the program. When I use g++ -c main.cpp it compiles but throws a binary error when I try to run the program and when I use g++ main.cpp -IGL -IGLFW -Iglm I get that error. Am I missing something?
Error:
/usr/bin/ld: /tmp/cco9YYTw.o: in function `main':
main.cpp:(.text+0x17): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x26): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x35): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x44): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x53): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x74): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.cpp:(.text+0xa4): undefined reference to `glfwTerminate'
/usr/bin/ld: main.cpp:(.text+0xb7): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: main.cpp:(.text+0xbd): undefined reference to `glewExperimental'
/usr/bin/ld: main.cpp:(.text+0xc3): undefined reference to `glewInit'
collect2: error: ld returned 1 exit status
To build your example you should install the following dependencies on your Ubuntu with apt-get
libglm-dev, libglfw-dev, libglew-dev, cmake
I've used CMake to build your file
Put the following CMakeLists.txt file in your folder where .cpp file located (I copied your code into a file named glfw_sample.cpp)
cmake_minimum_required(VERSION 3.10)
project(glfw_sample)
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
add_executable(${PROJECT_NAME} glfw_sample.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${GLFW_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE GLEW::GLEW ${GLFW_LIBRARIES})
Then execute in a shell
cmake -S. -Bbuild
cmake --build build
If no errors occurred on both steps you'll find glfw_sample executable in the build folder.
I use the following links to build your code:
https://www.glfw.org/docs/3.0/build.html
https://cmake.org/cmake/help/latest/module/FindGLEW.html
A very quick google search might help: glfw-errors-with-glfwwindowhint
Link to -lglfw3 not -lglfw - if this is correct, please vote up the linked-to-answer and not this one
To cover the location of the library, from compile-glfw-on-ubuntu-and-fix-libglfw-so-cannot-open-error
This is because the GLFW installer puts libglfw.so in /usr/local/lib
instead of /usr/lib like Ubuntu expects. But /usr/local/lib is a fine
place to put shared libraries for most of the Unix world, so let’s
just tell Ubuntu to look there when linking our program. You’ll want
to edit /etc/ld.so.conf and add the line “/usr/local/lib” to the file,
then save. Afterwards you should run ldconfig as root. Something like
this:
sudo vim /etc/ld.so.conf
# Add the path and save - then try to build again
UPDATE I switched to the meson build system. Everything is working fine now!
I am very new to using C++, OpenGl, and Gnome Builder. I have a very very basic foundation with C++ and I know how to link header files and libraries in CodeLite, however after messing around Gnome Builder I want to make the switch. I haven't found any beginner friendly tutorials on using Builder. I am just lost as to how I should link external libraries in Builder. Do I just manually edit the Makefile or is there a setting somewhere that will automate the makefile process with automake? Am I wrong in assuming that this is a makefile problem? Apologies if this is a very novice question.
I am using Ubuntu. I am getting the error "undefined reference to ..." for all the glfw and glew variables and headers. After installing libraries with apt, I have my libraries installed in usr/lib/x86-64-linux-gnu, headers in usr/include.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
int main ()
{
glewExperimental = true;
if (!glfwInit() )
{
fprintf(stderr, "Failed to initialize GLFW \n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
if ( window == NULL )
{
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. \n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to Initialize GLEW. \n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
while ( glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
return 0;
}
When trying to build I get this error output~
g++ -o practice -Wall -ggdb -fno-omit-frame-pointer -O2 practice.cpp
/usr/bin/ld: /tmp/ccLx11Ky.o: in function main':
/home/joe/Projects/practice/practice.cpp:30: undefined reference toglewExperimental'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:31: undefined reference to glfwInit'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:36: undefined reference toglfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:37: undefined reference to glfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:38: undefined reference toglfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:39: undefined reference to glfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:40: undefined reference toglfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:43: undefined reference to glfwCreateWindow'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:50: undefined reference toglfwMakeContextCurrent'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:51: undefined reference to glewExperimental'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:52: undefined reference toglewInit'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:58: undefined reference to glfwSetInputMode'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:65: undefined reference toglfwWindowShouldClose'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:61: undefined reference to glClear'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:62: undefined reference toglfwSwapBuffers'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:63: undefined reference to glfwPollEvents'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:65: undefined reference toglfwGetKey'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:47: undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make: *** [Makefile:8: practice] Error 1
My default Makefile looks as follows~
all: practice
WARNINGS = -Wall
DEBUG = -ggdb -fno-omit-frame-pointer
OPTIMIZE = -O2
practice: Makefile practice.cpp
$(CXX) -o $# $(WARNINGS) $(DEBUG) $(OPTIMIZE) practice.cpp
clean:
rm -f practice
# Builder will call this to install the application before running.
install:
echo "Installing is not supported"
# Builder uses this target to run your application.
run:
./practice
You need to customize the build command (practice) to include the required libraries. Conside using pkg-config --libs glew (or pkg-config --libs -static glew) to find which libraries are needed, and pkg-config --cflags for command line flags, if any.
Most likely:
Add '-lglfw3' to the 'practice' build command,
If you use static libs, add '-DGLEW_STATIC'
See glfw Errors with glfwWindowHint, and GLEW Linker Errors (undefined reference to `__glewBindVertexArray')
I'm a C++ beginner with some experience in Java trying to set up a project using GLM, GLFW, and Vulkan on Windows. This is going to be my first time getting my hands dirty in a lower-level language like C++. I'm having a lot of trouble getting the compiler to link the Vulkan and GLFW libraries to my project. I'm following the tutorial here at vulkan-tutorial.org to get started. Here's the code in main.cpp:
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <iostream>
int main() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::cout << extensionCount << " extensions supported\n";
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Here are the commands being used to compile it:
g++ -std=c++11 -fexceptions -g -IC:/glfw-3.2.1/include -IC:/glm-0.9.9.1/glm -IC:/VulkanSDK/1.1.82.1/Include -IC:/glfw-3.2.1/include -c "src/main.cpp"
g++ -LC:/glfw-3.2.1/lib-mingw-w64 -LC:/VulkanSDK/1.1.82.1/Lib -o VulkanTest.exe main.o -lglfw3 -lvulkan-1
The first command compiles the .cpp into a .o successfully, but the second command gives me errors from the linker. Every single reference I made to a member from Vulkan or GLFW is undefined. (Path has been shortened for easier reading)
[omitted]/src/main.cpp:12: undefined reference to `glfwInit'
[omitted]/src/main.cpp:14: undefined reference to `glfwWindowHint'
[omitted]/src/main.cpp:15: undefined reference to `glfwCreateWindow'
[omitted]/src/main.cpp:18: undefined reference to `vkEnumerateInstanceExtensionProperties#12'
[omitted]/src/main.cpp:26: undefined reference to `glfwWindowShouldClose'
[omitted]/src/main.cpp:27: undefined reference to `glfwPollEvents'
[omitted]/src/main.cpp:30: undefined reference to `glfwDestroyWindow'
[omitted]/src/main.cpp:32: undefined reference to `glfwTerminate'
It seems like the linker can't find the library files I provided with -L and -l, but if I change -lglfw3 to -llibglfw3.a or -lglwf3.dll, I get this:
[omitted]/mingw32/bin/ld.exe: cannot find -llibglfw3.a
or
[omitted]/mingw32/bin/ld.exe: cannot find -lglfw3.dll
Leading me to think that the linker DID find the libraries at first, since it didn't complain about being unable to find the library - but why can't it find sources for the references to GLFW / Vulkan functions? I have no idea what's happening. Is it finding the library files?
I'm using GLFW 3.2.1, Vulkan SDK 1.1.82.1, MingW GCC version 6.3.0, and I'm running on Windows 10 Pro 64-bit.
Turns out my problem was rooted in using an old version of MingW. Originally I downloaded MingW from here, which was recommended by the guide on MingW's wiki. I updated to 8.1.0 from this site and the linker began throwing errors about undefined references to different functions, such as "__imp_CreateDCW" and "__imp_SwapBuffers".
I happened to recognize these as GDI functions from some other research I did. I added GDI32 to the libraries option in the linker and the build went through successfully.
Now my build commands are as follows:
g++ -std=c++11 -fexceptions -g -IC:/glfw-3.2.1/include -IC:/glm-0.9.9.1/glm -IC:/VulkanSDK/1.1.82.1/Include -c "src/main.cpp"
g++ -LC:/glfw-3.2.1/lib-mingw-w64 -LC:/VulkanSDK/1.1.82.1/Lib -o VulkanTest.exe main.o -lglfw3 -lvulkan-1 -lgdi32
I'm trying to create a SDL2 project with Eclipse Kepler and MinGW on Windows.
I already added SDL2 libs in MinGW (.a) in C:\MinGW\lib, SDL2 include in MinGW(C:\MinGW\include\SDL2) and I also added in projects properties -> C/C++ general -> paths and symbols -> librairies the following lines in that order :
mingw32
SDL2main
SDL2
Then I put '-mwindows' in MinGW C++ linker at the end of the line "Command line pattern"
I also added -Dmain=SDL_main for the entry point...
But the compiler gives me error :
main.cpp:7: undefined reference to `SDL_CreateWindow'
this is the code :
#include <SDL2/SDL.h>
int main(int, char**)
{
SDL_Window *pWindow = nullptr;
pFenetre = SDL_CreateWindow("Test SDL 2.0", 0, 0, 320, 240, SDL_WINDOW_SHOWN);
if (!pWindow)
{
return -1;
}
SDL_DestroyWindow(pWindow);
return 0;
}
And this is the build console :
Info: Internal Builder is used for build
g++ "-LC:\\MinGW\\lib" -o Test.exe main.o -lmingw32 -lSDL2main -lSDL2 -mwindows
main.o: In function `SDL_main':
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:7: undefined reference to `SDL_CreateWindow'
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:13: undefined reference to `SDL_DestroyWindow'
C:\MinGW\lib/libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain#16'
collect2.exe: erreur: ld a retourné 1 code d'état d'exécution
what's wrong ?
Make sure you're using the right version of the library. You can't mix 64-bit import libraries with 32-bit compiler. For the SDL2 library you downloaded(SDL2-devel-2.0.0-mingw.tar.gz) it comes with both 32-bit and 64-bit. i686-w64-mingw32 is 32-bit and x86_64-w64-mingw32 is for 64-bit.