C ++程序不在Code :: Blocks中运行 [英] C++ program does not run in Code::Blocks

查看:204
本文介绍了C ++程序不在Code :: Blocks中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #include< iostream> 

using namespace std;

int main()
{
cout< 你好,世界! << endl;
return 0;
}

它会产生以下错误:

  -------------- Build:在项目中调试--------------- 

编译:main.cpp
链接控制台可执行文件:bin \Debug\project.exe
C:\Program文件(x86)\CodeBlocks\MinGW\lib / libstdc ++。a(eh_throw.o):eh_throw.cc :(。text + 0x7b):未定义的引用`__w32_sharedptr_unexpected'
C:\Program Files(x86)\CodeBlocks\MinGW\lib / libstdc ++ .a(eh_throw.o):eh_throw.cc :(。text + 0x8c):未定义的引用`__w32_sharedptr_terminate'
C:\Program Files(x86)\CodeBlocks\MinGW\lib / libstdc ++。 a(eh_globals.o):eh_globals.cc :(。text + 0x4e):未定义的引用`__w32_sharedptr'
C:\Program Files(x86)\CodeBlocks\MinGW\lib / libstdc ++。 (eh_globals.o):eh_globals.cc :(。text + 0xb9):未定义的引用`__w32_sharedptr'
C:\Program Files(x86)\CodeBlocks\MinGW\lib / libstdc ++。 eh_globals.o):eh_globals.cc :(。text + 0x179):未定义的引用`__w32_sharedptr'
C:\Program文件(x86)\CodeBlocks\MinGW\lib / libstdc ++。 .o):eh_globals.cc :(。text + 0x186):未定义的引用`__w32_sharedptr'
C:\Program文件(x86)\CodeBlocks\MinGW\lib / libstdc ++ a(eh_globals。 o):eh_globals.cc :(。text + 0x1e3):未定义的引用`__w32_sharedptr'
C:\Program Files(x86)\CodeBlocks\MinGW\lib / libstdc ++。a(eh_globals.o ):eh_globals.cc :(。text + 0x1ef):更多未定义的引用`__w32_sharedptr'跟随
C:\Program文件(x86)\CodeBlocks\MinGW\lib / libstdc ++ a(eh_terminate。 o):eh_terminate.cc :(。text + 0x67):未定义的引用`__w32_sharedptr_terminate'
C:\Program Files(x86)\CodeBlocks\MinGW\lib / libstdc ++。a(eh_terminate.o ):eh_terminate.cc :(。text + 0x97):未定义的引用`__w32_sharedptr_unexpected'
C:\Program文件(x86)\CodeBlocks\MinGW\lib / libstdc ++ a(eh_terminate.o) :eh_terminate.cc :(。text + 0xb3):未定义的引用`__w32_sharedptr_terminate'
C:\Program文件(x86)\CodeBlocks\MinGW\lib / libstdc ++ a(eh_terminate.o) eh_terminate.cc:(.text+0xd3):未定义引用`__w32_sharedptr_unexpected'
collect2:ld返回1退出状态
进程以状态1(0分钟,1秒)终止
12错误,0条警告


解决方案

链接器无法找到 __ w32_sharedptr ,这可能是依赖libstdc ++需要工作。



通常,在构建项目时,标准库及其所需的任何依赖关系都会自动链接。然而,由于trojanfoe的注释表明这只是真的,如果你使用g ++编译。如果你正在使用gcc构建C ++代码,那么C ++标准库将不会自动包含,因为gcc驱动程序认为它正在编译C代码。



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





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



启用完整日志记录后,重新生成项目并使用所使用的命令更新您的问题。 / p>

这是大约在你打开以上选项重建时在日志窗口中应该看到的:



>


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

#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

解决方案

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

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.

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

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 settings tab-> Compiler Log = Full command line. Note you might have to scroll a bit to the right to find the tab.

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:

这篇关于C ++程序不在Code :: Blocks中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