【问题标题】:error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup错误 LNK2019:未解析的外部符号 _main 在函数 ___tmainCRTStartup 中引用
【发布时间】:2011-06-18 05:46:39
【问题描述】:

我不知道它有什么问题。我找不到错误在哪里,注释掉实现也不能解决错误。

头文件

#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib>  // Provides size_t

namespace main_savitch_3
{
    class sequence
    {
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef double value_type;
        typedef std::size_t size_type;
        static const size_type CAPACITY = 30;
        // CONSTRUCTOR
        sequence( );
        // MODIFICATION MEMBER FUNCTIONS
        void start( );
        void advance( );
        void insert(const value_type& entry);
        void attach(const value_type& entry);
        void remove_current( );
        // CONSTANT MEMBER FUNCTIONS
        size_type size( ) const;
        bool is_item( ) const;
        value_type current( ) const;
    private:
        value_type data[CAPACITY];
        size_type used;
        size_type current_index;
    };
}

#endif

来源

#include "sequence1.h"
#include <assert.h>

namespace main_savitch_3
{

    // Default constructer - sequence is empty
    sequence::sequence()
    {
        used = current_index = 0;
    }


    // Start the iteration
    void sequence::start()
    {
        current_index = 0;
    }
    // Iterate
    void sequence::advance()
    {
        current_index++;
    }


    // Number of items in the sequence
    sequence::size_type sequence::size() const
    {
        return used;
    }
    // Checks if there is a current item
    bool sequence::is_item() const
    {
        return current_index <= used && used > 0;
    }
    // Returns the current value
    sequence::value_type sequence::current() const
    {
        assert(is_item()); // no current item
        return data[current_index];
    }


    // Adds an item BEFORE the current index
    void sequence::insert(const value_type& entry)
    {
        assert(entry != 0); // pointer is invalid
        assert(current_index < sequence::CAPACITY); // no room to add an item

        // move items up - starting with the last item and working down to the current item
        // arrays start at 0, so the -1 adjusts it
        for (size_type i = used - 1; i >= current_index; i--)
            data[i + 1] = data[i];

        data[current_index] = entry;
    }
    // Adds an item AFTER the current index
    void sequence::attach(const value_type& entry)
    {
        assert(entry != 0); // pointer is invalid
        assert(current_index < sequence::CAPACITY); // no room to add an item

        // move items up - starting with the last item and working down to the current item
        // arrays start at 0, so the -1 adjusts it
        for (size_type i = used - 1; i > current_index; i--)
            data[i + 1] = data[i];

        if (current_index = 0)
            data[used] = entry;
        else
            data[current_index + 1] = entry;
    }
    // Removes the current item
    void sequence::remove_current()
    {
        for (size_type i = current_index; i < used; i++)
            data[i] = data[i + 1];
    }

}

【问题讨论】:

  • 这是一个链接错误。看起来您正在尝试构建没有 main() 函数的可执行文件?您应该构建一个库,或者您需要一个包含main() 的源文件。
  • Nitpick:如果你想要size_t,你应该使用&lt;cstddef&gt;而不是&lt;cstdlib&gt;
  • @Billy: size_t 在两个标头中都有定义。
  • @James:是的,但是&lt;cstddef&gt; 是“更小” :)
  • 我有类似的问题导致轻微的无知,没有选择 x64 构建,而包含的 lib 是 x64。

标签: c++ linker


【解决方案1】:

即使您的项目有main() 方法,链接器有时也会感到困惑。您可以在 Visual Studio 2010 中解决此问题,方法是转到

项目 -> 属性 -> 配置属性 -> 链接器 -> 系统

并将SubSystem 更改为控制台。

【讨论】:

  • SubSystem 已经为我设置为 Console 并且我有一个 main() 函数。你还有什么想法吗?
  • 尝试清理并重建您的解决方案。
  • 谢谢,问题是我有一个命名空间lol里面的主要功能。
  • 我有完全相反的(控制台设置而不是 Windows),所以我切换并且它工作。无论如何,您的解决方案帮助我走上了正轨。谢谢!
  • 我刚刚通过将 console 更改为 windows 解决了类似的错误 :)
【解决方案2】:

我们也遇到过这个问题。我的同事找到了解决方案。原来是在第三方库标头中重新定义了“main”:

#define main    SDL_main

所以解决方案是添加:

#undef main

在我们的主要功能之前。

