使用MinGW编译器时出现链接错误(找不到__main) [英] Link error while using MinGW compiler (can't find __main)
问题描述
我正在尝试使用Windows上的MinGW编译一个非常简单的程序,但仍然出现链接错误.要编译的程序只是C ++的hello world.
I'm trying to compile a very simple program with MinGW on Windows, but I still got link errors. The program to be compiled is just C++ hello world.
Z:\dev>type test.cpp
#include <iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
当然,仅使用MinGW的g ++即可.
Of course, just using MinGW's g++ is okay.
Z:\dev>g++ test.cpp -o test.exe
Z:\dev>test.exe
Hello World!
但是,我尝试将编译和链接分开,但是失败了.
However, I tried to separate compilation and linking, but failed.
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'
很明显,我错过了一些图书馆.因此,我尝试与几个MinGW的库进行链接,但-lmsvcrt
这样的库还是不好用.我也做了lstdc++
,但是仍然找不到__main
和大量警告消息.
It is obvious that I've missed some libraries. So, I tried to link with several MinGW's libraries, but still no good such as -lmsvcrt
. I also did lstdc++
, but still __main
cannot be found and tons of warning messages.
您能帮助我将哪些库链接在一起吗?
Could you help me out which libraries should be linked together?
推荐答案
而不是使用ld
,请尝试使用g++
进行链接.
Instead of using ld
, try using g++
to link.
尝试一下:
Z:\dev> g++ -c test.cpp -o test.o
Z:\dev> g++ -o test.exe test.o
这篇关于使用MinGW编译器时出现链接错误(找不到__main)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!