Setting up OpenGL in CodeBlocks - c++

I am having trouble getting the right setings in order to use OpenGl in CodeBlocks.
I have used the instructions from this tutorial: GLUT but for my project to run I need the following flags: -lGL -lGLU -lglut which I can set in the Other linker options tab from Build options. When I do this, the compiler says cannot find -lGL -lGLU -lglut. What do I have to install in order for these libraries to work? GL.h GLU.h glut.h? and if yes how can I link them to the project? By adding them in the Link libraries tab? And also from the project tree which appears in Build options does the name of the project have to be selected when I install these libraries, or Debug or Release?
In Build options, if I select the name of the project, at Link libraries I have the following: glut32, opengl32, glu32, winmm, gdi32 but I don't remember giving a path for them. Are they correct or do I have to change them as well?
I would like to mention that the created project is a GLUT project and that I am using Windows 7.

The issue is that you are telling Code::Blocks to look for opengl32.lib, glu32.lib, etc. but not where to look for them. Hence the error during linkage.
Under Project Build Options -> Search Directories -> Linker you need to add the directories containing your OpenGL libraries. Example:
Note that the directory containing your OpenGL libraries will probably be different from mine, since according to the link in your question they should be wherever you put MinGW.
You will also need to make sure you add to the Search Directories the location of the OpenGL header files. Example:
This is the folder that contains the gl subdirectory.

After downloading the GLUT bin zip file (considering you already installed codeblocks earlier), you extract all the files in it and copy those three files separately. The glut32.lib goes to c:\program files\mingw\lib and copy glut32.dll to c:\windows\system and copy glut.h (header file) to c:\program files\mingw\include\GL
Then open codeblocks and go for new project>GLUT. Then set up the GLUT location to Mingw(in program files) and then finish. It worked for me just fine.

Related

wxWidgets include files all in same folder?

I'm on a Xubuntu machine. I've downloaded and compiled the source, did the make install.
I'm trying to create a simple wxFrame class, but when I include <wx-3.0/wx/frame.h>, it says Can't resolve type wxFrame. When I look inside frame.h, it includes a reference to <wx/toplevel.h>. Both frame.h and toplevel.h are in the same folder. Why does the compilation place the include files all in the same folder, but the paths to the files are different?
I am using CLion IDE and the include/lib paths are set by cmake's FIND_PACKAGE().
#ShrimpCrackers,
It is generally advisable to start you project by copying one of the sample code into you project and then set you project properties to the output of wx-config --cxxflags and wx-config --libs.
So what you should do is to copy the wxWidgets/samples/minimal/minimal.cpp over to the directory where you project is, fix the project properties and recompile.
BTW, you don't have to do make install. You might want to compile debug and release versions of the library on one machine which defeats the purpose of installing. You just use the compiled version of the library

How to link against GLFW on Windows

I am looking to learn how to use OpenGL with C++, and I am following the tutorial at open.gl. I am at this part in the tutorial, and I am using GLFW. I tried following the instructions to build GLFW,
After you've downloaded the GLFW binaries package from the website or
compiled the library yourself, you'll find the headers in the include
folder and the libraries for your compiler in one of the lib folders.
-Add the appropriate lib folder to your library path and link with GLFW.
-Add the include folder to your include path.
I downloaded it, and moved the include folder into my MinGW include folder, but I was unsure where to find the lib folder, how to add it to my library path, or how to link it with OpenGL. This was not explained in the tutorial, and I could not find any instruction online about what this meant or how to do it. I proceeded without doing the other two steps, and when attempting to run the test code from the tutorial:
#include <GLFW/glfw3.h>
#include <thread>
int main()
{
glfwInit();
std::this_thread::sleep_for(std::chrono::seconds(1));
glfwTerminate();
}
I received this error:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\thread:35:0,
from D:\Files\Documents\Programming\opengl_tut.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
I added -std=c++11 to my compiler flags, and I received this error:
D:\Files\Documents\Programming\opengl_tut.cpp: In function 'int main()':
D:\Files\Documents\Programming\opengl_tut.cpp:7:10: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(1));
Thinking the problem might just be with std::this_thread, I removed that line from my program, and ran it to see if initializing and terminating GLFW would return any errors. I got this:
C:\Users\Brett\AppData\Local\Temp\ccCJrDGj.o:opengl_tut.cpp:(.text+0xc): undefined reference to `glfwInit'
C:\Users\Brett\AppData\Local\Temp\ccCJrDGj.o:opengl_tut.cpp:(.text+0x11): undefined reference to `glfwTerminate'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\Brett\AppData\Local\Temp\ccCJrDGj.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
I think this may be because of the fact that I skipped earlier parts of the tutorials.
TL;DR
Tried to follow tutorial to use OpenGL
Couldn't figure out how to link GLFW
Couldn't find lib folder
Got errors relating to std::this_thread
Got error relating to GLFW
EDIT
I found some info on the GLFW site saying to use Cmake. I downloaded it and tried using it to build from the downloaded files. It made a new folder, but I'm not sure what to do with it.
First, it's easy to do such config in Visual Studio, just NuGet, and search glfw, install, Visual Studi/NuGet will handle everything like set proper Include path, Additional Library path, add additional library files
Second, it looks like your link issue, try to add these to your gcc cmd line
g++ -o example.exe example.o -L. -lexample_dll
Third, check the glfw library you downloaded, use MinGW-w64 lib files. Also remember distinguish the static and dynamic libs
It looks like you've downloaded the source files. This will require building (using CMake). You should go download the precompiled binaries. To do this, go back to glfw.org and instead of hitting the download button on the home page, go to the download tab in the top right and under "Windows pre-compiled binaries" select the 32 or 64 bit version and click download. See the attached image
Then, when you extract from the .zip folder you should find in it several folders - the include folder, as well folders with names beginning with "lib" (like lib-mingw, lib-mingw-w64, lib-vc2010, lib-vc2012, etc). You only need one of them, which depends on the compiler you're using - minGW or Visual C++ (vc). Choose the appropriate folder and inside you should find .lib and .dll files.
Now, whenever you compile your OpenGL program, you must tell the compiler (the linker, really) to link those files to your program's object files. You do this by using the proper compiler command line arguments. You can read about doing that in MinGW here: http://mingw.org/wiki/Specify_the_libraries_for_the_linker_to_use
For that tutorial you'll probably only need to link glfw3.lib (for MSVC) or libglfw3.a (for MinGW). If you're using GLEW, you'll probably just need glew32.lib and glew32.dll (the latter is found in the "bin" folder not the "lib" folder).
Hope that helps answer your question.

