Using OpenGL with Visual Studio 2013 Express - c++

To give you an idea of what I am really trying to do. My goal is to create a c++ program in Visual Studio and using OpenGl display a blackscreen and a white dot in the middle of the screen.
Before I can even get to the coding part though, I have to include the OpenGL library somehow.
Looking at OpenGL documentation they say that it's already installed, I just need to initialize it.
I'd rather not have to do all the initialization work as it's already been done several times, such as FreeGLUT, but I have 2 real problems that I currently just do not understand.
1) How do I compile FreeGlut?
I've downloaded the source code for FreeGLUT here http://prdownloads.sourceforge.net/freeglut/freeglut-3.0.0.tar.gz?download
I configured it with CMAKE into a visual studio 2013 compatible project.
but once I open it with Visual Studio and try to compile it, I get a bunch of errors saying:
Error C1083: Cannot open include file: 'EGL/egl.h': No such file or directory c:\freeglut-3.0.0\include\gl\freeglut_std.h 136 1 One_static
2) Once I get it compiled, how do I link it to my c++ project so that I can do
#include<FreeGLUT.h>
or
#include <GL/glut.h>
?

Most likely CMake did configure it wrong; EGL is used in embedded systems (think Android, set-top boxes, and such) not Windows. Double check that CMake does something sensible there.
After you've built FreeGLUT copy it somewhere convenient (do not copy it into the Visual Studio installation directory) and add the directories where you placed FreeGLUT to your own OpenGL project's compiler and linker search paths (reachable in the Visual Studio Build configuration).
Personally I place customly built libraries at
C:\local\include\ (the header files)
and
C:\local\lib (the .lib, .a and .dll files)
I also tend to give libraries an architecture infix like x86_32 or x86_64. e.g. freeglut-x86_64.dll. It's unlikely Windows will ever get some kind of "fat binary" in which the code for several architectures can be merged.
For convenience put the DLL path into the system search path for DLLs. When deploying your program copy the required DLLs into the same directory as the EXE files.

Related

I cant link against opengl32 in Windows

I am working on porting an app of mine that I made in OS X to Windows 10. I'm using visual studio 2015 Community Edition.
To get the project running, I need to link against GLFW and, in Windows as I understand, GLEW, since I am targeting GL 4.5.
So I have the glfw and glew libs. I also use Cygwin so I have the libs installed in usr/local/(bin | lib | include) to mimic the folder structure I'd use in OS X for these libs.
The issue I'm having is with the opengl lib itself.
Until yesterday, I had managed to get my project compiling and initializing the glfw window with the following visual studio settings:
Project->Properties-> :
C++->General:
Additional Include Directories:
C:\cygwin64\usr\local\include
Linker->General:
Additional Library Directories:
C:\cygwin64\usr\local\lib
C:\cygwin64\usr\local\bin
Linker->Input:
opengl32.dll
glew32s.lib
glfw3.lib
The first thing to notice is that I was linking against opengl32.dll, not .lib. I saw a bunch of questions / posts online that state that when you install Windows SDK you have a opengl32.lib in Program Files/Microsoft SDKs/Windows (x86 or otherwise). This is not the case for me. I have even reinstalled the Windows 10 SDK and it does not install any opengl lib, static nor dynamic, anywhere.
So I'm pretty sure that my opengl dll was coming from Windows/System32 because that is the only place in the whole machine where there is any sort of opengl lib.
Which makes me think I might have also had C:\Windows\System32 in the linker's additional library directories section. I say makes me think, because I had set this up with a lot of struggle about a week ago. Since then I was able to develop just fine.
Last night all I did was commit my work to a git branch, switch to another branch and merge to that branch. After the merge, all Visual Studio linker/C++ settings were wiped. So I had to recreate them, as I showed above.
Now what happens is that if I include C:\Windows\System32 and the opengl32.dll VS spits out:
LNK1107 invalid or corrupt file: cannot read at 0x2E0 OpenVRTest C:\Windows\System32\opengl32.dll
And if I don't include it obviously half the stuff in glfw is unresolved.
Any hints as to how to get this working again?
It makes no sense to me... It was working just fine and it has to have been working fine with this opengl32.dll
Also as a side note, I'm not sure why people insist that installing the Windows SDK installs a static version of the gl lib; at least it does not for me.
Linker->Input:
opengl32.dll
glew32s.lib
glfw3.lib
That opengl32.dll is wrong. In Windows development the linker always takes .lib files. In case of static libraries the .lib contains the actual library binary. In case of DLLs the corresponding .lib informs the linker about which DLL to use and which symbols it offers.
The main reason for this particular choice of how things are to be done was, that in Windows development it shall be possible to link against a DLL without having the actual DLL around.
Also system libraries always are suffixed …32 even on 64 bit systems.
The issue was that among the VS configuration settings that got wiped, were a couple things related to x86 vs x64 platform settings. So the project was now trying to build for 64bit linking against the 32bit dll in System32.
So that's that.
A separate issue is why a lot of answers speak of an opengl32.lib (static) that theoretically comes with the Windows SDK but I have not seen it anywhere. I just link against the dynamic one.

