简体   繁体   中英

String variables don't work in Eclipse CDT

When I use string variables in Eclipse CDT (MinGW compiler) and I run the program, it doesn't show me anything. Here's my code:

#include <iostream>

using namespace std;

int main() {
    string hw = "Hello, world!";

    cout << hw << endl;

    return 0;
}

So that doesn't show anything, but when I just do cout << "Hello, world!" << endl; it does work.

I also tried including <string> , <string.h> , "string" and "string.h" but still no success. The same code does work in VC++ and Codeblocks though.

I can reproduce this problem on my machine. The output does not appear. But it is not an output problem, but actually two problems before you get to main:

  1. The program is failing to launch. If you try double-clicking on it in Windows Explorer, you will get an error message like this:

The program can't start because libgcc_s_dw2-1.dll is missing from your computer. Try reinstalling the program to fix this problem.

Screenshot of above:

上面引用的错误的屏幕截图

  1. When launched from within Eclipse, this error message is silently swallowed, so how are you supposed to know!

Solutions/Workarounds

You need to get the environment correctly set up to launch the MinGW program because its DLLs are not installed on Windows PATH or other standard places DLLs are searched for.

  1. Add C:\\MinGW\\bin to your PATH
  2. Launch Eclipse from within a MinGW shell (has basically same effect as 1)
  3. Run the program in debug mode, this causes the program to be launched as a child of GCC and that sets up
  4. Other options (not tried by me) may include statically linking the offending library, see The program can't start because libgcc_s_dw2-1.dll is missing
  5. File a CDT bug about the error message being hidden.

Extra info

If your program compiles, as I am sure it does based on your comments, changing the includes is probably irrelevant.

By adding an infinite loop around the cout s I could immediately identify something more than simply an output not being shown was going on. Try the same thing on your machine, and also try running the program from within MinGW shell and from outside it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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