tags:

views:

71

answers:

2

Hi, here is a sample code. If I remove the stdafx.h file, the program wont compile.

stdafx.h file

'#pragma once

'#include "targetver.h"

'#include <stdio.h>
'#include <tchar.h>

Capture.ccp

// Capture.cpp : Defines the entry point for the console application. //

'#include "stdafx.h"
'#include "string.h"
'#include "cv.h"
'#include "highgui.h"




int _tmain(int argc, char ** argv[])
{
CvCapture * pCapture = 0;
IplImage * pVideoFrame = 0;
int i;
char filename[50];

//Initialize vieo capture
pCapture = cvCaptureFromCAM( CV_CAP_ANY );
if( !pCapture )
{
    fprintf(stderr, "failed to initialize video capture\n");
    return -1;
}

//Capture three video frames and write them as files
for(i=0;i<20;i++)
{
    pVideoFrame = cvQueryFrame( pCapture );
    if( !pVideoFrame )
    {
        fprintf(stderr, "failed to get a video frame\n");
    }

    //Write the captured video frame as an image file
    sprintf(filename, "VideoFrame%d.jpg", i+1);
    if( !cvSaveImage(filename, pVideoFrame))
    {
        fprintf(stderr, "faile dto write image file %s\n", filename);
    }

    //IMPORTANT : Don't release or modify the image returned 
    //from cvQueryFrame() !
}

//Terminate video capture and free capture resources
cvReleaseCapture( &pCapture );
return 0;
}

What is the purpose of the stdafx.h file - this file by the way is generated automatically.

Many Thanks

+1  A: 

It's part of the compilers Precompiled Header to speed up builds.

peterchen
And if you want to remove it, you'll need to modify the compilation settings for that .cpp file (or all files) to remove the 'use precompiled header' option - that's why it's failing. But there's no reason to. You can also move more headers into it if they're widely used throughout your app to save recompiling them throughout.
Rup
+1  A: 

As says peterchen, it's used to speedup builds and add some Windows stuff. This include is totally visual studio specific, and shall not be used if you want your program to be cross platform, which is the case of Open Cv library (you can use opencv under windows, linux, ...).

What is your compilation error message ?

Guillaume Lebourgeois
Visual studio specific, not windows. You can use gcc with mingw you know ;-)
Scharron
Totally, I wrote that to fast ;) I'll edit my answer right now.
Guillaume Lebourgeois
Huh? It won't speed up builds on other platforms but there's no reason it'll break anything. It's just a header file. Even GCC supports `#pragma once` IIRC
Rup
It'll break since it's not present, and if you import it, it references other includes not present. At least, you should have guards around it checking for an MSVC / windows build.The fact that compilers supper non-standard add-ons does not mean you have to use them. C++ is crappy by design, but you have to deal with it. ;-)
Scharron
@Rup: unfortunately, not in all cases. With the default settings for precompiled headers, VC5-2008 expect `#include "stdafx.h"` even if the correct path would be e.g. `"../stdafx.h"`. (Don't know about VS2010, but I wouldn't bet a pound of dog food on this being fixed.) Also, all declarations / includes before stdafx.h are ignored when it is set as precompiled header.
peterchen
@Scharron But it is present - it's not a system header, it's a (generated) header file in your project directory. So you can include it from any platform. Yes, stdafx.h does itself usually get generated with a few windows-only includes in it, though (windows.h or tchar.h)
Rup
@peterchen I've never seen the path one - even if you change the `/Yu` switch? Doesn't it work with `/I` anyway? Sure, I've been caught by the ignore-before too but once you know to always include it first that's fine. But these are VC++-specific problems anyway and certainly won't affect builds on other platforms as Guillaume was saying.
Rup
@Rup: For the "path one" - just move a .cpp file from the project folder to a sub directory. The includes usualyl need to be prefixed with "../", but if you do that on stdafx.h the compiler complains it's missing, not modifying it works, though. (yes, you have to adjust the /Yu option). Both are file if you stick to VC++, but are compatibility issues to compilers that don't "understand" VC++'s PCHs
peterchen