这是一个编译错误,意思是在函数___tmainCRTStartup中引用了未解析的外部符号_main。可能是由于缺少源文件、库文件或链接器设置不正确等原因导致的。

70

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

头文件

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

}

9
这是一个链接错误。看起来你正在尝试构建一个没有 main() 函数的可执行文件?你应该构建一个库,或者需要一个包含 main() 函数的源文件。 - Paul R
@Billy:size_t 在两个头文件中都有定义。 - James McNellis
1
@James:是的,但 <cstddef> 更“小” :) - Billy ONeal
1
我曾经遇到过类似的问题,是由于小小的疏忽导致的,因为我没有选择 x64 构建而库文件却是 x64 的。 - Pervez Alam
谢谢。我正在进行汇编教程。除了带有代码的source.asm之外,还有一个空的source.cpp文件。只需将int main等内容添加到source.cpp文件中即可消除错误和致命错误。但现在它无法运行source.asm文件? - mortenlund
显示剩余2条评论
17个回答

88

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

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

然后将SubSystem更改为Console。


13
对我来说,子系统已经被设置为Console,并且我有一个main()函数。你有其他的想法吗? - rfcoder89
2
尝试清理并重建您的解决方案。 - Caleb Jares
15
谢谢,问题是我把主函数放在了一个命名空间里哈哈。 - rfcoder89
11
我之前使用的是控制台设置,与Windows相反,所以我进行了切换并且成功了。不过你的解决方案帮助我找到了正确的方向。谢谢! - Vahron
3
我刚刚通过将“console”更改为“windows”来解决类似的错误 :) - LCFactorization
显示剩余3条评论

51

我们也遇到了这个问题。我的同事找到了解决方案。原来是第三方库头文件中 "main" 的重新定义导致的。

#define main    SDL_main

所以解决方案是添加:

#undef main

在我们的主函数之前。

这显然很愚蠢!


谢谢,这也是我的问题所在!不过更好的解决方案是正确初始化SDL。 - Csq
哎呀,这个也对我起作用了。我使用的是SDL 2.0.3和GLEW1.13.0。 - Nick Desaulniers
2
哦,在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。 - Nick Desaulniers
1
@Nick Desaulniers 刚刚解决了这个问题,按照这个答案解决了。"看起来有更好的方法来初始化SDL2"是什么意思?谢谢。 - RigidBody
4
在你使用#include <SDL.h>之前,你可以使用#define SDL_MAIN_HANDLED - Mike
显示剩余2条评论

22
如果你的项目中有 _tmain 函数,你需要包含 <tchar.h>

我在另一个文件中有一个主函数,它是教科书提供的测试程序。 int main() {...} 我不知道该怎么做才能让程序识别它。 - Caleb Jares

17

你需要一个main()函数,这样程序才知道从哪里开始。


我在另一个文件中有一个主函数,它是教科书提供的测试程序。 int main() {...} 我不知道该怎么做才能让程序识别它。 - Caleb Jares
1
@cable729: 你是否已经将这两个.cpp文件添加到Visual Studio中同一个项目中了? - James McNellis
2
我找到了解决方法,在项目属性中,链接器下,它被设置为Windows,而不是Console。感谢您的帮助。 - Caleb Jares
啊,就是这样。那个总是会让你犯错。 - Andrew Shelansky
2
@cable729:作为程序的入口点,main函数必须位于全局命名空间中。您不能将其放在任何其他命名空间中,也不能使用using指令将其他main函数引入全局命名空间。 - James McNellis
显示剩余3条评论

8

如果有人没有注意到,注意一下,如果你构建一个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);
}


7

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

要解决此问题,请右键单击file.cpp -> 单击属性

转到配置属性 -> 常规 -> 项类型,并将其值更改为 C/C++编译器 而不是 C/C++头文件。


7
我在使用Visual Studio 2013开发DLL项目时遇到了LNK2019错误。
我向项目中添加了新的配置。但是,Visual Studio将“Configuration Type”设置为“Application”,而不是“Dynamic Library”。
这导致了LNK2019错误。
通过进入“Project -> Properties -> Configuration Properties -> General”并将“Configuration Type”更改为“Dynamic Library (.dll)”,将“Target Extension”更改为“.dll”,解决了LNK2019错误。
原问题中提到的是控制台/应用程序项目,这是一个不同的问题。但我认为添加这个答案可能会帮助一些人(像我一样)在查看该线程时遇到相似问题。

2
可以了!只要确保你使用了正确的构建配置。在vs2015中,该对话框中的Debug和Release都必须更改。 - Edza
另外,请确保将库路径添加到您的解决方案中。 - CocoCrisp

6
你是否实现了main()函数?
int main(int argc, char **argv) {
    ... code ...
    return 0;
}

[编辑]

你的main()函数位于另一个源文件中,因此你可能忘记将它添加到项目中了。

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


1
我在另一个文件中有一个主函数,它是教科书提供的测试程序。 int main() {...} 我不知道该怎么做才能让程序识别它。 - Caleb Jares
@cable729 你可能使用的是Visual C++。那么你应该将带有main()函数的文件添加到项目源文件列表中。另一个选项是将你的main()函数复制并粘贴到sequence1.cpp文件中。 - ssmir

6

尽管我:

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

但我仍然遇到了这个问题。我最终解决方法如下:

  • 我的main()在一个命名空间中,因此实际上被称为something :: main()……删除这个命名空间就解决了这个问题。

1
在Visual Studio中,项目属性针对x86、x64、Release和Debug配置进行设置。
连接器 > 系统 > 子系统
对于“/SUBSYSTEM:WINDOWS”,需要以下主函数(请注意,这是Unicode宽字符版本):
#include <Windows.h>

int WINAPI  wWinMain(_In_ HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPWSTR    lpCmdLine,
    int       nCmdShow)
{
   return 0;
}
< p>对于 /SUBSYSTEM:CONSOLE,需要以下主要内容:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接