I am trying to learn the glfw library in C. I have installed the glfw pre-compiled binaries from their official website. I have moved the 32-bit libs from the pre-compiled binaries zip file to my mingw-gcc's lib folder and the include files from the zip file to the mingw-gcc's include folder. I am trying to make the a window using the library and c. I am using vscode as my editor. This is my code:
#include <stdio.h>
#include <windows.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main() {
printf("Hello World!\n"); //test line
GLFWwindow * window;
if (!glfwInit) {
printf("glfw3 initialization failed");
return -1;
} else {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
printf("Could not create window.");
glfwTerminate();
return -1;
} else {
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
}
}
I am using the following command to compile my code:
gcc main.c -lglfw3dll -lopengl32
The program compiles without any errors but when I run the compiled .exe, nothing happens. I even tried to print hello world before the code runs, but even that doesn't print. What is happening and how do I fix it?
Edit: I fixed the problem by adding the glfw libraries to my system path. now it is working fine
You are doing:
if (!glfwInit)
This just checks to see if the address of glfwInit is non-zero.
It does not call/invoke it.
To invoke it, you want:
if (! glfwInit())
UPDATE:
the code still is doing nothing. it is still not printing the test hello world –
The Parallax
The "Hello World" in your glfwCreateWindow call just sets the window title.
The first question is: Does a window come up on the screen? That is, what, exactly, do you mean by the code is doing nothing ...?
I don't see any drawing calls in your code.
Your code is similar to the example code from: https://www.glfw.org/documentation.html#example-code with some small exceptions but not exactly the same.
In the link, it has:
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
at the top of the loop.
AFAICT, this will clear the buffer to the background color.
You may need/want to add some drawing calls to put up a rectangle (e.g.) after the glClear call.
Otherwise, you may get the window to come up, but it won't have anything in it.
Here is the step by step tutorial: https://www.glfw.org/docs/3.3/quick.html
It has a more complete/extensive example code. I'd use the code in the two links as a model.
I fixed the problem by adding the glfw libraries to my system path. now it is working fine
Related
I haven't found any extension, or tutorial to make and execute a C console application in the VSCode Terminal.
A simple program like
int main()
{
printf("Hello World!");
return 0;
}
And have the output in the VSCode Terminal.
Does someone know how to realize this? And/or are there solutions?
Thanks in advance
Regards
There actually is an extension for c/c++ on VSCode:
When you click the arrow in the top right (to run the file) you will be asked which compiler you want to use. In our case we can use the gcc compiler:
Then you can paste your code into a .c file and run it with the compiler. It should automatically also execute the binary and print your output into the debug-console:
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Hello World!
You even have a debugger, if you set certain breakpoints!
Extra:
Make sure that you have the correct OS set in the bottom right (in the status bar), so your c code compiles for your machine specifically.
I'm a something of a beginner in programming. Here's a snippet from a C Program I wrote for Windows, trying to use NCurses library, compiled with mingw32.
char NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);
char *dllDirectory = malloc(strlen(NPath) + strlen("\\dlls") + 1);
strcpy(dllDirectory, NPath);
strcat(dllDirectory, "\\dlls");
SetDllDirectoryA(dllDirectory);
HINSTANCE hGetProcIDDLL = LoadLibrary("libncursesw6.dll");
if (!hGetProcIDDLL) {
printf("libncursesw6.dll could not be loaded!");
return -1;
} else {
printf("loaded libncursesw6.dll\n");
}
I tried to load the libncursesw6.dll from the dlls subfolder, from the same folder of the executable.
The above code works perfectly and displays, the dll is loaded.
But when I try to use use the library functions, I get an error box telling me libncursesw6.dll was not found. The program then works fine if is place the dll with the executable (which is what I'm trying to avoid).
When the following lines are added after the previous snippet, I get a run time error.
initscr();
addstr("Hello World");
refresh();
getch();
endwin();
I've included the necessary header files and got no compilation errors or warnings.
Am I doing something wrong??
The problem is, that you load the library after you have started the program, but if you use the functions in the code, the runtime linker, that tries to resolve unresolved symbols, needs to find the library before it passes control to the actual program.
So, you cannot do it this way. It would be possible to not use the ncurses functions directly, but to define a bunch of function pointers and resolve the symbols you need manually with GetProcAddress, but this is quite cumbersome.
For example like this:
void (*initscr)() = GetProcAddress(hGetProcIDDLL, "initscr");
Another possibility is, to link the ncurses library statically, so it isn't needed at runtime.
I'm far from being a Windows programmer, not even a windows user, but now I should cross-compile an application to Windows using mingw32. My problem: console output does not work. It's a GUI application, compiled with -mwindows. I tried to use AllocConsole() which indeed produces a console window, but still no output from printf() and friends. I also tried to use freopen() on name CONOUT$ also with/without using setvbuf(stdout, NULL, _IONBF, 0). These were mentioned by several resources that can help, but still no output at all in the console window. Now I feel lost as I can't imagine what I can do more, even MSDN pages seems to agree that I do the right thing (if I read them correctly ...), but still no output :(
#include <stdio.h>
#include <windows.h>
int main ( void )
{
FreeConsole(); // just to be sure we had no console before
AllocConsole();
// SetConsoleTitle("Test console");
freopen("CONOUT$", "w", stdout);
setvbuf(stdout, NULL, _IONBF, 0);
printf("Hello, world!\n");
sleep(10);
return 0;
}
Simply compiled with (on Linux): i686-w64-mingw32-gcc -o test.exe test.c -mwindows
Please note, that the problem exists in a more complex software. In this example I may choose to use console application instead of GUI then, but what I have problem with, is an SDL2 application otherwise and can't be console application, just I need console too. The code above is only a test case. I also tried to comment call of freopen() and setvbuf() out, modify their order, etc, it didn't helped.
I'm using GLEW version 1.10.0 with MinGW (Through the CodeBlocks IDE), running on Windows 8. I downloaded the Windows binaries from the GLEW website, and have been linking to the libraries included with that build.
I have a linking problem that I just can't seem to find an answer to. I have followed the installation on the GLEW home page. I have referenced the linker to the glew32.lib, as well as the other required libs such as opengl32 and glu32.
Unfortunately, compiling this code (I'm also using GLFW for context/window management):
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define TRUE 1
#define FALSE 0
int main()
{
GLFWwindow *window;
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3.0);
window = glfwCreateWindow(640, 480, "Hello World!", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental=TRUE;
GLenum err = glewInit();
if (err!=GLEW_OK)
fprintf(stderr, "Could not initialize GLEW!");
printf("%s\n", glGetString(GL_VERSION));
while (!glfwWindowShouldClose(window))
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I get the error:
*undefined reference to imp_glewExperimental*
Even though I'm new to C, as far as I understand, this means that I'm referring to something which has no definition, which generally means the library is missing. In this case though, I have included the library, and I got no errors whatsoever about the other GLEW references I make, such as glewInit, which I feel it should also complain about should it be a problem of missing libraries.
I've tried to search the web but I simply haven't found anything on this problem.
Anyone got any ideas? :)
Thank you all very much for your time. It is much appriciated.
It seems I have solved the problem. For anyone who'd like to know, it appears the problem was the pre-build Windows binaries from the GLEW website, because they originate from Visual Studio (they are .lib files). I was using MinGW to do compilation. As soon as I tried to compile GLEW myself using MinGW to create a .a archive, it worked.
There's already a great answer here on stackoverflow on how to compile GLEW for MinGW, and can be found right here.
try to put #define GLEW_STATIC on the first line:
#define GLEW_STATIC
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
and put -lglew32s first in the link libraries table.
codeblocks:
project > build options... > linker settings > add glew32s
then click the up arrow until it get first
i'm trying to make a program in which I can play music i.e mp3 files.I'm trying to do this by using the winmm library.At first when i tried linking it,the compiler gave errors from which i realized that the program hadn't been linked properly with the library but then i added the library file in the linker settings and now the program executes fine(no errors-suggesting that it has been linked properly) but no music is played.I can't figure out what the problem is.I'm currently using codeblocks,which uses gcc compiler.Can anyone explain what the problem is and why the music isn't playing? I'd be grateful if anyone can help me out! :)
my code(it simply prints the text but no music is played):
#include <stdio.h>
#pragma comment (lib, "winmm.a")
#include <windows.h>
#include <mmsystem.h>
int main()
{
printf("Hello world!\n");
mciSendString("play song.mp3",NULL,NULL,NULL);
printf("\nY");
mciSendString("pause song.mp3",NULL,NULL,NULL);
mciSendString("close song.mp3",NULL,NULL,NULL);
printf("\ndone");
return 0;
}
MCI commands return immediately. This means you immediately pause and close the mp3 hardly before playing started. Looking at the documentation you have to use the Wait Flag:
mciSendString("play song.mp3 wait",NULL,NULL,NULL);