这显然是愚蠢的!

【讨论】:

  • 谢谢,这也是我的罪魁祸首!不过,正确初始化 SDL 是一个更好的解决方案。
  • 嗯。这也对我有用。使用 SDL2.0.3 和 GLEW1.13.0。
  • 哦,在 SDL_main.h 中,有一条关于“在某些平台上重新定义 main() 以便 SDL 调用它”的注释。然后: #ifdef WIN32 #def SDL_MAIN_AVAILABLE #endif ... #if 定义(SDL_MAIN_NEEDED)|| defined(SDL_MAIN_AVAILABLE) #define main SDL_main #endif 正如@Csq 所说,似乎有更好的方法来初始化 SDL2。
  • @Nick Desaulniers 刚刚解决了这个问题并按照这个答案解决了。 “看起来有更好的方法来初始化 SDL2”是什么意思?谢谢。
  • 在你之前#include &lt;SDL.h&gt;,你可以#define SDL_MAIN_HANDLED
【解决方案3】:

如果您的项目中有_tmain 功能,您需要include &lt;tchar.h&gt;.

【讨论】:

  • 我在另外一个文件中有一个main函数,是教材提供的一个测试程序。 int main() {...} 我不知道该怎么做才能让程序识别它
【解决方案4】:

您需要一个main() 函数,以便程序知道从哪里开始。

【讨论】:

  • 我在另外一个文件中有一个main函数,是教材提供的一个测试程序。 int main() {...} 我不知道该怎么做才能让程序识别它
  • 现在编译和构建两个源文件,即g++ main.cpp sequence1.cpp -o result.out
  • @cable729:您是否已将两个 .cpp 文件添加到 Visual Studio 中的同一个项目中?
  • 我想通了,在项目属性中,在链接器下,它被设置为 Windows,而不是控制台。感谢您的帮助
  • @cable729:作为程序入口点的main函数必须在全局命名空间中。您不能将它放在任何其他命名空间中,也不能使用 using 指令将其他一些 main 函数带入全局命名空间。
【解决方案5】:

万一有人错过了明显的;请注意,如果您构建一个 GUI 应用程序并使用
链接参数中的“-subsystem:windows”,应用程序入口为WinMain@16。不是 main()。因此你可以使用这个 sn-p 来调用你的 main():

#include <stdlib.h>
#include <windows.h>

#ifdef __GNUC__
#define _stdcall  __attribute__((stdcall))
#endif

int _stdcall
WinMain (struct HINSTANCE__ *hInstance,
         struct HINSTANCE__ *hPrevInstance,
         char               *lpszCmdLine,
         int                 nCmdShow)
{
  return main (__argc, __argv);
}

