c++ - 每次我要使用外部资源时,我都会得到 : collect2. exe: error ld returned 1 exit status

标签 c++ compiler-errors

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

-------------- Build: Debug in date (编译器: GNU GCC Compiler)---------------

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: file not recognized: File format not recognized collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minute(s), 1 second(s)) 1 error(s), 0 warning(s) (0 minute(s), 1 second(s))



日期.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;
}

主文件
#include "Date.h"

using namespace std;

int main()
{
    Date test(2000, 05, 10);
    return 0;
}





最佳答案

你不应该编译或链接头文件。
所以删除 Data.h.gch从你的链接器的命令,你会没事的。

如果它在您的 IDE 中自动为您完成,请禁用“预编译头”选项。

关于c++ - 每次我要使用外部资源时,我都会得到 : collect2. exe: error ld returned 1 exit status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105834/

相关文章:

java - 我对 Java 很陌生,这是我在类里面的第二课。可以使用任何帮助

ios - NSUserDefaults 的 NSMutableArray 错误

c# - 为什么我在 C# 编译器中收到此错误 "Unassigned Local Variable"

c++ - 在命名空间中转发定义类?

c++ - << 的运算符重载需要 const;产生头痛

C++ 优雅地预测大多数字符串数组

c++ - 理解 new[] 中的顺序

c++ - 使用 C++ 和 EasyHook 注入(inject) x64 进程 Hook x86-DLL 的 x64-DLL 失败

c++ - 通过引用捕获异常是否危险?

C++编译错误