Starting off with OpenGL under Cygwin - opengl

Is it possible to compile and run OpenGL programs from under Cygwin? If yes, how?

It is possible to compile and run OpenGL programs under Cygwin. I illustrate the basic steps here:
I assume you know OpenGL programming. If not, get the Red Book (The OpenGL Programming Guide). It is mandatory reading for OpenGL anyway.
I assume you have Cygwin installed. If not, visit cygwin.com and install it.
To compile and run OpenGL programs, you need the Cygwin package named opengl. In the Cygwin installer, it can be found under the Graphics section. Please install this package.
Write a simple OpenGL program, say ogl.c.
Compile the program using the flags -lglut32 -lglu32 -lopengl32. (This links your program with the GLUT, GLU and OpenGL libraries. An OpenGL program might typically use functions from all the 3 of them.) For example:
$ gcc ogl.c -lglut32 -lglu32 -lopengl32
Run the program. It's as simple as that!

If the above doesn't work (and it didn't for me), try the following (which did!)
gcc ogl.c -lglut -lglu -lgl

I do not normally post answers this long, but this one is worth it.
I will present a Windows 10 64-bit setup for Cygwin that uses the same libraries as Visual Studio. You will be able to use both development environments with the same code (same includes and libraries), so you can switch between the two as you see fit.
You need three libraries: OpenGL, GLEW, and GLFW.
OpenGL
Visual Studio: The following default locations are valid for current versions of Windows 10 and Visual Studio 2019.
OpenGL static library:
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\SDK\ScopeCPPSDK\vc15\lib\SDK\lib\opengl32.lib
OpenGL DLL:
C:\Windows\SysWOW64\opengl32.dll
The opengl32.lib library will need to be specified under the VS project Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies. The same applies for all other dynamic libraries under Visual Studio. I will not mention it again.
Cygwin:
OpenGL static library default location:
/lib/w32api/libopengl32.a
OpenGL dynamic library (uses the Windows DLL):
C:\Windows\SysWOW64\opengl32.dll
GLEW
Visual Studio: Download 32-bit/64-bit binaries from http://glew.sourceforge.net/ and install in a custom folder, say C:\OpenGL\glew-2.1.0. The same download works for both Visual Studio and Cygwin.
GLEW headers (to #include GL/glew.h):
C:\OpenGL\glew-2.1.0\include
GLEW static library:
C:\OpenGL\glew-2.1.0\lib\Release\x64\glew32.lib
GLEW DLL:
C:\OpenGL\glew-2.1.0\bin\Release\x64\glew32.dll
These can be specified in your VS project's Properties menu.
Cygwin: You can link against this library from Cygwin as-is, meaning you can specify its download directory for the INCS, LIBS, and LDLIBS variables in your Makefile as follows (consistent with the download directory specified above):
GLEW headers directory:
/cygdrive/c/OpenGL/glew-2.1.0/include
GLEW static library directory:
/cygdrive/c/OpenGL/glew-2.1.0/lib/Release/x64
GLEW dynamic library directory:
/cygdrive/c/OpenGL/glew-2.1.0/bin/Release/x64
With these values for INCS, LIBS, and LDLIBS respectively, you can then link using the UNIX naming conventions as shown in the complete Makefile, at the bottom of the post.
GLFW
This can be downloaded at https://www.glfw.org/download. For our 64-bit setup, you need the Windows 64-bit precompiled binaries. You can place it also in a custom folder, say C:\OpenGL\glfw-3.3.4.bin.WIN64. The same download works for both VS and Cygwin.
Visual Studio:
You can specify directly the download locations into your project Properties for headers (to #include GLFW/glfw3.h in your source code) and DLLs (to have VS link against these libraries), respectively.
Cygwin:
For Cygwin, GLFW is trickier, because you can no longer link against it directly from the download location. You need to:
(a) copy the headers, static, and dynamic libraries from the download locations:
C:\OpenGL\glfw-3.3.4.bin.WIN64\include\GLFW\*.h
C:\OpenGL\glfw-3.3.4.bin.WIN64\lib-mingw-w64\*.a
C:\OpenGL\glfw-3.3.4.bin.WIN64\lib-mingw-w64\*.dll
...into your toolchain's (MinGW's) respective locations:
GLFW headers (create the include directory):
/usr/x86_64-w64-mingw32/include/GLFW/*.h
GLFW static libraries:
/usr/x86_64-w64-mingw32/lib/*.a
GLFW dynamic libraries:
/usr/x86_64-w64-mingw32/bin/*.dll
(b) place the dynamic library location into your PATH environment variable, editable in your .bash_profile file in your home directory.
The Makefile for Cygwin is:
CC=/usr/bin/x86_64-w64-mingw32-c++.exe
OPTS=-std=c++11
DEBUG=-g
CFLAGS=-Wall -c ${DEBUG}
INCS= -I.\
-I/cygdrive/c/OpenGL/glew-2.1.0/include\
-I/cygdrive/c/cygwin64/usr/x86_64-w64-mingw32
LIBS= -L/usr/lib\
-L/cygdrive/c/OpenGL/glew-2.1.0/lib/Release/x64\
-L/cygdrive/c/cygwin64/usr/x86_64-w64-mingw32/lib
LDLIBS= -L/bin\
-L/cygdrive/c/OpenGL/glew-2.1.0/bin/Release/x64\
-L/cygdrive/c/cygwin64/usr/x86_64-w64-mingw32\bin
Program.o: Program.cpp
${CC} ${OPTS} ${INCS} -c $<
Program: Program.o
${CC} ${OPTS} ${LIBS} ${LDLIBS} Program.o -lopengl32 -lglew32 -lglew32.dll -lglfw3 -lgdi32 -luser32 -o Program
With this setup, you can use the same exact source code files in both VS and Cygwin. You can compile, link, and run Program.exe from its directory in Cygwin with:
$ make Program
$ ./Program.exe
You can run from VS a Cygwin-compiled program by opening the existing *.exe as an SLN project and running it using the IDE interface. Conversely, you can run the VS executable (created by VS in Project/Debug or Project/Release) directly from the Cygwin command line with the command above.
The includes are:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
No changes whatsoever will have to be made in the source code to switch back and forth b/w VS and Cygwin. Happy coding :-)

I remember doing this once with some success, a few years ago, basically trying to cross compile a small Linux OpenGL C++ program. I do recall problems with Windows OpenGL drivers being behind the times (due to MS's focus on DirectX). I had NVidia OpenGL and DirectX drivers installed on my Windows system, but cygwin/g++ seemed to want to only use the Microsoft OpenGL DLLs, many years old, which do not have the latest support for all the ARB extensions, like shader programs, etc. YMMV.

Related

How to link GLEW and GLFW and OpenGL to MingW's g++

I am doing a course on OpenGL.
So I have run my project in Visual Studio 2019 and it works.
What I needed to do in Visual Studio was:
Add GLEW's and GLFW's additional include directories. (when you download the libraries it is simply the location of the include file)
Add GLEW's and GLFW's additional library directories. (lib folder locations)
Add the additional dependencies. (opengl32.lib, glew32.lib, glfw3.lib)
Copy and paste GLEW.dll into the project files, next to main.cpp.
And I run my code and it works.
But I don't want to use Visual Studio 2019, I would much rather work in Notepad++ or VSCode, what I really want is to just make a project out of pure notepad and compile it with the new Windows Terminal.
My question:
How can I do all of the things I did in visual studio 2019 from Power Shell?
I heard that MingW comes with its own power shell or command prompt, I imagine I need to find it and give it some commands to link everything up before I start coding.
Where do I find MingW's power shell equivalent and what commands do I need to type?
I have been stuck on this for ages now and could not find any information about it online. (except for what -L and -l does, but these are not power shell commands)
The MingGW and mingw-w64
compilers are Windows commandline tools. They are Windows ports of (some of)
the GCC compilers. tdm-gcc is yet another choice.
Commandine tools for any operating
system don't need their own shell. You can run them in any shell you've got
on that operating system. On Windows today
you have a choice of at least PowerShell and the old CMD shell.
The MinGW project provides, as well as its GCC toolchain, a minimal unix-like
environment for Windows called MSYS, which includes a shell. You don't need
MSYS to run the compiler.
As long as the GCC tools can be located in
the value of the PATH environment variable that is operative in
the shell at compiletime, then you run the compiler at the shell prompt:
>gcc [options...]
>g++ [options...]
the same way it is run on any operating system. If you want to work with a GCC toolchain then the question:
How can I do all of the things I did in visual studio 2019 from Power Shell?
is simply the question, How do you run GCC? That's a question of
sweeping generality. You need to study relevant books
and documentation
Very sketchily, if you want to compile and link a C++ program that has source files main.cpp and other.cpp and depends on libraries
foo and bar that have their C++ APIs defined in header files and are implemented in DLLs,
you will do it with commands of the following form:
To compile the source files to object files:
>g++ -c -o main.obj main.cpp -I/path/to/foo/header/files -I/path/to/bar/header/files [any other compilation options....]
>g++ -c -o other.obj other.cpp -I/path/to/foo/header/files -I/path/to/bar/header/files [any other compilation options....]
To link the object files and libraries to make an excutable program:
>g++ -o prog main.obj other.obj -L/path/to/foo_dll -L/maybe/a/different/path/to/bar_dll -lfoo -lbar [any other linkage options...]
And if all that is successful then the program will be prog.exe and you can run it:
>prog
just like you ran g++, provided that foo.dll and bar.dll can be found at runtime by the OS loader's DLL search protocol
As I think you appreciate, in real life nobody builds programs by typing the all the
commands in a shell except for instructional purposes. They use a build system or an IDE to automate it. But it is true that building programs though the medium of a build system or IDE presents fewer difficulties if you do it with a basic grasp of how the tools behind it work.

Using BLAS, LAPACK, and ARPACK with MSYS2

I am working on Windows 10 64-bit using MSYS2 with the 64-bit toolchain. In the MSYS2 terminal I found and installed the following packages after searching for them with pacman -Ss,
mingw64/mingw-w64-x86_64-openblas 0.2.20-2
mingw64/mingw-w64-x86_64-lapack 3.6.1-1
mingw64/mingw-w64-x86_64-arpack 3.5.0-1
I took an example fortran source file from the Intel MKL library examples; I copied the dgelsx.f file to a folder in my msys home directory. I compile (in the mingw64 terminal) with
gfortran dgelsx.f -o dgelsx -llapack -lblas
it compiles without any complaint, but when I attempt to run it, I get the error
C:/msys64/home/k_chu/lapacktext/dgelsx.exe: error while loading shared libraries: ?: cannot open shared object file: No such file or directory
Why is this happening? I haven't attempted to deal with an arpack example yet until I get this working. Also if I do get all of this working, will it be possible to use the arpack libraries in Microsoft Visual Studio with the Intel ifort compiler? I tried copying the .dll files in /msys64/mingw64/bin into my MSV project folder but no luck, same with the .a and .dll.a files in the /msys64/mingw64/lib.
Please use dependency walker (http://www.dependencywalker.com) to locate the .dll, which the program cannot find on the path. Or compile with -static-libgcc or -static-libstdc++ to avoid dependencies at runtime.

How to make Armadillo work on Windows?

I cannot make Armadillo 4.3 work on Windows. The library armadillo/include is included and I run g++ "-LC:\\Armadillo\\BLAS_Lapack" -o1 -o test.exe test.o -llapack -lblas, then I get the following error message:
C:/Armadillo/include/armadillo_bits/blas_wrapper.hpp:183: undefined reference to `wrapper_ddot_'
test.o: In function `ZN4arma4blas4gemvIdEEvPKcPKiS5_PKT_S8_S5_S8_S5_S8_PS6_S5_':
C:/Armadillo/include/armadillo_bits/blas_wrapper.hpp:34: undefined reference to `wrapper_dgemv_'
test.o: In function `ZN4arma4blas4gemmIdEEvPKcS3_PKiS5_S5_PKT_S8_S5_S8_S5_S8_PS6_S5_':
C:/Armadillo/include/armadillo_bits/blas_wrapper.hpp:69: undefined reference to `wrapper_dgemm_'
If I run g++ -o1 -o test.exe test.o -llapack -lblas, I get
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -llapack
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lblas
I did uncomment the configuration file config.hpp according to the README file.
Does anybody know how to make Armadillo work? (I am using Eclipse CDT.)
It took a while to get this right, but it seems transparent once it works. I will explain step by step. (Make sure you have uncommented the correct lines as directed in the README of the config.hpp file)
The general command for compiling with blas/lapack (using the default provided with armadillo (current version 4.500.0) and does make it faster :)
g++ main.cpp -I C:{ARMADILLO_ROOT}\include -L C:{ARMADILLO_ROOT}\examples\lib_win64 -lblas_win64_MT -llapack_win64_MT
where each of the commands are as follows:
g++: GNU G++ Compiler (using MinGW 4.9.1 C++ from equationsolution.com)
main.cpp: My main file of the C++ program (I have an abstract and concrete class defining the Levenshtein algorithm)
-I C:{ARMADILLO_ROOT}\include: The GCC C++ Compiler Include Path (To include the Armadillo library) where ARMADILLO_ROOT is where you have decompressed and placed your armadillo files
-L C:{ARMADILLO_ROOT}\examples\lib_win64: The MinGW C++ Linker Library Link Path (To link the BLAS and LAPACK libraries) I have used the default libs provided with armadillo, I believe and according to the doc you may and should substitute these linear libraries in a production instance)
-lblas_win64_MT -llapack_win64_MT: Identify the libraries to be used (name must match, so you cannot put -lblas or -llapack UNLESS your files are named that way - by default in this armadillo version they are named blas_win64_MT lapack_win64_MT (win64 since I am using both a MinGW/C++ 64bit and a 64bit armadillo [they should match])
Following this logic, you may configure eclipse (using eclipse Luna R1 4.4.1 -should be the same procedure on other versions) with the following:
Right Click the Project and Select "Properties" item in the popup menu
Under "C/C++ Build" on the left menu Select "Settings"
Under "GCC C++ Compiler" click on "Includes (-I)" and add the include path C:{ARMADILLO_ROOT}\include (Screenshot1)
Under "MinGW C++ Linker" click on "Libraries" add each "Libraries (-l)" as named blas_win64_MT lapack_win64_MT. (Screenshot2)
Under the same "MinGW C++ Linker" click on "Libraries" add a "Library search path (-L)" C:{ARMADILLO_ROOT}\examples\lib_win64
Click the "Apply" button before pressing "OK"
--Screenshot1
--Screenshot2
It seems to work now! I am using the Lapack and BLAS from the webpage indicated in the Readme file that I compiled myself according to #enhzflep comments.
To link everything properly in Eclipse CDT right click on your project and go to properties, then:
Under "C/C++ Build⟼Settings⟼GCC C++ Compiler⟼Command" type: g++ -I"C:\Armadillo\include" (replace with the correct path for your Armadillo folder). Make sure to do this for all Configurations (to be chosen in the drop-down menu at the top).
Under "C/C++ Build⟼Settings⟼MinGW C++ Linker⟼Libraries" add lapack and blas to the Libraries.
Under"C/C++ General⟼Paths and Symbols⟼GNU C++" add the path tot he include directory of your Armadillo folder C:\Armadillo\include.
It might be that it works with the original Lapack and BLAS files. It did not work for me, because I had only added the path to Armadillo under "C/C++ General⟼Paths and Symbols⟼GNU C++", which I guess only tells the editor what data types and functions to expect and not the compiler.
If you're using MinGW on Windows, a good option is to use OpenBLAS with Armadillo. OpenBLAS provides optimized and multi-threaded implementations of BLAS and LAPACK:
http://xianyi.github.io/OpenBLAS/
The OpenBLAS project provides pre-compiled versions for Windows. The source code can be also easily compiled under Linux and MacOS X.
Note that Armadillo doesn't support automatic installation under Windows. The cmake based installer provides a big fat warning message about that -- you're advised to heed it.
Instead of automatic installation, do the following (as described in the README.txt file that comes with Armadillo):
Manually modify include/armadillo_bits/config.hpp to enable the use of BLAS and LAPACK. In other words, make sure that ARMA_USE_BLAS and ARMA_USE_LAPACK are uncommented (enabled).
Also within include/armadillo_bits/config.hpp, disable ARMA_USE_WRAPPER, ie. make sure it's commented out.
When compiling your C++ programs, tell your compiler to use the armadillo include folder directly, and link with openblas. For example, with gcc use the -I switch to specify the include folder.

Library link error when starting Windows application compiled with MinGW on another computer

I wrote a simple HelloWorld console application and compiled it on Windows 7 with MinGW compiler using one of these commands:
gcc -Wall -pedantic Hello.c -o Hello.exe
g++ -Wall -pedantic Hello.cpp -o Hello.exe
However the compiler links some own dynamic libraries into the app and when i copy the executable into another computer with Windows 7, which does not have MinGW installed, i'm getting missing library error. On Linux this problem is solved by package system, which automatically installs all needed libs, but in Windows you surely don't want to tell your users to install MinGW in order to run your program.
So my question is: How do i link all libraries properly and what else do i have to do to make my application run independently?
Although i believe, this must be a fundamental problem to all Windows programmers, i have been unable to find any answers on the internet (maybe i just don't know how and what to search).
It was in the FAQ at some stage, but now I seem to find it only on this page:
Why I get an error about missing libstdc++-6.dll file when running my program?
GCC4 dynamically link to libgcc and libstdc++ libraries by default
which means that you need a copy of libgcc_s_dw2-1.dll and
libstdc++-6.dll files to run your programs build with the GCC4 version
(These files can be found in MinGW\bin directory). To remove these DLL
dependencies, statically link the libraries to your application by
adding "-static-libgcc -static-libstdc++" to your "Extra linking
options" in the project settings.
Try this,
g++ -static-libgcc -static-libstdc++ -Wall -pedantic Hello.cpp -o Hello.exe
I'm afraid to say that with all of the applications installed on my machine, it's easy to identify which ones were built with MinGW. The telltale sign is a folder filled with libraries.
Check to see if the libraries that you need are distributable, and then simply include them in your .exe directory.
Although you may have other applications installed on user's machine, and some of them may contain the libraries that you need, there's a good chance that your application wont be compatible with them. This is why asking your users to install MinGW would be unlikely to work anyways.

Developing OpenGL Applications Everywhere

I am studing OpenGL and I usually study in the laboratory in my university.
My problem is that the computers in the university don't have the necessary libraries installed for me to study the programs, and I have only user permissions in these computers, so I can't install anything that need administrator permissions.
Usually I develop in a linux environment, but the computers on the lab all got Windows XP or Windows 7 running. I use g++ to compile my programs, freeglut and opengl library. I also use the OpenGL extensions to be able to use vertex array objects for example, and other features to the newers versions of OpenGL.
I am thinking if that is a way to put everything I need in my pendrive and just execute this in every computer I go, without having to install nothing. Maybe something like put all the libraries with cygwin and a notepad++ portable in the pendrive and compile my programs using the cygwin on the pendrive.
But how can I achieve this, what modifications I need to do on my code?
Edit:
To solve my problem I did the following:
Installed MinGW with MSYS in my PC;
Create a batch file (.bat) named startMinGW with the following code inside MinGW folder:
#echo off
mode con:cols=130 lines=50
path=%path%;%CD%\bin
path=%path%;%CD%\lib
path=%path%;%CD%\msys\1.0\bin
title MinGW Compiler
echo Ready!
cmd.exe /K
Execute the batch file to compile the following;
Compile freeglut 2.8.0 with MinGW, going in the root folder of freeglut and executing:
sh configure
make
Compile glew 1.9.0 with MinGW, going in the root folder of freeglut and executing:
make
Copied the .h files localized in the include folders of freeglut and glew, to the include folder of MinGW
Copied the .a, .dll, .la, .lai (I don't know if it's necessary to copy the .la and .lai files) files localized in the lib folders of freeglut and glew, to the lib folder of MinGW
Copied the folder C:\MinGW to my pendrive.
Now to compile an OpenGL program I just need to execute startMinGW from the pendrive and execute
g++ file.cpp -lopeng32 -lglu32 -lglut -lglew
The glut (or freeglut) lib is cross-platform. Why wouldn't you compile it on Windows ?
Use a build generator (like CMake) to easily create your makefile on each platform and you're done.
Qt OpenGL API is probably the best fit in your case.