我试图在Windows上使用MinGW编译一个非常简单的程序,但我仍然遇到链接错误.要编译的程序只是C++ hello world.
Z:\dev>type test.cpp
#include <iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当然,只需使用MinGW的g ++即可.
Z:\dev>g++ test.cpp -o test.exe
Z:\dev>test.exe
Hello World!
Run Code Online (Sandbox Code Playgroud)
但是,我试图分离编译和链接,但失败了.
Z:\dev>g++ -c test.cpp -o test.o
Z:\dev>ld test.o -o test.exe
test.o:test.cpp:(.text+0xa): undefined reference to `__main'
test.o:test.cpp:(.text+0x19): undefined reference to `std::cout'
test.o:test.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& s
<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.o:test.cpp:(.text+0x37): undefined reference to `std::ios_base::Init::~Init()'
test.o:test.cpp:(.text+0x5a): undefined reference to `std::ios_base::Init::Init()'
test.o:test.cpp:(.text+0x66): undefined reference to `atexit'
Run Code Online (Sandbox Code Playgroud)
很明显,我错过了一些图书馆.所以,我试图链接几个MinGW的库,但仍然没有好处,如-lmsvcrt.我也做了lstdc++,但仍然 …