错误LNK2019:函数___tmainCRTStartup中引用了未解析的外部符号_main

Cal*_*res 58 c++ linker

我不知道它有什么问题.我无法找到错误的位置,注释掉实现并没有解决错误.

头文件

#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
Run Code Online (Sandbox Code Playgroud)

资源

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

}
Run Code Online (Sandbox Code Playgroud)

Cal*_*res 72

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

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

并更改SubSystem 为控制台.

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

Ant*_*eev 37

我们也有这个问题.我的同事找到了解决方案.它变成了第三方库标题中"main"的重新定义:

#define main    SDL_main
Run Code Online (Sandbox Code Playgroud)

所以解决方案是添加:

#undef main
Run Code Online (Sandbox Code Playgroud)

在我们的主要功能之前

这显然是愚蠢的!

  • 在你`#include &lt;SDL.h&gt;` 之前,你可以`#define SDL_MAIN_HANDLED`。 (3认同)
  • 哦,在SDL_main.h中,有一个关于“在某些平台上重新定义main()以便它被SDL调用”的注释。然后: #ifdef __WIN32__ #def SDL_MAIN_AVAILABLE #endif ... #if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE) #define main SDL_main #endif 正如@Csq 所说,似乎有更好的方法来初始化 SDL2。 (2认同)

eng*_*010 22

如果您_tmain在项目中有功能,则需要include <tchar.h>.


Jam*_*lis 16

你需要一个main()函数,所以程序知道从哪里开始.

  • 我想通了,在项目属性中,在Linker下,它被设置为Windows而不是Console.谢谢您的帮助 (2认同)
  • @cable729:作为程序入口点的“main”函数必须位于全局命名空间中。您不能将其放在任何其他命名空间中,也不能使用 using 指令将其他“​​main”函数引入全局命名空间。 (2认同)

小智 9

万一有人错过了明显的事; 请注意,如果您构建GUI应用程序并在link-args中使用
" -subsystem:windows ",则应用程序条目为WinMain @ 16 .不是主().因此,您可以使用此代码段来调用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);
}
Run Code Online (Sandbox Code Playgroud)


ssm*_*mir 6

你实现了这个main()功能吗?

int main(int argc, char **argv) {
    ... code ...
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

[编辑]

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

添加现有源文件:在" 解决方案资源管理器"中,右键单击" 源文件"文件夹,指向" 添加",然后单击" 现有项".现在选择包含的源文件main()


小智 6

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

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


小智 5

尽管我遇到了这个问题:

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

我的最终修复如下:

  • main()在一个命名空间中,所以被有效地称为something::main()...删除这个命名空间解决了这个问题。


Ant*_*ony 5

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

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

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

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

  • 作品!只要确保您使用正确的构建配置。该对话框中的 Debug 和 Release 都必须在 vs2015 中更改。 (2认同)