【讨论】:

    【解决方案6】:

    如果您使用的是 Visual Studio。您可能会收到此错误的原因可能是因为您最初创建了一个新的头文件.h,然后将其重命名为 file.cpp,您在其中放置了 main() 函数。

    要解决此问题,请右键单击 file.cpp -> 单击属性 前往
    配置属性 -> 常规 -> 项目类型并将其值更改为 C/C++ 编译器而不是 C/C++ 头文件。

    【讨论】:

      【解决方案7】:

      你实现main()函数了吗?

      int main(int argc, char **argv) {
          ... code ...
          return 0;
      }
      

      [编辑]

      您的main() 在另一个源文件中,因此您可能忘记将其添加到您的项目中。

      添加现有源文件:在 Solution Explorer 中,右键单击 Source Files 文件夹,指向 Add,然后单击现有项目。现在选择包含main()的源文件

      【讨论】:

      • 我在另一个文件中有一个main函数,是教材提供的一个测试程序。 int main() {...} 我不知道该怎么做才能让程序识别它
      • @cable729 您可能使用 Visual C++。然后您应该将带有main() 的文件添加到项目的源文件列表中。另一种选择是将您的main() 复制并粘贴到您的 sequence1.cpp。
      【解决方案8】:

      尽管如此,我还是遇到了这个问题:

      • 有一个main();和
      • 将我的解决方案中的所有其他项目配置为静态库。

      我的最终修复如下:

      • 我的 main() 位于命名空间中,因此被有效地称为 something::main() ...删除此命名空间解决了问题。

      【讨论】:

        【解决方案9】:

        我在 Visual Studio 2013 中处理 DLL 项目时遇到了 LNK2019 错误。

        我向项目添加了新配置。但是,Visual Studio 没有将“配置类型”作为“动态库”,而是将其添加为“应用程序”。 这导致了 LNK2019 错误。

        通过转到项目 -> 属性 -> 配置属性 -> 常规并将“配置类型”更改为“动态库 (.dll)”并将“目标扩展”更改为“.dll”,修复了 LNK2019 错误。

        是的,最初的问题是关于控制台/应用程序项目,这与我的回答不同。但我相信添加这个答案可能会对偶然发现这个线程的人(比如我)有所帮助。

        【讨论】:

        • 有效!只要确保您使用的是正确的构建配置。该对话框中的 Debug 和 Release 都必须在 vs2015 中进行更改。
        • 另外,请确保将库路径添加到您的解决方案中
        【解决方案10】:

        您似乎没有 main 函数,它应该是您程序的入口点。

        【讨论】:

        • 我在另外一个文件中有一个main函数,是教材提供的一个测试程序。 int main() {...} 我不知道该怎么做才能让程序识别它
        【解决方案11】:

        在 Visual Studio 中,项目属性,用于 x86、x64、发布和调试配置

        链接器 > 系统 > 子系统

        /SUBSYSTEM:WINDOWS 需要以下 main (注意这是 unicode,宽字符版本):

        #include <Windows.h>
        
        int WINAPI  wWinMain(_In_ HINSTANCE hInstance,
            HINSTANCE hPrevInstance,
            LPWSTR    lpCmdLine,
            int       nCmdShow)
        {
           return 0;
        }
        

        对于/SUBSYSTEM:CONSOLE,需要以下主要内容:

        int main(int argc, char* argv[], char* environment[]){
              return 0;
        }
        

        【讨论】:

          【解决方案12】:

          我的问题是别的
          我在一个没有实现的类中有一个方法。
          像这样:

          class Hello {
              public:
                  // ...
                  void PrintHelloWorld();
                  // ...
          };
          

          当我调用该方法时出现错误:

          hello().PrintHelloWorld();
          

          错误:函数 _main demo .\Peyman\source\repos\demo\demo\demo 中引用的 LNK2019 无法解析外部符号“public: void __thiscall Hello::PrintHelloWorld(void)”(?PrintHelloWorld@Hello@@QAEXXZ)。对象 1

          事实上,我使用了一个没有实现的 SDK,它会导致错误。 请检查是否有任何实施。 void PrintHelloWorld(){} 带括号

          【讨论】:

            【解决方案13】:

            我的背景:Visual Studio

            当我添加扩展名错误的现有文件(如 *.ccc)时,我遇到了同样的问题。

            我已将扩展名更改为 *.cpp。 当我构建解决方案时,我遇到了类似的链接错误。

            我在查看 *.vcxproject 文件时发现了原因。该文件尚未被 Visual Studio 识别为源文件,因此将其放入 ClInclude 组(见下文)。

                <ItemGroup>
                    <ClInclude Include="toto.cpp" />
                </ItemGroup>
            

            我将它移到 ClCompile 组(见下文)并重建没有错误。

                <ItemGroup>
                    <ClCompile Include="AnotherSource.cpp" />
                    <ClCompile Include="toto.cpp" />
                </ItemGroup>
            
            

            【讨论】:

              【解决方案14】:

              对我来说,问题也是 SDL。 我搬家了
              // Tell SDL not to mess with main()#define SDL_MAIN_HANDLED 在包含 SDL 库之前。而且问题似乎已解决。

              【讨论】:

                【解决方案15】:

                转到“项目-属性-配置属性-链接器-输入-附加依赖项”,然后转到末尾并键入“;ws2_32.lib”。

                【讨论】:

                • 请在提交任何内容作为答案之前尝试理解问题。
                【解决方案16】:

                尝试使用 return 0;

                如果一直失败,请将您的解决方案平台更改为 64 倍而不是 86 倍 并转到配置管理器(那是您将 86x 更改为 64x) 并在平台中将其设置为 64 位

                对我有用,希望对你有用

                【讨论】:

                • 没有main 函数,OP 应该来自哪里return
                猜你喜欢
                • 2012-06-30
                • 2015-02-06
                • 2014-08-28
                • 2014-09-23
                • 1970-01-01
                • 2014-02-10
                • 2021-04-18
                • 2013-08-04
                相关资源
                最近更新 更多