Communication error while using MinGW compiler (cannot find __main)

I am trying to compile a very simple program with MinGW on Windows, but I still have link errors. The program you need to compile is just a C ++ welcome world.

Z:\dev>type test.cpp
#include <iostream>

int main() {
  std::cout << "Hello World!\n";
  return 0;
}

Of course, just using MinGW g ++ is fine.

Z:\dev>g++ test.cpp -o test.exe
Z:\dev>test.exe
Hello World!

However, I tried to separate compilation and binding, but could not.

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'

Obviously, I missed some libraries. So, I tried to link several MinGW libraries, but still not as good as -lmsvcrt. I also did lstdc++, but still __mainmany warning messages could not be found.

Could you help me which libraries should be linked to each other?

+2
source share
3 answers

ld g++ .

:

Z:\dev> g++ -c test.cpp -o test.o
Z:\dev> g++ -o test.exe test.o
+6

ld , , . g++ , .

g++ -o test.exe test.o
+1

use g++to call the linker e.g.

g ++ test.o -o test.exe

Cheers and hth.,

+1
source

All Articles