每次我要使用外部资源时,我都会得到:collect2。exe:错误 ld 返回 1 退出状态

Everytime I'm going to use external sources I get: collect2. exe: error ld returned 1 exit status

本文关键字:exe collect2 错误 ld 状态 退出 返回 我要 外部 资源      更新时间:2023-10-16

例如,这段代码,我在代码块中创建一个新项目,添加这些文件,启用头文件,当尝试运行它时,我得到:

-------------- 构建:调试日期(编译器:GNU GCC 编译器(---------------

mingw32-g++.exe -Wall -fexceptions -g -std=c++11 -I..\date -c "C:\Users\kairenne\Documents\programming\Chapter 8\monster_generation\date\main.cpp" -o obj\Debug\main.o mingw32-g++.exe -o bin\Debug\date.exe obj\Debug\Date.o Date.h.gch obj\Debug\main.o Date.h.gch:文件无法识别:文件格式无法识别 识别的 collect2.exe:错误:ld 返回 1 退出状态 进程 终止状态 1(0 分钟、1 秒( 1 错误、0 警告(0 分钟、1 秒(

日期.h

#define DATE_H
class Date
{
private:
int m_year;
int m_month;
int m_day;
public:
Date(int year, int month, int day);
void SetDate(int year, int month, int day);
int getYear() { return m_year; }
int getMonth() { return m_month; }
int getDay()  { return m_day; }
};
#endif

日期.cpp


// Date constructor
Date::Date(int year, int month, int day)
{
SetDate(year, month, day);
}
// Date member function
void Date::SetDate(int year, int month, int day)
{
m_month = month;
m_day = day;
m_year = year;
}

主.cpp

#include "Date.h"
using namespace std;
int main()
{
Date test(2000, 05, 10);
return 0;
}


永远不要编译或链接头文件。 因此,从链接器的命令中删除该Data.h.gch,您将没事的。

如果在 IDE 中自动为您完成,请禁用"预编译标头"选项。