C++ 程序不能在 Code::Blocks 中运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7991561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:50:13  来源:igfitidea点击:

C++ program does not run in Code::Blocks

c++codeblocks

提问by Prajwal Acharya

While doing programming in Code::Blocks it compiles well for C but not for C++. Even for a "Hello World" program:

在 Code::Blocks 中进行编程时,它可以很好地为 C 编译,但不能为 C++ 编译。即使对于“Hello World”程序:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

it gives these errors:

它给出了这些错误:

-------------- Build: Debug in project ---------------

    Compiling: main.cpp
    Linking console executable: bin\Debug\project.exe
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_throw.o):eh_throw.cc:(.text+0x7b): undefined reference to `__w32_sharedptr_unexpected'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_throw.o):eh_throw.cc:(.text+0x8c): undefined reference to `__w32_sharedptr_terminate'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x4e): undefined reference to `__w32_sharedptr'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0xb9): undefined reference to `__w32_sharedptr'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x179): undefined reference to `__w32_sharedptr'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x186): undefined reference to `__w32_sharedptr'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x1e3): undefined reference to `__w32_sharedptr'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x1ef): more undefined references to `__w32_sharedptr' follow
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x67): undefined reference to `__w32_sharedptr_terminate'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x97): undefined reference to `__w32_sharedptr_unexpected'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0xb3): undefined reference to `__w32_sharedptr_terminate'
    C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0xd3): undefined reference to `__w32_sharedptr_unexpected'
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 1 seconds)
    12 errors, 0 warnings

回答by greatwolf

The errors you're getting indicate that the linker is having trouble locating __w32_sharedptrwhich is probably a dependency libstdc++ needs to work.

您收到的错误表明链接器无法定位__w32_sharedptr可能是 libstdc++ 需要工作的依赖项。

Normally the standard library and any dependencies it needs are linked in automatically when you build your project. However, as trojanfoe's comment indicates this is only true if you're compiling with g++. If you're building C++ code with gcc, the C++ standard library won't get included automatically since the gcc driver thinks it's compiling C code.

通常,当您构建项目时,标准库及其所需的任何依赖项都会自动链接。但是,正如 trojanfoe 的评论表明这仅在您使用 g++ 编译时才成立。如果您使用 gcc 构建 C++ 代码,则不会自动包含 C++ 标准库,因为 gcc 驱动程序认为它正在编译 C 代码。

To verify what's actually happening in your codeblocks setup go to Settings->Compiler and Debugger->Global compiler settings(on the left)->under Toolchain executablestab. You should see something similar to this:

要验证代码块设置中实际发生的情况,请转到设置->编译器和调试器->全局编译器设置(左侧)-> 在工具链可执行文件选项卡下。您应该会看到类似的内容:

enter image description here

在此处输入图片说明

If your setup looks right but still refuses to build properly, enable full compiler logging and see what commands are actually being invoked by the IDE. You can find this under Global compiler settings->Other settingstab-> Compiler Log = Full command line. Note you might have to scroll a bit to the right to find the tab.

如果您的设置看起来正确但仍然拒绝正确构建,请启用完整的编译器日志记录并查看 IDE 实际调用了哪些命令。您可以在全局编译器设置->其他设置选项卡 -> 编译器日志 = 完整命令行下找到它。请注意,您可能需要向右滚动一点才能找到该选项卡。

With full logging enabled, rebuild your project again and update your question with the commands used.

启用完整日志记录后,再次重建您的项目并使用所使用的命令更新您的问题。

This is approximately what you should see in the log window when you rebuilt with the above options turned on:

当您在打开上述选项的情况下重建时,这大约是您应该在日志窗口中看到的内容:

enter image description here

在此处输入图片说明