There's a C++ code:
#include <stdio.h>
int main() {
int b = sizeof('a');
if(b==4) printf("I'm a C program!
");
else printf("I'm a C++ program!
");
}
Compile it like this:
gcc main.cpp -o main
It succeeds and gives:
I'm a C++ program!
Then add a line somewhere inside function main
int *p1 = new int [1000];
It fails with:
C:Users...AppDataLocalTempcccJZ8kN.o:main1.cpp:(.text+0x1f): undefined reference to operator new[](unsigned long long)'
collect2.exe: error: ld returned 1 exit status
Then the following two commands successfully compile the code:
gcc main.cpp -o main -lstdc++
and
g++ main.cpp -o main
The compiler is minGW-win64 (http://mingw-w64.sourceforge.net/).
The questions are:
- Which of the two last commands are better?
- To my mind
gcc
correctly chooses the right compiler but then uses a wrong linker. Is it right?
- May it be a problem in minGW-win64?
As I see (correct me if it's wrong) gcc
was intended to be a main program that takes the input and decides what to do with it. So I'd better use gcc
if it worked without -lstdc++
. But if it's not possible I'll prefer using g++
instead as don't know what else gcc
may miss.
Many thanks for your considerations
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…