【问题标题】:Unexpected end of file error文件错误意外结束
【发布时间】:2012-02-10 09:08:12
【问题描述】:

我希望你能帮助我,因为我不知道发生了什么。我在尝试将 Beecrypt 库添加到我的项目时遇到以下错误:

致命错误 C1010:查找预编译头时文件意外结束。您是否忘记在源代码中添加“#include "stdafx.h""?

其实我并没有忘记将#include "stdafx" 添加到我的源代码中。编译器将错误指向此 .cxx 文件的末尾:

#define BEECRYPT_CXX_DLL_EXPORT

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "beecrypt/c++/security/SecureRandom.h"
#include "beecrypt/c++/security/SecureRandomSpi.h"
#include "beecrypt/c++/security/Security.h"

using namespace beecrypt::security;

SecureRandom* SecureRandom::getInstance(const String& algorithm) throw       (NoSuchAlgorithmException)
 {
Security::spi* tmp = Security::getSpi(algorithm, "SecureRandom");

assert(dynamic_cast<SecureRandomSpi*>(tmp->cspi));

SecureRandom* result = new SecureRandom(reinterpret_cast<SecureRandomSpi*>(tmp->cspi), tmp->prov, tmp->name);

delete tmp;

return result;
}

 SecureRandom* SecureRandom::getInstance(const String& type, const String& provider) throw (NoSuchAlgorithmException, NoSuchProviderException)
  {
Security::spi* tmp = Security::getSpi(type, "SecureRandom", provider);

assert(dynamic_cast<SecureRandomSpi*>(tmp->cspi));

SecureRandom* result = new SecureRandom(reinterpret_cast<SecureRandomSpi*>(tmp->cspi), tmp->prov, tmp->name);

delete tmp;

return result;
    }

   SecureRandom* SecureRandom::getInstance(const String& type, const Provider& provider) throw (NoSuchAlgorithmException)
   {
Security::spi* tmp = Security::getSpi(type, "SecureRandom", provider);

assert(dynamic_cast<SecureRandomSpi*>(tmp->cspi));

SecureRandom* result = new SecureRandom(reinterpret_cast<SecureRandomSpi*>(tmp->cspi), tmp->prov, tmp->name);

delete tmp;

return result;
     }

  void SecureRandom::getSeed(byte* data, int size)
 {
entropyGatherNext(data, size);
 }

 SecureRandom::SecureRandom()
 {
Security::spi* tmp = Security::getFirstSpi("SecureRandom");

assert(dynamic_cast<SecureRandomSpi*>((SecureRandomSpi*) tmp->cspi));

_rspi = (SecureRandomSpi*) tmp->cspi;
_type = tmp->name;
_prov = tmp->prov;

delete tmp;
   }

  SecureRandom::SecureRandom(SecureRandomSpi* rspi, const Provider* provider, const String& type)
  {
_rspi = rspi;
_prov = provider;
_type = type;
  }

 SecureRandom::~SecureRandom()
 {
delete _rspi;
 }

void SecureRandom::generateSeed(byte* data, int size)
 {
_rspi->engineGenerateSeed(data, size);
 }

 void SecureRandom::setSeed(const byte* data, int size)
 {
_rspi->engineSetSeed(data, size);
 }

  void SecureRandom::nextBytes(byte* data, int size)
 {
_rspi->engineNextBytes(data, size);
 }

 const String& SecureRandom::getType() const throw ()
 {
return _type;
 }

  const Provider& SecureRandom::getProvider() const throw ()
 {
return *_prov;
  }

这里是 h 文件:

#ifndef _CLASS_BEE_SECURITY_SECURERANDOM_H
#define _CLASS_BEE_SECURITY_SECURERANDOM_H

#include "beecrypt/beecrypt.h"

#ifdef __cplusplus

#include "beecrypt/c++/security/SecureRandomSpi.h"
using beecrypt::security::SecureRandomSpi;
#include "beecrypt/c++/security/Provider.h"
using beecrypt::security::Provider;
#include "beecrypt/c++/security/NoSuchAlgorithmException.h"
using beecrypt::security::NoSuchAlgorithmException;
#include "beecrypt/c++/security/NoSuchProviderException.h"
using beecrypt::security::NoSuchProviderException;

 namespace beecrypt {
namespace security {
    /*!\ingroup CXX_SECURITY_m
     */
    class BEECRYPTCXXAPI SecureRandom : public Object
    {
    public:
        static SecureRandom* getInstance(const String& type)    throw (NoSuchAlgorithmException);
        static SecureRandom* getInstance(const String& type,    const String& provider) throw (NoSuchAlgorithmException, NoSuchProviderException);
        static SecureRandom* getInstance(const String& type,   const Provider& provider) throw (NoSuchAlgorithmException);

        static void getSeed(byte*, int);

    private:
        SecureRandomSpi* _rspi;
        const Provider*  _prov;
        String           _type;

    protected:
        SecureRandom(SecureRandomSpi* spi, const Provider*   provider, const String& type);

    public:
        SecureRandom();
        virtual ~SecureRandom();

        void generateSeed(byte*, int);
        void nextBytes(byte*, int);
        void setSeed(const byte*, int);

        const String& getType() const throw ();
        const Provider& getProvider() const throw ();
    };
}
   }

   #endif

   #endif