CLion MinGW freeglut opengl library linking

I am trying to follow this guide to configure my freeglut and opengl .
I am used to the intellij enviroment via android studio so i would like to work in Clion.
I am stuck at the part....
Libraries: the OpenGL library "libopengl32.a", GLU library
"libglu32.a" and GLUT library "libfreeglut.a" are kept in
"\lib" directory. This directory is in the implicit
library-path. Nonetheless, we need to include these libraries in
linking. They shall be referred to as "opengl32", "glu32", "freeglut"
without the prefix "lib" and suffix ".a".
how do i add this in Clion ?
CLion uses cmake for building your projects. Follow below steps to add link libraries to your project.
Open the CMakeLists.txt file in your project and add,
target_link_libraries(<target executable> libopengl32.a libglu32.a libfreeglut.a)
to that file.
target executable is the executable which you want to link these libraries. Normally this is your project name as defined in add_executable.
Note: Cmake will show you an error if you place target_link_libraries before add_executable.

Customize compiler command in C++ IDEs

Is it possible to customize or specify the command for compiling in codeblocks or any of the other IDEs like NetBeans and Eclipse C++?
I'm asking this because I tried all those IDEs and no one can find the libraries even though it's there listed in the code assistance list (NetBeans 7.2). However, when I compile it in shell in Ubuntu 12.04 LTS, the libraries are located and the program can be compiled.
I use one of the commands:
gcc -lGL -lglut filetoCompile.cpp -o compiled.sh
g++ -lGL -lglut filetoCompile.cpp -o compiled.sh
to compile. I'm trying to use glut for open GL stuff and this is the library it can't find. I assume it may be the same for other third party libraries I may add in the future that's why I really need to get this fixed that's why my plan is to have the IDE use that command instead because it might work.
What's also odd is that my /usr/local/lib directory only contains 2 folders: python 2.7 and python 3.2. I'm not sure if this has an effect but is this normal? Shouldn't the libraries somehow be found here and the headers are in /usr/local/include?
The header files are located in /usr/include. Is this the correct location?
Generally speaking, yes this is possible.
In Eclipse CDT, you can set the directory where the header files are in this window:
Right click on project name; Properties; C/C++ General; Paths and symbols; Includes;
In window you must add all directories that contain header files for the functions/classes you want auto completion for. For example, my configuration is:
/usr/include/c++/4.4.5
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.4.5/include
to find which folder contain an header file you can use the find or locate command, and set the IDE properly.

How to include OpenCV include into XCode 4 c++ project?

I have seen posts on this before, but none of the answers seemed to work for me. I just installed OpenCV on my Mac and got it to work fine with g++ from the command line. I installed it using Mac Ports:
$ sudo port install opencv configure.compiler=llvm-gcc-4.2
To compile with g++, I use the command:
g++ myfile.cpp -o myprogram -I/opt/local/include -L/opt/local/lib -lopencv_core.2.4.2 -lopencv_calib3d.2.4.2
And my OpenCV include header in the main.cpp file is:
#include "opencv2/opencv.hpp"
And all works well.
Now, when I tried to include the library in Xcode, it simply says 'opencv2/opencv.hpp' file not found. To include the library in Xcode, I followed some detailed instructions. First, to the project target, I added /opt/local/lib to the library search paths, and /opt/local/include, /opt/local/include/opencv and /opt/local/include/opencv2 to the header search paths under the Build Settings tab. Then I clicked on Build Phases, and to Link Binary With Libraries I added all of the OpenCV .dylib files in /opt/local/lib (all 11 of them). This didn't work. I've tried many other things, including adding to user search paths and setting up "Other Linker Flags" for the target. Nothing worked. It can't find the file. If I use the exact path (#include "/opt/local/include/opencv2/opencv.hpp") it can't find all the other header files.
The version of OpenCV is 2.4.2. I am using Xcode 4.3.3 on OS X Lion 10.7.4. This is a C++ project. Any ideas?
This problem can be solved by going to the project properties and click on the target.
In the build settings search "header search path".
And there you add "/opt/local/include".
By doing this you're telling Xcode to look for the header files in that directory also.
Now you can use:
#include <opencv2/opencv.hpp>
If you don't want to do this every time you start a new project you can add the .h and .hpp files to the defaulti include directory Xcode uses. You can find this directory in Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include .
You will have to add the binaries to your project as well. For that you go to "build phases" and then add the binaries in the "Link binaries with libraries" section.
I hope this helps you out.