how to deploy simple boost program written in visual studio on other computers

I have written a simple program in C++ that uses the boost filesystem library to organize some files. I am using the Visual Studio 2013 Express for Windows Desktop IDE. I would like my program to run on other computers without an installer or anything else. The user should be able to just download the program and click on it.
I built my program in Release mode and it works on my computer. When I tried to deploy on another computer, the error I got was MSVCP120.dll is missing. To fix this I right-clicked on my project, clicked properties, then Configuration Properties > C/C++ > Code Generation, and I changed the Runtime Library from Multi-threaded DLL (/MD) to Multi-threaded (/MT). Now my program won't compile properly because of this build error:
Error 1 error LNK1104: cannot open file
'libboost_filesystem-vc120-mt-s-1_55.lib' C:\Users\inspection\Desktop\sortFile\Project1\Project1\LINK Project1
I looked in the following folder:
C:\Program Files\boost\boost_1_55_0\libs\filesystem\build\msvc-12.0\release\link-static\threading-multi
and I could not find libboost_filesystem-vc120-mt-s-1_55.lib, though I could find libboost_filesystem-vc120-mt-1_55.lib. The file names are close, they only differ by an '-s' term located after the '-mt' term.
How can I statically link the boost filesystem library so that my whole program runs as a standalone executable, without another user having to install anything?
Boost needs to be recompiled to match your new runtime library setting. The filename of the Boost library indicates the settings it was linked with, and the Boost auto-link code automatically updated the needed filename to match your new settings. In this case, it quite accurately indicates that you need a Boost lib which was compiled with a static stdlib link.

How to link opencv and other dll files to output exe of visual studio 2013

I am new with visual studio, opencv.
I am using visual studio 2013, opencv and c++ for my project.
I configured (copied path) the opencv and other library to my computer environment system.
After run the project in visual studio, normally, there will be an exe file in the project.
I can copy the exe file in the project folder and copy to other place in my computer and it will run normally.
This is because my computer environment systems are configured with opencv and other library.
I want to do the same thing with other computers BUT I do not want to manually configure each computer with opencv and other libraries.
Are there any ways that I can do to link everythings all in exe file after run the project in visual studio 2013 so that I can run the exe without depend on the path of libaries and opencv?
EDITED
I use opencv installer opencv2.4.7.exe
In the current VS2013 my project, i configured my project and opencv installer as this link
http://www.anlak.com/using-opencv-2-4-x-with-visual-studio-2010-tutorial/
question : Can i use the library in folder C:\opencv\build\x64\vc11\staticlib come from the opencv installer no need create my own library from source opencv?
question : In case i need to generate new library from opencv source (http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html) or use lib in static folder of opencv installer, if i want to include it all to exe files, do i need to create new project and reconfigure?
Thank you.
First you need to rebuild openCV to generate static libraries instead of dynamically linked ones. This way all code that your application uses is thrown together in one single exe-file (which will probably be significantly bigger). This exe-file you can move to other computers and they should still work there, provided they have an architecture that is at least compatible with yours. So if you build it on an x86 perconal computer (32-bit), it should basically work on any other personal computer. If you build it on a x64 computer (AMD 64-bit), it will only run on other x64 machines. At least this is true assuming both systems use the same syscall API (Windows NT, POSIX...).
For openCV you do this by setting the BUILD_SHARED_LIBS build flag to false (see OpenCV as a static library (cmake options), the following line is taken from there):
cmake -DBUILD_SHARED_LIBS=OFF ..
Once you have done this, you will see that the openCV folder looks very similar to the one you have now, except that in your 'lib' folder there will now be .lib-files instead of .dll files (at least if you are working on Windows, which I assume you do since you are using Visual Studio).
Next step is to go to your project settings and set your linker to link with the static libraries instead of the dynamically ones. If you have used openCV's local method for linking, you can go to project settings -> linker -> input -> Addtional dependencies. There you change the extension of all the openCV libraries from .dll to .lib.
Now you should be able to rebuild your application and the resulting exe-file should have all dependent libraries contained in it.

