简体   繁体   English

使用vscode,为什么同时包含“iosteam”和“stdio.h”时无法编译?

[英]Using vscode, why can't I compile when both “iosteam” and “stdio.h” are included?

When I declare and at the same time, vscode can't complie the file and there is the error log:当我同时声明时,vscode无法编译文件并且有错误日志:

    > Executing task: C:\mingw64\bin\gcc.exe -g d:\CODES\C++\try\main.cpp -o d:\CODES\C++\try\main.exe <

C:\Users\16337\AppData\Local\Temp\ccqDR0fO.o: In function `__tcf_0':
C:/mingw64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
C:\Users\16337\AppData\Local\Temp\ccqDR0fO.o: In function `__static_initialization_and_destruction_0':
C:/mingw64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status

My code is very simple:我的代码很简单:

#include <iostream>
#include <stdio.h>


int main()
{
    printf("print something");
    return 0;
}

Dev-C++ can compile that code properly. Dev-C++ 可以正确编译该代码。

If I delete #include <iostream> , it can be properly compiled.如果我删除#include <iostream> ,它可以正确编译。

<iostream> is a standard C++ header, so you need a C++ compiler and linker to use it. <iostream>是一个标准的 C++ header,所以你需要一个 C++ 编译器和 Z3175B426046787BEECE27 来使用7it4444 gcc is smart enough to compile .cpp files as C++, however it won't link the standard library into your program, hence the undefined references. gcc足够聪明,可以将.cpp文件编译为 C++,但是它不会将标准库链接到您的程序中,因此是未定义的引用。

You need to use g++ , which will link the standard C++ library by default, as Dev-C++ does.您需要使用g++ ,默认情况下它将链接标准 C++ 库,就像 Dev-C++ 一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM