Opencv in visual studio 2010 [duplicate] - visual-studio-2010

I'm trying to use opencv 2.3 with Visual Studio 2010 Express. My code is from example:
#include "stdafx.h"
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
int c;
// allocate memory for an image
IplImage *img;
// capture from video device #1
CvCapture* capture = cvCaptureFromCAM(1);
// create a window to display the images
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
// position the window
cvMoveWindow("mainWin", 5, 5);
while(1)
{
// retrieve the captured frame
img=cvQueryFrame(capture);
// show the image in the window
cvShowImage("mainWin", img );
// wait 10 ms for a key to be pressed
c=cvWaitKey(10);
// escape key terminates program
if(c == 27)
break;
}
return 0;
}
What have I done so far?
Added build\bin and one of build\{x86|x64}\{vc9\vc10\mingw}\bin to my system path (to use DLLs).
Added build\{x86|x64}\{vc9\vc10\mingw}\lib or build\{x86|x64}\{vc9\vc10\mingw}\staticlib as library directories to my linker settings.
Added build\include and build\include\opencv as include directories to my compiler settings.
And the result is:
1>LINK : fatal error LNK1104: cannot open file 'c:\OpenCV2.3\build\x86\vc10\lib.obj'
There's no lib.obj in OpenCV folders. I've only unziped OpenCV-2.3.0-win-superpack.exe, without using CMake software.
What am I doing wrong?

Well, the official guide is for installing OpenCV 2.1 on VS2010, so I wrote some instructions below that shows how to properly install and configure the x86 version of OpenCV 2.3 on Visual Studio 2010 (Express), since a lot of folks seem to have problems setting it up correctly.
Download OpenCV-2.3.0-win-superpack.exe and execute it to extract all files to a folder named OpenCV2.3. Inside this folder there are 2 directories: build and opencv. All the setup on VS2010 will refer to the build directory. For practical purposes I moved the folder OpenCV2.3 to my C:\ drive, so pay attention to the paths I suggest on this guide as yours might be different.
On Visual Studio, create a new Win32 Console Application project and name it whatever you like. After that, a new window will show up. Click on the tab Application Settings and make sure the option Empty Project gets selected:
Add a new file main.cpp to the folder Source Files, then add this code to main.cpp:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: ./opencv_hello <file.png>\n");
return -1;
}
IplImage* img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (!img)
{
return -1;
}
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
cvShowImage("display", img );
cvWaitKey(0);
return 0;
}
At this point, we need to configure the project so it can locate OpenCV headers and libraries. Go to the Project Properties (ALT+F7), and once the new window shows up do the following:
On the Configuration box, select All Configurations
Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directories to add these 3 paths (for the headers):
C:\OpenCV2.3\build\include\opencv
C:\OpenCV2.3\build\include\opencv2
C:\OpenCV2.3\build\include
Note that include\opencv is for the C interface of OpenCV and include\opencv2 if for the C++ interface. We are also adding the folder include to prevent our build from being broken by some headers of the C interface that refer to C++ headers as opencv2\core.
Then, add the path of the libraries on Configuration Properties > Linker > General, and on the Additional Library Directories field, add this: C:\OpenCV2.3\build\x86\vc9\lib:
Finally, for this simple test we are going to add the libraries opencv_core230.lib and opencv_highgui230.lib. So go to Configuration Properties > Linker > Input and add them:
When writing more complex applications you'll probably need to add other OpenCV libs that I did not
mentioned on this little project of ours.
Press F7 to Build Solution and you should see:
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
To be able to execute the application you'll need to modify the PATH environment variable of your system to add the location of OpenCV's DLLs. Add this to end of PATH:
; C:\OpenCV2.3\build\x86\vc9\bin

If you are struggling with editing the PATH environment variables, you can also copy the required .dll files to your project folder:
The dll files are located in this folder ../OpenCV2.3/build.x86/vc9/bin
Then copy them to the folder where .exe file is created:
c:\Users\PIMMES\Documents\Visual Studio 2010\Projects\eigenfaces\Debug (Ofcourse you have to change the path to your Debug folder)
You only have to copy the .dll files which you are using in your project (#include for example) For example if you get an error message saying opencv_core231d.dll is not found then get this .dll file from the above location (bin folder) and copy to your project Debug folder.
Hope this helps..

Whenever I make a program that uses opencv 2.2 or greater I include everything, and then comment out the libraries I don't need. Try this, I'm sure you need more than highgui.h
#include "opencv2\opencv.hpp"
using namespace cv;
//#pragma comment(lib, "opencv/opencv_calib3d231.lib")
//#pragma comment(lib, "opencv/opencv_contrib231.lib")
#pragma comment(lib, "opencv/opencv_core231.lib")
//#pragma comment(lib, "opencv/opencv_features2d231.lib")
//#pragma comment(lib, "opencv/opencv_flann231.lib")
//#pragma comment(lib, "opencv/opencv_gpu231.lib")
//#pragma comment(lib, "opencv/opencv_haartraining_engine.lib")
#pragma comment(lib, "opencv/opencv_highgui231.lib")
//#pragma comment(lib, "opencv/opencv_imgproc231.lib")
//#pragma comment(lib, "opencv/opencv_legacy231.lib")
//#pragma comment(lib, "opencv/opencv_ml231.lib")
#pragma comment(lib, "opencv/opencv_objdetect231.lib")
//#pragma comment(lib, "opencv/opencv_ts231.lib")
//#pragma comment(lib, "opencv/opencv_video231.lib")

Related

Visual Studio 2017 only include my files using #include "File" but failed with #include <file>

I am building a C project that uses some header files of mines. I put these files in a folder and add that path in Project Properties-->C/C++-->General-->Additional Include Directories. When I use #include "myfile.h", it works fine. But when I use #include <myfile.h>, it says Can not open include file. Thanks

debug assertion failure during domodal

I have just started working on CDialog classes. When I try to execute the following code, I am getting debug assertion failure.
#include "stdafx.h"
#include "resource.h"
#include <afxwin.h>
#include "dialog.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
dialog dial(NULL);
dial.DoModal();
return 0;
}
I have created a dialog resource and CDialogEx class named dialog. The error I am getting is
"Debug Assertion Failed!
Program: ...pp_projects\ConsoleApplication1\Debug\ConsoleApplication1.exe
File: f:\dd\vctools\vc7libs\ship\atlmfc\include\afxwin1.inl
Line: 24"
Can anyone give me a solution?
Your project name suggests that you have created a Win32 Console Application project and added the MFC stuff. What you need to create is a MFC Application project.
MFC can also be used in a console application, but then you would not show dialogs and you would need to initialize MFC first.

