mingw32/bin/ld.exe ... undefined reference to [class] ... collect2.exe: error: ld returned 1 exit status
mingw32/bin/ld.exe ... undefined reference to [class] ... collect2.exe: error: ld returned 1 exit status
我尝试将我的代码从 Linux 移动到 Windows 时的问题描述:
- MinGW 关于 Windows 链接器问题
- 当我在 Main.cpp 中调用用户定义的 class 时发生
(如果我不在 Main 中调用用户定义的 class 构造函数,则工作正常)
相关代码示例:
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)
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++ 编译步骤中遇到了链接器问题:
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.
我试图解决其他类似的问题,但不幸的是它们是不同的问题:
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- facing error in linking codes in c++
我应该如何更改我的 Makefile 或我的代码? MinGW 在 Windows 中使用了哪些标志?非常感谢
与我为
提供的答案相同
我遇到了和你一样的问题。尝试将构造函数和析构函数定义从 cpp 移动到头文件中。这样,仅通过 运行 您提到的简单 g++ 命令即可很好地完成链接,为此您不需要 Makefile。包含告诉您的编译器在哪里可以找到定义。
尝试:
MyClass(const string& className){ _className=className };
如果定义在 cpp 文件中,我会得到与您相同的错误。
我尝试将我的代码从 Linux 移动到 Windows 时的问题描述:
- MinGW 关于 Windows 链接器问题
- 当我在 Main.cpp 中调用用户定义的 class 时发生 (如果我不在 Main 中调用用户定义的 class 构造函数,则工作正常)
相关代码示例:
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)
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++ 编译步骤中遇到了链接器问题:
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.
我试图解决其他类似的问题,但不幸的是它们是不同的问题:
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- facing error in linking codes in c++
我应该如何更改我的 Makefile 或我的代码? MinGW 在 Windows 中使用了哪些标志?非常感谢
与我为
我遇到了和你一样的问题。尝试将构造函数和析构函数定义从 cpp 移动到头文件中。这样,仅通过 运行 您提到的简单 g++ 命令即可很好地完成链接,为此您不需要 Makefile。包含告诉您的编译器在哪里可以找到定义。
尝试:
MyClass(const string& className){ _className=className };
如果定义在 cpp 文件中,我会得到与您相同的错误。