mingw32/bin/ld.exe ...对[class]的未定义引用... collect2.exe:错误:ld返回1退出状态 [英] mingw32/bin/ld.exe ... undefined reference to [class] ... collect2.exe: error: ld returned 1 exit status

查看:330
本文介绍了mingw32/bin/ld.exe ...对[class]的未定义引用... collect2.exe:错误:ld返回1退出状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将代码从Linux迁移到Windows时的问题描述:

Problem description while I am trying to move my code from Linux to Windows:

  • MinGW在Windows链接器上的问题
  • 当我在Main.cpp中调用用户定义的类时发生 (如果我不在Main中调用用户定义的类构造函数,效果很好)
  • MinGW on Windows linker problems
  • happens when I am calling a user-defined class inside my Main.cpp ( works fine if I do not call the user-defined class constructor in Main )

相关代码示例:

Person.hpp

Person.hpp

class Person
{
    public:
        Person(const string& iName, 
                const list<string>& iContactDetails,
               ); 

        virtual ~Person(); 
        ...

Main.cpp

#include "Person.hpp"
...
int main()
{
...
Person myPerson = Person (myName, myContactDetails); //here is the linker problem
...
return 0;
}

编译命令:

独立

g++ -o MyProgram.exe Main.cpp -Wall

Makefile(甚至尝试使用CC代替CXX,或者使用LDFLAGS = -lgdi32代替CXXFLAGS)

Makefile (tried even CC instead of CXX, or LDFLAGS=-lgdi32 instead of CXXFLAGS)

EXECUTABLE = MyProgram.exe

CXX = "C:\MinGW\bin\g++.exe"
CXXFLAGS = -Wall      // tried LDFLAGS=-lgdi32 as well

src = $(wildcard *.cpp)
obj = $(src:.cpp=.o)

all: myprog

myprog: $(obj)
    $(CXX) -o $(EXECUTABLE) $^ $(CXXFLAGS)

.PHONY: clean
clean:
    del $(obj) $(EXECUTABLE)

错误:

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\....:Main.cpp:(.text+0x124c): undefined reference to `Person::~Person()'
collect2.exe: error: ld returned 1 exit status

作为总结,我在MinGW G ++编译步骤中遇到了Linker问题:

As a summary, I encounter Linker problems during the MinGW G++ compilation step:

所有外部实体引用均已解决.库组件是 链接以满足外部引用中未定义的实体 当前的翻译.所有这样的翻译器输出都被收集到一个 程序映像,其中包含执行所需的信息 执行环境.

All external entity references are resolved. Library components are linked to satisfy external references to entities not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment.

我尝试关注其他类似问题,但不幸的是,它们是不同的问题:

I tried to follow other similar problems, but they are unfortunately different issues:

  1. C编译器错误:未定义对函数的引用
  2. 什么是未定义的引用/未解决的外部符号错误,如何解决?
  3. 在c ++中链接代码时面临错误
  1. C compiler error: undefined reference to function
  2. What is an undefined reference/unresolved external symbol error and how do I fix it?
  3. facing error in linking codes in c++

如何更改我的Makefile或代码? MinGW在Windows中使用哪些标志?非常感谢

How should I change my Makefile or my code? What flags does MinGW uses in Windows? Thank you very much

推荐答案

与我为我和你有同样的问题.尝试将构造函数和析构函数定义从cpp移到头文件中.这样,仅通过运行您提到的简单g ++命令即可很好地完成链接,而您不需要Makefile.包括告诉您的编译器在哪里可以找到定义.

I had the same problem as you. Try to move your constructor and destructor definition from the cpp into the header file. This way, the linkage is well done only by running the simple g++ command that you mentioned, you do not need a Makefile for this. The includes tell your compiler where to find the definitions.

尝试:

MyClass(const string& className){ _className=className };

如果该定义位于cpp文件中,则会收到与您相同的错误.

If the definition is inside the cpp file, I get the same error as you have.

这篇关于mingw32/bin/ld.exe ...对[class]的未定义引用... collect2.exe:错误:ld返回1退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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