简单的 C++ 程序在 MingW 下的 Windows 上没有链接 [英] Simple C++ program is not linking on Windows under MingW

查看:71
本文介绍了简单的 C++ 程序在 MingW 下的 Windows 上没有链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我不能链接这个程序.首先这是我的头文件 gcd.h:

I do not know why I cannot link this program. First off this is my header file, gcd.h:

#ifndef GCD_H
#define GCD_H

/**
 * Calculate the greatest common divisor of two integers.
 * Note: gcd(0,0) will return 0 and print an error message.
 * @param a the first integer
 * @param b the second integer
 * @return the greatest common divisor of a and b
 */

long gcd(long a, long b);

#endif

这是我的 gcd.cpp 文件:

And this is my gcd.cpp file:

#include "gcd.h"
#include <iostream>
using namespace std;

long gcd(long a, long b) {

    // if a and b are both zero, print an error and return 0
    if ( (a==0) && (b==0) ) {
        cerr << "WARNING: gcd called with both arguments equal to zero." << endl;
        return 0;
    }

    // Make sure a and b are both nonnegative
    if (a<0) {
        a = -a;
    }
    if (b<0) {
        b = -b;
    }

    // if a is zero, the answer is b
    if (a==0) {
        return b;
    }

    // otherwise, we check all the possibilities from 1 to a
    long d; // d will hold the answer

    for (long t=1; t<=a; t++) {
        if ( (a%t==0) && (b&t==0) ) {
            d = t;
        }
    }

    return d;
}

主要问题是当我编译时,它返回错误

The main problem is when I compile, it returns the error

c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0xd2):对WinMain@16"的未定义引用 collect2:ld 返回 1 个退出状态

c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16' collect2: ld returned 1 exit status

我不明白这是什么意思.

I don't understand what that means.

请帮忙?

好吧,实际上有人可以修改我的代码以使其正常运行吗?这是目前最好的选择,因为那样我就会真正明白我做错了什么.

Okay actually can someone just modify my code so it runs properly? That's the best bet at this point because then I'll actually understand what I did wrong.

推荐答案

你的 main 函数在哪里(程序的入口点..)?

where is your main function (the entry point of the program..)?

顺便说一句,我喜欢你写的主要问题":)

Btw I like that you wrote "the main problem" :)

这篇关于简单的 C++ 程序在 MingW 下的 Windows 上没有链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