I've searched on google and haven't been able to come up with a solution.
I would like to compile some OpenGL programming using GCC. In the GL folder in GCC I have the following headers:
gl.h
glext.h
glu.h
Then in my system32 file I have the following .dll
opengl32.dll
glu32.dll
glut32.dll
If I wanted to write a simple OpenGL "Hello World" and link and compile with GCC, what is the correct process?
I'm attempting to use this code:
#include <GL/gl.h>
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitWindowSize(512,512);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("The glut hello world program");
glutDisplayFunc(display);
glClearColor(0.0, 0.0, 0.0, 1.0);
glutMainLoop(); // Infinite event loop
return 0;
}
I am using WindowsXP and GCC version 3.4.5. Thank you in advance for the help.
You probably want to run gcc like this:
gcc -g -Wall hello_gl.c -lopengl32 -lglu32 -lfreeglut
Unfortunately GLUT does not come preinstalled on Windows.
GLUT is a library that takes care of the (platform specific) job of creating a window and graphic context for you. Many OpenGL samples use it.
The official GLUT port to Win32 is available here but it's a bit dated.
I suggest you use the compatible freeglut library instead. You can use this tutorial for setting up freeglut with Mingw32
Related
I'm trying to learn how to use libraries when writing C code on my mac (not using Xcode). My understanding is that on macs, there is the Library/Frameworks folder where you can put common libraries that can be shared across different projects.
My goal at this point is to use the SDL library to open basic window and do nothing else, but I can't figure out how to utilize libraries on my mac. So to be very specific, I just want to have one file of application code that I have written called main.c and it will have this boilerplate SDL code:
#include "SDL2/SDL.h" // OR #include "SDL.h" (Not sure how the difference in path works)
int SCREEN_HEIGHT = 800;
int SCREEN_WIDTH = 600;
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("SDL Game", 0, 0,
SCREEN_HEIGHT, SCREEN_WIDTH, SDL_WINDOW_HIDDEN);
SDL_ShowWindow(window);
SDL_Event event;
int running = 1;
while(running) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
running = 0;
}
}
SDL_Delay( 32 );
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I downloaded the development library for mac from the SDL website (https://www.libsdl.org/download-2.0.php) and moved the download to the /Library/Frameworks folder on my machine, just as SDL instructed. However I don't know what to do in my Makefile for the library to be included and linked and then compiled with my main.c file.
Here are some specifics of my laptop/compiler:
MacOS Bug Sur
Version 11.3.1
MacBook Pro (16-inch, 2019)
Processor: 2.3 GHz 8-Core Intel Core i9
Memory: 32 GB 2667 MHz DDR4
Startup Disk: Macintosh HD
Graphics: AMD Radeon Pro 5500M 8 GB
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: x86_64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Can someone show me what an example of a very simple Makefile would look like to accomplish this goal??
If you work from the command line or with a properly configured IDE, you link (In GCC) with the -l(library name) flag. The library you want to link needs to be in some directory like /usr/lib, /usr/local/lib or some Libraries folder MacOS might have. If it's not there, then use the -L(path/to/lib/directory) to tell GCC where to find it, and then use -l(library name) flag again.
The library name should start with lib and either end with a .a or .so suffix. So if you wish to link to your own SDL2 library build:
gcc source.c -o mygame -Llibraries/ -lSDL2
Assuming that the library is under libraries/libSDL2.so (Numbers in the end don't matter).
Hi i need to convert an image to b&w with C.
I'm on MXLinux.
And my idea is to use opencv api. I search on web and i found this way to gain b&w foto.
The problem is only when i try to compile it with gcc. It can't found the library opencv, how i can link (permanently not only local) this library?
#include <string.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv){
//read and convert to greyscale
IplImage* im_rgb = cvLoadImage("home.jpg");
IplImage* im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);
//to black and white
IplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
cvThreshold(im_gray,im_bw,128,255,CV_THRESH_BINARY | CV_THRESH_OTSU);
//save the image
cvSaveImage("image_bw.png",im_bw);
cvSaveImage("image_grey.png",im_gray);
}
The other question is, why doesn't gcc see automatically the library ? After the installation i see the /usr/local/bin and the library are on it!
-----UPDATE1----- 25-02-2021 -----
Yes, after OpenCV 3.x library C seems doesn't exist or maybe doesn't work.
Problem solved by install OpenCV 3.4.3.
Compile in this way:
gcc NameFile.cpp -o NameFile $ ( pkg-config --cflags --libs opencv )
I obtain an error (i think is caused by cvSaveImage) :
what(): OpenCV(3.4.3) /home/pi/opencv-3.4.3/modules/core/src/matrix_wrap.cpp: 800: error: (-215:Assertion failed) (flags & FIXED_TYPE) != 0 in function 'type'
--SOLVED--
I solved all problem by installing an other version of OpenCV, via web i found that the 3.4.3 version has a bug with matrix_wrap i really don't understand very well the problem but with a clean installation of opencv 3.0.0 all works fine.
I get only few error on cmake and make but all are easy to resolve by web.
Thanks to all.
I've compiled the following code with gcc on cygwin:
#include <cairo.h>
int
main (int argc, char *argv[])
{
cairo_surface_t *surface =
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
cairo_t *cr =
cairo_create (surface);
cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 32.0);
cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
cairo_move_to (cr, 10.0, 50.0);
cairo_show_text (cr, "Hello, world");
cairo_destroy (cr);
cairo_surface_write_to_png (surface, "hello.png");
cairo_surface_destroy (surface);
return 0;
}
The code is from the official cairo homepage, so I assume it should run.
compiling with this command:
i686-w64-mingw32-gcc kairo.c -o kairo.exe -I /usr/include/cairo/ -L /usr/lib -lcairo
It compiles without a problem, but trying to open the .exe (regularly in the windows file explorer) throws this error:
"The program could not be started, since cygXau-6.dll is missing on your PC. Reinstall the program, to fix the problem."
(roughly translated from german)
But that dll seems to be cygwin-specific and I'd like to not have my .exe depend on cygwin.
I'm new to C (coming from a webdev background) and the whole concept of linking is foreign to me, but I understand that I'll likely need to include the DLLs somewhere near the executable, and maybe link to it with the -I flag.
I don't even know if this is the right approach when building cross-platform C, but my first goal would be to simply get this snippet running under windows.
It does work when compiling with "regular" gcc and running it as kairo.o in cygwin - it produces the desired result, a png with "Hello, World".
The theory that I've compiled some sort of Hybrid seems to confirm itself - I've checked the exe with dependencywalker and got the following output.
Error: At least one required implicit or forwarded dependency was not found.
Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.
Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.
So I guess I need to find out where I can get the mingw-binaries and header files for cairo and link to those. I just have no idea where to get them.
according to apt-cyg mingw-i686-cairo is installed - I linked to the files in the header file and it compiled as well, but now executing the .exe gives me another error:
the application could not be started (0x000007b).
the following command was used to compile this time:
i686-w64-mingw32-gcc kairo.c -o kairo.exe -I /usr/i686-w64-mingw32/sys-root/mingw/include/cairo/ -L /usr/i686-w64-mingw32/sys-root/mingw/bin/ -lcairo
So I've done some more research and it seems I might still have some mixup between files compiled for 32 and 64 bits. Might have to do with the dlls
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 working on Windows XP I have portable version of Eclipse Galileo, but I didn't find there glut so I decided to add it using this link I made all steps and and now I'm trying to compile this code
#include "GL/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
///////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
// Flush drawing commands
glFlush();
}
///////////////////////////////////////////////////////////
// Setup the rendering state
void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
///////////////////////////////////////////////////////////
// Main program entry point
void main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("Simple");
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
}
and I have this errors
Simple.o: In function `RenderScene':
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:16: undefined reference to `_imp__glClear'
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:20: undefined reference to `_imp__glFlush'
Simple.o: In function `SetupRC':
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:27: undefined reference to `_imp__glClearColor'
Simple.o: In function `main':
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:34: undefined reference to `glutInit'
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:35: undefined reference to `glutInitDisplayMode'
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:36: undefined reference to `glutInitWindowSize'
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:37: undefined reference to `glutCreateWindow'
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:38: undefined reference to `glutDisplayFunc'
C:/Documents and Settings/Administrator/Desktop/workspace/open/Debug/../Simple.c:42: undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status
please can somebody help me, thanks in advance
It looks like you're not linking the OpenGL, GLU or GLUT libraries. You need to tell Eclipse to link them, and you need to tell it the directories where they're stored (at least with most IDEs, the two operations are separate from each other).
If memory serves, openGL itself will be opengl32.lib. If it installed reasonably to start with, the IDE will probably already know the location for this library (i.e., it's a normal part of Windows, and the library will be along with the other normal Windows libraries). The glu functions are in glu32.lib, which should be in the same place.
Glut will normally be in a file named glut32.lib. Assuming you installed Glut in the root directory of your C drive, it would typically be at "C:\glut-3.7\lib\glut".
It seems that you have missed to add the libraries and the linker cannot find them. Make sure that the correct libraries are specified in the Libraries dialog. I don't have a Eclipse installation here but this dialog should be somewhere around `Right click project -> Properties -> Libraries/C++ Linker"