抱歉,写了这么多代码。

【问题讨论】:

  • 您是否尝试将 Beecrypt 库作为单独的 project 添加到您的 solution
  • 可能是,我不知道你到底是什么意思。
  • 但是,如果您有成功将 Beecrypt 添加到解决方案的经验,那么我非常需要您的建议。如果你说俄语(我猜是你的名字)——也许用俄语会更方便?
  • 我没有这方面的经验,也不用VS了。但我记得有某种设置可以将这个预编译的头文件功能转换为单独的项目。
  • 我不明白那是什么

标签: c++ visual-studio-2008 visual-c++ compiler-errors precompiled-headers


【解决方案1】:

您确实忘记在源代码中包含stdafx.h(因为我看不到您的代码)。如果没有,请确保 #include "stdafx.h".cpp 文件中的 first 行,否则即使在源文件中包含 "stdafx.h" 也会看到相同的错误(但不在文件的最开头)。

【讨论】:

  • 当我把它放在第一行时,我变得无法包含“beecrypt/c++/security/SecureRandom.h”。它告诉我没有这样的文件或目录
  • @AndreyChernukha 不过,这是一个单独的问题,不会通过省略 #include &lt;stdafx.h&gt; 来解决!编译器在找到此标头之前不会对您的代码执行任何操作。它没有去寻找你的标题。如果不想包含&lt;stdafx.h&gt;,则需要关闭预编译头文件。
  • 我怎么能在我的 main.cpp 文件中打开那个 ("beecrypt/c++/security/SecureRandom.h") 文件,但不能在 SecureRandom.cxx 中打开?
【解决方案2】:

#include "stdafx.h"必须是每个源文件顶部的第一行,在包含任何其他头文件之前。

如果您显示的是整个 .cxx 文件,那么您确实忘记在该文件中包含 stdafx.h

【讨论】:

    【解决方案3】:

    Goto SolutionExplorer(应该已经可见,如果不使用菜单:View->SolutionExplorer)。

    在解决方案树中找到您的 .cxx 文件,右键单击它并从弹出菜单中选择“属性”。您将获得带有文件属性的窗口。

    使用左侧的树转到“C++/预编译头”部分。在窗口的右侧,您将获得三个属性。将名为“Create/Use Precompiled Header”的属性设置为“Not Using Precompiled Headers”的值。

    【讨论】:

    • 我在关注Walkthrough: Creating and Using a Dynamic Link Library (C++) 并且将“清除预编译头复选框”误读为“检查预编译头复选框”!呵呵。
    • 我以为这是整个项目的设置,没有意识到单个文件可以有不同的预编译头选项。这工作,谢谢!
    • 但是有谁知道为什么会发生错误,以及为什么实际上包含 stdafx.h 时会出现此错误?
    • 这也为我解决了这个问题,因为我有一个使用预编译头的 .cpp 和一个不使用预编译头的 .c。
    • 每个单个文件都可以有自己的预编译头的提示很好。但是,在我看来,这只是一种解决方法。
    【解决方案4】:

    在命名新的 Win32 控制台应用程序后,我忘记从向导的附加选项中取消选中预编译标头时遇到了该错误。

    因为我不需要 stdafx.h 库,所以我通过转到 Project 菜单将其删除,然后单击 Properties[我们项目的名称]属性 或直接按 Alt + F7。在配置旁边的下拉列表中,选择所有配置。在其下方,是一个树节点,点击Configuration Properties,然后点击C/C++。在右侧窗格中,选择Create/Use Precompiled Header,然后选择Not using Precompiled Header

    【讨论】:

      【解决方案5】:

      我也遇到了这个错误,但是对于 .h 文件。修复方法是进入文件Properties(通过解决方案资源管理器的文件弹出菜单)并正确设置文件类型。它被设置为C/C++ Compiler,而不是正确的C/C++ header

      【讨论】:

      • 我遇到了同样的问题,您的解决方案解决了!,我认为当您创建一个 .cpp 文件然后将其重命名为头文件时会发生这种情况..
      • 谢谢你们,伙计们!当你错误地创建一个.cpp文件然后将它重命名为.h文件时,确实存在这个问题。
      【解决方案6】:

      将您的 C++ 项目的平台更改为“x64”(或您的目标平台)而不是“Win32”。这可以在 Visual Studio 中的 Build -> Configuration Manager 下找到。在列表中找到您的项目并更改平台列。不要忘记对所有解决方案配置执行此操作。

      【讨论】:

      • 有效,为什么有效?其他合理的解决方案没有奏效,但这个解决方案做到了,目前还没有明显的原因或解释。
      【解决方案7】:

      如果您在项目中不使用预编译头文件,请将源文件的创建/使用预编译头文件属性设置为不使用预编译头文件。要设置此编译器选项,请按以下步骤操作:

      • 在项目的解决方案资源管理器窗格中,右键单击项目名称,然后单击Properties
      • 在左侧窗格中,单击C/C++ 文件夹。
      • 单击Precompiled Headers 节点。
      • 在右侧窗格中,单击Create/Use Precompiled Header,然后单击Not Using Precompiled Headers

      【讨论】:

        猜你喜欢
        • 2018-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-16
        • 2017-06-08
        • 2019-07-31
        • 2013-04-13
        相关资源
        最近更新 更多