【问题标题】:Undefined reference to WinMain@16 - Codeblocks未定义对 WinMain@16 的引用 - 代码块
【发布时间】:2015-06-06 22:13:06
【问题描述】:

我已阅读:undefined reference to `WinMain@16' & 仍然不明白我的问题。

我有一个正在运行的程序。添加了一个类,但尚未将其实现到程序中,只是编写了头文件和 .cpp 文件。之前添加这个类的程序可以工作,现在不行了。

错误状态... **文件地址....libmingw.32.a(main.o):main.c:(.text.startup+0xa7)

头文件

        #ifndef DATE_H
        #define DATE_H

        #include <iostream>
        #include <string>

        class Date
        {
            public:
                Date();

                //Setters
                void SetDay(unsigned dy);

                void SetMonth(std::string month);

                void SetYear(unsigned mnth);

                //Getters
                unsigned GetDay() const;

                std::string GetMonth() const;

                unsigned GetYear() const;

            private:
                unsigned day, year;
                std::string month;
        };

        #endif // DATE_H

.cpp 文件

        #include "Date.h"

        Date::Date()
        {
            day = 0;
            year = 0;
            month = "Not Set";
        }

        //Setters
        void Date::SetDay(unsigned dy)
        {
            day = dy;
        }

        void Date::SetMonth(std::string mnth)
        {
            month = mnth;
        }

        void Date::SetYear(unsigned yr)
        {
            year = yr;
        }

        //Getters
        unsigned Date::GetDay() const
        {
            return day;
        }

        unsigned Date::GetYear() const
        {
            return year;
        }

        std::string Date::GetMonth() const
        {
            return month;
        }

我调用它的 main ,只是因为我不确定错误是因为它没有被调用还是类似的原因:

        #include <iostream>
        #include <fstream>
        #include "unit.h"  // Unit class declaration
        #include "regist.h"  // Registration class declaration
        #include "date.h"

        using namespace std;

        // Main program:
        // Open an input file stream, read a Registration object,
        // including its list of courses. Redisplay all information,
        // and calculate the total credits taken. Write the results
        // to a file stream.


        int main()
        {
            ifstream infile( "testin.txt" );
            if( !infile ) return -1;

            Registration R;
            Date D;
            infile >> R;

            ofstream ofile( "testout.txt" );

            ofile   << R;

            cout << "\nComputation completed. Check output file. \n";
            cout << "\n Day is " << D.GetDay;

            return 0;
        }

Day 没有在与注册相关的重载 >> 运算符中设置。它由基本构造函数设置。

同样,我还没有将这个类添加到程序中,因为它将是我只是在编写并以基本测试方式添加后尝试编译。通过 main 进行测试。

提前致谢。 =D

【问题讨论】:

  • 所以它只适用于一个主要功能,但不适用于添加的类。您确定将这两个 .cpp 文件编译并链接在一起吗?
  • 即使该类没有被链接并且它只是项目的一部分但没有被使用。它仍然有同样的错误。
  • 是否可能已经有一个名为 date.h 的库,因此出现错误?
  • 这不应该导致缺少主要功能。我唯一真正拥有的是主文件没有被链接。我之前肯定用过 int main() 和 Windows 子系统和 MinGW。

标签: c++ gcc compiler-errors winmain


【解决方案1】:

将你的项目设置下的应用类型改为Console。 WinMain16与Windows图形应用有关。我相信您确实需要设置一个特殊的预处理器标志或包含某种库以使其正常工作,如果您将其保留为 Windows 图形应用程序,但在这种情况下,最简单的解决方法是获取控制台应用程序.

另外,添加 -mwindows 标志可能会对您有所帮助。

【讨论】:

  • 是PE头中的一个标志,由-Wl,--subsystem,windows设置。
  • OP 包含的链接解释了为什么这不是必需的。
【解决方案2】:

问题是您的新项目已创建为Win32 GUI project,而它本应创建为Console application。前者要求存在具有签名int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 的函数,而后者需要C 或C++ 项目采用的常用形式之一,即int main()int main(char *argv[], int argc)

要么创建一个控制台类型的新项目并将代码复制到其中,要么使用“胶带”解决方案,并将 int main() 更改为 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 您还需要导航到项目的属性并更改从GUI applicationConsole application 的构建目标 - 不这样做意味着您将看不到任何输出,因为您使用的是 cout,它会打印到控制台显示的stdout。此窗口始终可用于控制台应用程序,但仅可用于 GUI 应用程序的调试版本。

我建议正确执行并创建一个适当类型的新项目,即Console application。 :)

【讨论】:

  • 无论如何要检查项目是做什么的?我正在使用别人给我的项目。
  • 另外,为什么它以前会起作用,而当我添加日期时不起作用。在“它被创建为 Win32 GUI 项目”的假设下?
  • 两者都不确定,抱歉。 :|
  • OP 包含的链接解释了为什么这不是必需的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2021-11-18
  • 2013-01-15
相关资源
最近更新 更多