【发布时间】:2020-05-06 18:30:24
【问题描述】:
我尝试将代码从 Linux 移动到 Windows 时的问题描述:
- MinGW 上的 Windows 链接器问题
- 当我在 Main.cpp 中调用用户定义的类时发生 (如果我不在 Main 中调用用户定义的类构造函数,则可以正常工作)
相关代码示例:
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++编译步骤中遇到了Linker问题:
所有外部实体引用均已解析。库组件是 链接以满足对未在 当前翻译。所有这样的翻译输出都被收集到一个 程序映像,其中包含执行所需的信息 执行环境。
我尝试关注其他类似的问题,但不幸的是它们是不同的问题:
- C compiler error: undefined reference to function
- 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 中使用哪些标志?非常感谢
【问题讨论】:
-
那么,在您看来,析构函数的代码在哪里?编译器能看到吗?例如不在显示的代码中。
-
链接 2.,接受的答案,列表中的第二个链接,第四个代码块。
-
实际上,在链接中有一个触发此链接错误的原因列表。在我们的例子中,它应该是:stackoverflow.com/questions/12573816/… 不过,我不知道如何更改 Makefile 以引用 Person 类?