VC++ Multiple Project Solution

I am very new to C++ and I am attempting to setup multiple projects in the same solution in VS2013. Currently I have stepped back to a simpler example project to try to figure out my error.
Project 1:
Main.cpp
#include "Test.h"
#include <iostream>
using namespace std;
int main()
{
cout << _MOVEMENTSPEED();
system("pause");
return 0;
}
Project 2
Test.h
#ifndef TEST_H
#define TEST_H
int _MOVEMENTSPEED();
#endif
Test.cpp
#include "Test.h"
int _MOVEMENTSPEED()
{
return 10;
}
Whenever I attempt to build this I get the error "error LNK2019: unresolved external symbol "int __cdecl _MOVEMENTSPEED(void)" (?_MOVEMENTSPEED##YAHXZ) referenced in function _main c:\Users\Max\documents\visual studio 2013\Projects\Project1\Project2\Main.obj" and "Error 2 error LNK1120: 1 unresolved externals c:\users\max\documents\visual studio 2013\Projects\Project1\Debug\Internal".
UPDATE
I tested this same code but within one project file in visual studio and it worked fine.
When you create multiple projects you should do the following:
Make sure the following:
Include the .h file properly from other project(Generally every project has its own directory), so you need to include the file like below:
#include "..\Test\Test.h"
Export the function / class by using _declspec(dllexport) and _declspec(dllimport)
Include the .lib file properly in the project settings of Link tab.
Set the project dependencies correctly.
The below links should help you:
http://support.microsoft.com/kb/815650
http://msdn.microsoft.com/en-us/library/799kze2z.aspx

CMake: How to use different ADD_EXECUTABLE for debug build?

I'd like to build my application such that debug mode is a console application and release mode is a Win32 application. According to the documentation I need to add WIN32 to add_executable depending on whether I want a console application or not.
Because I'm using Visual Studio, I can't use CMAKE_BUILD_TYPE (the generated project contains multiple configurations). How can I tell CMAKE to use WIN32 for release builds and omit it for debug builds?
Quoting http://www.cmake.org/Wiki/VSConfigSpecificSettings
if(WIN32)
set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE")
set_target_properties(WindowApplicationExample PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE")
set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE")
set_target_properties(WindowApplicationExample PROPERTIES COMPILE_DEFINITIONS_RELWITHDEBINFO "_CONSOLE")
set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:windows")
set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:windows")
endif(WIN32)
UPDATE: This feature is broken in recent versions due to a bug. One workaround I've found is to specify "/SUBSYSTEM:windows" instead of "/SUBSYSTEM:WINDOWS". That seems to work for some reason.
Dunno if this bug has been fixed in CMake yet. I'm using VC++ 2010 express and CMake v2.8.10.1 (which is currently the latest release) and I'm still having the exact same problem.
A working solution was provided here: modify your source code (e.g. main.cpp/main.c) by adding:
#ifndef NDEBUG
#pragma comment(linker, "/SUBSYSTEM:CONSOLE")
#endif
Alternatively, you could add the linker flag "/SUBSYSTEM:WINDOWS" to the release-mode build. I'm using this definition which seems to work:
#ifdef _MSC_VER
# ifdef NDEBUG
# pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
# else
# pragma comment(linker, "/SUBSYSTEM:CONSOLE")
# endif
#endif
Use the entry-point setting in order to avoid linker errors in case you've defined:
int main(int argc, char* argv[]) { ... }

How to reference .cpp files without including them into the solution in Visual Studio?

I'm trying to compile this code:
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <luabind/luabind.hpp>
#include<iostream>
int main(){
lua_State*pL=lua_open();
luabind::open(pL);
lua_close(pL);
return 0;
}
But I don't have a .lib of luabind, so I use the source with the .h/.cpp files.
The way I do it is by adding the directories to include, but I get a link error.
The only way I can compile is by adding the .cpp files as existing elements, but the solution tree gets messy with the additional files.
Can somebody tell me if there's a way to add the directory of the additional .cpp files in the solution's properties?
Thanks
Compile the lua cpp files into a static library. Add the directory where you put those under "linker | input | additional library directories".
You need to tell the linker where to find the functions referenced by the .h files (the .lib file, typically).

Resources