2010-07-21 39 views
1

这里是一个示例代码。如果我删除stdafx.h文件,程序将不会编译。使用opencv在C++程序中包含stdafx.h文件的目的是什么?

的stdafx.h文件

'#pragma once 

'#include "targetver.h" 

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

Capture.ccp

// Capture.cpp:定义控制台应用程序的入口点。 //

'#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; 
} 

什么是StdAfx.h文件的目的 - 这个文件的方式自动生成。

非常感谢

+0

请不要使用#pragma一次,它不是C++,它依赖于编译器。改为使用包含警卫(http://en.wikipedia.org/wiki/Include_guard)。 – Scharron 2010-07-21 12:47:47

+0

TKS Scharron我会做 – Haxed 2010-07-21 12:52:42

回答

2

这是编译器Precompiled Header的一部分,以加快构建。

+2

如果你想删除它,你需要修改该.cpp文件编译设置(或所有文件)删除了“使用预编译头”选项 - 这就是为什么它的失败。但没有理由。如果您的应用程序广泛使用它们以保存整个应用程序的重新编译,您还可以将更多标题移动到该应用程序中。 – Rup 2010-07-21 12:52:05

2

正如peterchen所说,它用于加速构建并添加一些Windows内容。这包括完全视觉工作室专用,如果您希望您的程序是跨平台的,那么Open Cv库(您可以在windows,linux,...下使用opencv)就不能使用。

什么是您的编译错误消息?

+1

特定于视觉工作室,而不是windows。你可以使用gcc和mingw你知道的;-) – Scharron 2010-07-21 12:45:58

+0

总的来说,我写得很快;)我现在编辑我的答案。 – 2010-07-21 12:46:35

+0

咦?它不会加快在其他平台上的构建,但没有理由会破坏任何东西。这只是一个头文件。即使GCC支持'#pragma Once' IIRC – Rup 2010-07-21 12:51:04

相关问题