问题描述
当我尝试将代码从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:
我尝试关注其他类似问题,但不幸的是,它们是不同的问题:
I tried to follow other similar problems, but they are unfortunately different issues:
- 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中使用哪些标志?非常感谢
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退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!