GLAD link is not providing needed functions - c++

TLDR: The GLAD header file won't let me use the openGL Commands and I don't know exactly why.
Here's a breakdown:
I'm on a Windows 10 Laptop.
I'm coding in C++.
I'm compiling with MinGW's g++ tool.
I'm using GLFW and GLAD.
My file layout is something like this:
OpenGLTest
include
glad
glad.h
GLFW
glfw3.h
glfw3native.h
KHR
khrplatform.h
glad.c
glfw3.dll
GraphicsTest.cpp(Main File)
libglfw3dll.a(I have forgotten what this does, it's a remnant from older attempts at openGL. Relevant?)
Makefile(a single command: "g++ GraphicsTest.cpp -g -L glad.c glfw3.dll")
As far as I can tell, the program will compile and run flawlessly if any and all commands from GLAD and openGL are commented out. All it then does is make a small window.
If said lines are not commented out, the compiler will throw a slew of errors, all following this form:
D:\Documents\Programming\C++\Programs\OpenGLTest/GraphicsTest.cpp:23: undefined reference to `gladLoadGL'
...with gladLoadGL being replaced with the relevant function.
The file itself reads thusly:
#include<iostream>
#include<glad\glad.h>
#include<GLFW\glfw3.h>
static void whatIsGoingOnSeriouslyGuysHelp(int id,const char* desc)
{
std::cout<<desc;
}
int main()
{
glfwSetErrorCallback(&whatIsGoingOnSeriouslyGuysHelp);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(100,100,"TEST",NULL,NULL);
if (window == NULL)
{
std::cout<<"Window Creation Error";
}
glfwMakeContextCurrent(window);
gladLoadGL();
//glViewport(0,0,100,100);
while (!glfwWindowShouldClose(window))
{
//std::cout<<"?";
glfwPollEvents();
glfwSwapBuffers(window);
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT);
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
I have tried re-installing GLAD, but that didn't do anything. I haven't tried re-installing GLFW, but I don't think that's the problem.
Correct any misconceptions I have or errors I am making.

You need to include glad.c in your compile command as follows: (you had a -L before it which tells gcc to treat glad.c as a directory for libraries)
g++ GraphicsTest.cpp glad.c -g glfw3.dll
, and instead of gladLoadGL, you should use the GLFW loader:
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
Finally, you should invest a tiny bit of time into a decent build system because compiling every file every time will get real slow real fast. Here is the absolute minimal Makefile to get you started:
graphicstest: graphicstest.o glad.o
g++ -g -o $# $^ glfw3.dll

Related

Cannot link with glfw3.lib c++

I am trying to compile a c++ file on command line with g++
i have this file
#include <iostream>
#include "C:\Users\Shaurya\Documents\Opengl\Dependencies\GLFW\include\GLFW\glfw3.h"
using namespace std;
int main(){
GLFWwindow* window;
if(!glfwInit()){
cout << "Window not initialized";
return -1;
}
window = glfwCreateWindow(600,600,"OpenGL", NULL, NULL);
if(!window){
cout << "Window Not created";
glfwTerminate();
}
glfwMakeContextCurrent(window);
while(glfwWindowShouldClose(window)){
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I run this command
g++ -o Test main.cpp -L <fullpath>\Dependencies\GLFW\lib-vc2019 -lglfw3.lib
but this throws a gigantic error
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: mode i386pe
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o
C:\Users\Shaurya\AppData\Local\Temp\ccxDP2Km.o
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libstdc++.dll.a)d004332.o
.
.
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libgcc_s.a)d000122.o
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libgcc.a)_chkstk_ms.o
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libgcc.a)_ctors.o
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingwex.a)fesetenv.o
.
.
.
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmoldname.a)dagwbt.o
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtend.o
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lglfw3.lib
collect2.exe: error: ld returned 1 exit status
It took me alot of time to realize that the -l is only looking for .o files
So how do i link with .lib files? i got the .lib from glfw.org binaries.
I dont want to use Visual Studio 2019 to do this because my laptop has a hard time running that.
So i solved this. First i was using the wrong libraries. I was provided with multiple versions of Libraries and i was using VC version. But when i used MinGW Version , it worked.
Moreover , i was using relative paths without typing ./ before them.

SDL_Init_Everything fails in C

I am trying to create a game in C using SDL. After some time of debugging I found an error at SDL_Init:
#include <SDL2/SDL_ttf.h>
int main(void) {
printf("Init attempt\n");
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fprintf(stderr, "Failed at SDL init: %s\n", SDL_GetError());
fflush(stderr);
fflush(stdout);
exit(EXIT_FAILURE);
}
printf("Init success\n");
return EXIT_SUCCESS;
}
The output is the following:
Init attempt
Somehow the fprintf inside the write function is getting executed.
The gcc command I used is:
gcc -Wextra -Wall -pedantic program.c -lpthread -lSDL2 -lSDL2_ttf
Note: If I use g++, everything works fine. However, I must use gcc.
Is there a way to fix the error?
using the official doc (https://wiki.libsdl.org/SDL_Init) there's a flag that might conflict with your setup: SDL_INIT_HAPTIC
i didn't get any error or crash so im not sure, but it is possible that this flag might be included in the EVERYTHING one, and if so:
It’s disabled by default if you don’t use ./configure; make style builds.
Otherwise it’ll happily build if your headers are new enough.
(https://discourse.libsdl.org/t/sdl2-sdl-init-sdl-init-everything-failing/19250)
did you try setting up by flags you only need, like SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO and see if this works?

Undefined references to GLFW / Vulkan although libraries seem to be linked

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

undefined reference to glfwSetErrorCallback

I'm trying to use the GLFW library, but am having difficulty with compiling a simple program. I went to the GLFW website and download the latest release, then using "How to build & install GLFW 3 and use it in a Linux project" I built and installed it.
Here's my code:
#include <GLFW/glfw3.h>
#include <iostream>
using std::cout;
using std::endl;
void GLFW_error(int error, const char* description)
{
fputs(description, stderr);
}
void run()
{
cout << "pooch" << endl;
}
int main()
{
glfwSetErrorCallback(GLFW_error);
if (!glfwInit()) exit(EXIT_FAILURE);
run();
glfwTerminate();
return 0;
}
Using the command line:
bulletbill22#ROBOTRON ~/Desktop $ g++ -std=c++11 -lglfw source.cpp
yields
source.cpp:function main: error: undefined reference to 'glfwSetErrorCallback'
glfwSetErrorCallback is taken from their tutorial for "Setting an error callback".
Inclusion of -glfw3 results in /usr/bin/ld: error: cannot find -lglfw3
Even though everything seemed to be installed correctly, I suspect the problem may lie somewhere with the installation of the GLFW library because I'm not used to CMake and don't entirely understand how it works. I'm frustrated because the answer must be simple, but I'm not sure which keywords are really relevant when googling the problem; mostly the results are people who were incorrectly compiling with CMake, which I'm not compiling with in this case.
It seems that the directories for the glfw3.h header and libglfw3.so (and/or libglfw3.a) library are not in the default path.
You can check with by adding the -v option to the g++ options. Locate the directory where the glfw3.h header is found - call this $GLFW_INCDIR - it typically ends with .../GLFW. Locate the directory where the library is found - call this $GLFW_LIBDIR. Try:
g++ -std=c++11 -I$GLFW_INCDIR source.cpp -o pooch -L$GLFW_LIBDIR -lglfw3
If all the library dependencies are satisfied, this hopefully results in a program called pooch.
One other thing: GLFW3 is a C library, and the callback function arguments are expected to be C functions. So your callback should have 'C' linkage, i.e.,
extern "C" void GLFW_error(int error, const char* description) ...
Also, if you're having trouble with cmake, you may have ccmake installed. Try ccmake . in the top-level directory of the GLFW3 package for 'interactive' configuration.

Problem with build SFML project with mingw

I want to build simple project:
////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#define SFML_DYNAMIC
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
// Create the main window
sf::Window App(sf::VideoMode(800, 600, 32), "SFML Window");
// Start main loop
bool Running = true;
while (Running)
{
App.Display();
}
return EXIT_SUCCESS;
}
I build it with:
g++ main.cpp -I./include -L./lib -o main -lsfml-system -lsfml-window -static-libgcc .
Everything compile without erros, but when I run it looks:
I try to build with:
-lopengl32 -lglu3s
Without -static-libgcc and #SFML_DYNAMICS
A lot of combinations, but I get the same result: black command window instead of normal window with graphics.
I use SFML 1.6 , and gcc 4.5.2 ( I have the same problem on 3... version :/ )
Anyone know what I do wrong ? Or how to compile it ? I know i can try visual studio, but I want to make it with gcc.
I've just tried this - I used MinGW under Windows XP, and I get a window pop up. This looks normal to me.
Try some more examples from the tutorials.