无法打开输出文件:用C ++构建

时间:2011-01-31 23:34:14

标签: c++ eclipse build

我正在尝试学习C ++,所以我在Windows中编写了一个无限循环程序,要求用户输入两个整数并读出这些数字的正弦和正切值,以及第一个整数是否为多个第二个。

Program1Math.h

#ifndef PROGRAM1MATH_H_
#define PROGRAM1MATH_H_
class Program1Math {
public:
  Program1Math(int, int);
  void calculateSine(int);
  void calculateTangent(int);
  void calculateModulus();
};
#endif

Program1Math.cpp

#include "Program1Math.h"
#include < iostream >
#include < math.h >
using namespace std;
int c;
int d;
Program1Math::Program1Math(int a, int b)
{ 
  c=a;
  d=b; 
}
void Program1Math::calculateSine(int a)
{ 
  cout<<"\nSine("<< a <<")\t=\t"<< sin(a); 
}
void Program1Math::calculateTangent(int a)
{ 
  cout<<"\nTan("<< a <<")\t=\t"<< tan(a); 
}
void Program1Math::calculateModulus()
{
  if (c%d==0)
  {
    cout<<"\n"<< c <<" is a multiple of "<< d <<"!"; 
  }
  else
  {
    cout<<"\n"<< c <<" is not a multiple of "<< d <<"."; 
  }
}

Program1.cpp

#include < iostream >
#include "Program1Math.h"
using namespace std;
int main()
{
  int num1;
  int num2;
  int i=1;

  while (i>0){
    cout<<"Please enter the first integer number:\n";
    cin>>num1;
    cout<<"Please enter the second integer number:\n";
    cin>>num2;
    Program1Math p(num1, num2);
    p.calculateModulus();
    p.calculateSine(num1);
    p.calculateTangent(num1);
    p.calculateSine(num2);
    p.calculateTangent(num2);
    cout<<"\n\n";
  }
  return 0; 
}

该程序将在Eclipse中正确构建和运行(除了calculateTangent函数的格式化问题)。但是,我无法让程序在Unix环境中运行。该程序将构建,但当我尝试运行它时,我收到此错误消息:

Program1Math: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.text+0x0): first defined here
Program1Math: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o:(.fini+0x0): first defined here
Program1Math:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
Program1Math: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.data+0x0): first defined here
Program1Math:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o:(.rodata+0x0): first defined here
Program1Math: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
Program1Math:(.dtors+0x8): first defined here
collect2: ld returned 1 exit status

有谁知道问题可能是什么?

1 个答案:

答案 0 :(得分:1)

您的构建设置一定存在问题。首先,我将变量 c d 移动到 Program1Math.h 文件中。

Program1Math.h

#ifndef PROGRAM1MATH_H_
#define PROGRAM1MATH_H_

class Program1Math {
  private:   
    int c;
    int d;

  public:
    Program1Math(int, int);
    void calculateSine(int);
    void calculateTangent(int);
    void calculateModulus();
};
#endif

然后创建一个名为 Makefile 的文件,并将以下内容添加到其中:

生成文件

all:
    g++ program1.cpp Program1Math.cpp -o Program1 -Wall

将Makefile保存在与其他文件相同的目录中。 您现在可以以这种方式构建和运行:

> make
> ./Program1