Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

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

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: binDebugproject.exe
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_throw.o):eh_throw.cc:(.text+0x7b): undefined reference to `__w32_sharedptr_unexpected'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_throw.o):eh_throw.cc:(.text+0x8c): undefined reference to `__w32_sharedptr_terminate'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x4e): undefined reference to `__w32_sharedptr'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0xb9): undefined reference to `__w32_sharedptr'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x179): undefined reference to `__w32_sharedptr'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x186): undefined reference to `__w32_sharedptr'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x1e3): undefined reference to `__w32_sharedptr'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x1ef): more undefined references to `__w32_sharedptr' follow
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x67): undefined reference to `__w32_sharedptr_terminate'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x97): undefined reference to `__w32_sharedptr_unexpected'
    C:Program Files (x86)CodeBlocksMinGWlib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0xb3): undefined reference to `__w32_sharedptr_terminate'
    C:Program Files (x86)CodeBlocksMinGWlib/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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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 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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...