I can't make SDL + OpenGL work in Visual Studio 2012

I just started a tutorial to learn C++ OpenGL using SDL. The steps shown in the videotutorial were done by using the IDE called Code Blocks, but I thought it was going to work also for Visual Studio 2012.
I downloaded the SDL2 developer which is for Windows x64 (my Windows 7 is 64 bits, but the Visual Studio 2012 I have is an x86 version)
To install the SDL2 in Visual Studio 2012, I put:
the .h files in the include folder (D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\SDL).
the .lib files in the lib folder (D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\SDL).
the .dll file in C:/Windows/system folder.
After that, I opened the IDE, made a new project and while I was writting the code, autocompletion popped up for specific SDL features, meaning success... until I tried to run it.
This is the code I wrote according to the videotutorial:
#include "SDL.h"
int main(int argc, char* args[]){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
return 0;
}
When I click on "Start Debugging" or "Start without debugging", after pressing yes to build it, the output comes with errors.
Here's a screenshot of the code I wrote, plus the output:
After that, I tried an older version of the SDL the tutorial was already using, which was the 1.2 (I overwrote few files of the newer version) but still no good results.
How do I fix it so I can practice with OpenGL + SDL features?
TLDR:
When you use a library in your project,
Compiling needs header filenames (#include <*.h>) and include directories containing them
Linking needs lib filenames (*.lib, what you're missing) and library directories containing them
Running needs dynamic libraries (*.dll), commonly using the same name as the .lib in the current or system directory (VS has binary directories too but I've never needed to know what they're for exactly)
When you compile code and add #include <SDL.h>, the compiler runs through a list of possible directories its been given to find that file. There are some default directories already set up, but so you don't have a mess of all your libraries in one place it's possible to add more. For example, Setting Global C++ Include Paths in Visual Studio 2012 (and 2011, and 2010). If your SDL.h is in a subdirectory in an include directory it won't be found - eg. just check this shouldn't be <SDL/SDL.h> or something similar.
With the include path set and the correct #include your code can compile. Now it's the linking stage. You've got some code that says I need the these functions and the linker has to either find the actual code for those functions and link them statically (all the code can be found in the .lib) or set up the program to link at runtime, dynamically (in which case the program needs to be able to find the .dll, and I think on windows you need a .lib file that goes with it).
So in addition to the include directory you need a library directory. Also, just like you said to the compiler #include <SDL.h> you need to tell the linker to look for the code for those functions defined in SDL.h in SDL.lib (which may end up actually providing the code at runtime when loading SDL.dll). I think specifying libraries could even be done through #pragma calls, but it's more common to see the library files specified as arguments to the linker program. As #ScottMcP-MVP said, append SDL.lib to the Project Properties->Configuration Properties->Linker->Input->Additional Dependencies list. If it still says unresolved external that generally means the linker still can't find the file, so check for typos and double check the directories. Worst case is a mismatch between SDL.h and SDL.lib or SDL.lib was compiled with different options or a different compiler.
The error you're receiving is the linker saying the compiler says I need an "external" function, SDL_Init, to make your code work but I haven't found/resolved-the-reference-to one in the list of libraries I've been given.
Just as an example/recap all these GUI options end up going to the compiler/linker. So for example a full compile line might look like this:
./compiler -I/path/to/my/includes -L/path/to/my/lib -lmylibrary.lib mycode.cpp -o myexecutable.exe
Where mycode.cpp contains #include <mylibrary.h>. Note that here the ./compiler is operating as both compiler and linker. The process could be split into:
./compiler -I/path/to/my/includes mycode.cpp -o mycompiledcode.o
./linker -L/path/to/my/lib -lmylibrary.lib mycompiledcode.o -o myexecutable.exe
Once you have your executable it needs to be able to find SDL.dll when you run it. Exactly as you've done, the executable should search the windows system directory. It should also search its current directory. On linux you can set an rpath (not sure on windows). If you keep getting application configuration incorrect and the dll is definitely there it's time to grab dependency walker and see what dll name the program is actually trying to load. Worst case is the application finds a different version of the dll or something else with the same name before the desired one.
Good luck. OpenGL is great fun!!
[EDIT]
You can use either x32 or x64 lib files, but all your code must be the same. It'll probably be easier to start with x32, and if you need fast double computation and more ram then change. OpenGL doesn't have great support for x64 yet so it probably won't make much difference to you.
Additional Dependencies is where you put just "SDL.lib". This file then needs to exist in one of visual studio's library directories. If you don't get unresolved externals from SDL anymore then that part is fixed.
Your _main error is probably to do with a disgusting hack *spit* SDL uses. Straight after #include <SDL.h> put #undef main (may want to surround in #ifdefs in case it changes):
#include <SDL.h> //my include is <SDL2/SDL.h>, as I have both an SDL and SDL2 dir in my include path
#ifdef main
#undef main //remove SDL's main() hook if it exists
#endif
An important thing to note: generally use </> to include external libraries and "/" to include headers within your project - What is the difference between #include <filename> and #include "filename"?
You have to link the lib file into your exe. See Project, Properties, Link, Additional Dependencies. Put the name of the lib file there.

Using a DLL with unmanaged code in Visual Studio 2010?

I'm fairly new to C++ and an trying to figure out to use the TagLib library for a project I am working on. I'm working with unmanaged C++ in Visual Studio 2010 on Windows 7 64bit. I've never used an external library before so I'm very confused on how to go about this.
From this blog entry I got the libtaglib.a and taglib.dll files. I ran across this SO question on how to use TagLib, but it deals with QT Creator, not Visual Studio and I'm not knowledgeable enough about the subject to understand what is being said to translate it into what needs done for Visual Studio.
So, some questions:
Is it even possible to do this with unmanaged code?
What exactly is the function of a .a file?
Most importantly, how do I go about using the taglib.dll in my program??
I've been all over Google looking for a way to do this, but my major problem is that everything I run across is over my head. Please let me know if more info is required. Any help is very much appreciated! Thanks!
I seem to have gotten it working successfully. Here's a rough outline of what I did:
1.) I used CMake to generate the Visual Studio solution.
2.) I attempted to build the tag project in the VS solution, but it failed.
3.) I made the corrections to a few source files as outlined here: http://old.nabble.com/taglib-fails-to-compile-with-MS-VC%2B%2B-2010-td29185593.html
4.) I built the tag project again in release mode. This time it was successful.
5.) I copied the resulting dll, def, and lib files to the same directory as the source files for my project.
6.) I copied the header files from the taglib source to a subdirectory in my project (not sure if this entirely good practice)
7.) In my project settings, I set the subdirectory with the header files as an additional include directory.
8.) I added the dll, exp, and lib files to my project by just going to Add>Existing Item.
9.) I added some code from the taglib examples and built it. Everything worked so I think I got it.
One caveat I ran into, since the DLL was built in release mode, my project had to be run in release mode or it would crash. I'm guessing that if I replaced the DLL with one built in debug mode I could run my program in debug mode, but I have not tried this.
You cannot use libraries specific to GCC (you can tell because they have .a extensions) with Visual Studio. You will have to build the library from source in order to use it with MSVC. Once you have done that it's a simple matter of adding the .lib generated from the build process to your project and things should work out of the box. (Note that it's a .lib you need whether you're compiling for dynamic linking or not -- doesn't matter in msvc land)
EDIT -- after looking at TagLib itself --
In order to compile TagLib you'll need to get the CMake build system, and TagLib itself, and have CMake build you a visual studio solution. Using that solution you'll be able to build the .libs and .dlls you need. Note that because TagLib is a KDE library, you'll probably need to also build some QT bits in order for everything work work successfully. However, I don't have specific experience with the library so I'm not going to be all that helpful here.
Yo do not have to recompile the source (to create the .lib file) if you have the .dll file. With dumpbin /exports and lib (both came with Visual Studio) yo can create a lib that you can link with your application. In this link you can see a nice explanation: http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/
But as Billy Said, probably you would need other parts of QT to use this library.