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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4845410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:44:41  来源:igfitidea点击:

error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

c++linker

提问by Caleb Jares

I don't know what's wrong with it.. I can't find where the error is, commenting out the implementation doesn't resolve the error either.

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

Header File

头文件

#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

Source

来源

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

}

回答by Caleb Jares

Even if your project has a main()method, the linker sometimes gets confused. You can solve this issue in Visual Studio 2010 by going to

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

Project -> Properties -> Configuration Properties -> Linker -> System

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

and changing SubSystemto Console.

并更改SubSystem为控制台。

回答by Anton Andreev

We also had this problem. My colleague found a solution. It turned up to be a redefinition of "main" in a third party library header:

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

#define main    SDL_main

So the solution was to add:

所以解决方案是添加:

#undef main

before our main function.

在我们的主要功能之前。

This is clearly a stupidity!

这分明是愚蠢的!

回答by engf-010

if you have _tmainfunction in your projects you need to include <tchar.h>.

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

回答by James McNellis

You need a main()function so the program knows where to start.

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

回答by G. Vanem

In case someone missed the obvious; note that if you build a GUI application and use
"-subsystem:windows" in the link-args, the application entry is WinMain@16. Not main(). Hence you can use this snippet to call your main():

万一有人错过了显而易见的事情;请注意,如果您构建 GUI 应用程序并在链接参数中使用
-subsystem:windows”,则应用程序条目为WinMain@16 。不是 main()。因此,您可以使用此代码段来调用您的 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);
}

回答by user5632040

If you are using Visual Studio. The reason you might be recieving this error may be because you originally created a new header file.h and then renamed it to file.cpp where you placed your main() function.

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

To fix the issue right click file.cpp -> click Properties go to
Configuration Properties -> General ->Item Type and change its value to C/C++ compiler instead of C/C++ header.

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

回答by ssmir

Did you implement the main()function?

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

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

[edit]

[编辑]

You have your main()in another source file so you've probably forgotten to add it to your project.

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

To add an existing source file: In Solution Explorer, right-click the Source Filesfolder, point to Add, and then click Existing Item. Now select the source file containing the main()

添加现有源文件: 在解决方案资源管理器中,右键单击Source Files 文件夹,指向Add,然后单击Existing Item。现在选择包含main()

回答by Daniel Timms

I had this problem despite:

尽管我遇到了这个问题:

  • having a main(); and
  • configuring all other projects in my solution to be static libraries.
  • 有一个main(); 和
  • 将我的解决方案中的所有其他项目配置为静态库。

My eventual fix was the following:

我的最终修复如下:

  • my main()was in a namespace, so was effectively called something::main()...removing this namespace fixed the problem.
  • main()在一个命名空间中,所以被有效地称为something::main()...删除这个命名空间解决了这个问题。

回答by Antony

I encountered the LNK2019 error while working on a DLL project in Visual Studio 2013.

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

I added a new configuration to the project. But instead of having the "Configuration Type" as "Dynamic Library", visual studio added it as "Application". This resulted in the LNK2019 error.

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

Fixed the LNK2019 error by going to Project -> Properties -> Configuration Properties -> General and changing "Configuration Type" to "Dynamic Library (.dll)" and "Target Extension" to ".dll".

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

Yes, the original question talks about a console/application project, which is a different problem than my answer. But I believe adding this answer might help someone (like me) that stumbles upon this thread.

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

回答by Andrew Shelansky

You appear to have no main function, which is supposed to be the entry-point for your program.

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