String variables don't work in Eclipse CDT - c++

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:
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:
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.
Add C:\MinGW\bin to your PATH
Launch Eclipse from within a MinGW shell (has basically same effect as 1)
Run the program in debug mode, this causes the program to be launched as a child of GCC and that sets up
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
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 couts 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.

Related

Eclipse C++ Setup

I want to switch from Dev-C++ to Eclipse (Version 12/18, 4.10.0) , but I am not able to Compile in Eclipse. I installed MinGW, edited the Pathvariable, restared the PC, installed the C++ Eclipse IDE, created a new Project there I choosed MinGW as my Compiler, and wrote a Hello World.
The Code is:
#include<iosteam>
using namespace std;
int main(){
cout << "Hello World!";
return 0;
}
The Error Message I get is the following:
error: CreateProcess: No such file or directory
What do I need to do to be able to build/run Proramms?
Thanks in advance
You have everything you need to be able to build/run programs. Maybe something is flawed in the configuration. You should separate the tasks to find out the problem.
First try to compile your minimal example by executing g++ from a console like that:
g++ main.cpp. If you get the same error message then you have to fix your Mingw-w64 setup.
If this works, then something is wrong with eclipse. You might want to check different project types. When using Makefile projects, you perhaps have more possibilities to do checks on the console.

C++: Why does libtiff break the console-output?

So finally I’m not able to help myself out by researching anymore. Hopefully you can help me.
I recently decided to learn C++ in the context of my bachelor-thesis: My first aim is to read the pixel-values of a tiff-image with the libtiff-library. Yet, every call of a function of the library seems to break parts of my program.
Here’s the simple “HelloWorld”-Program, it works as it should:
#include "tiffio.h"
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
// TIFF* tif = TIFFOpen("path to .tif", "r");
return 0;
}
When I uncomment the second line in main(), the code still does compile without errors (except the warning that ‘tif’ isn’t used) and I can start the program as usual. Yet nothing gets printed into the console anymore. Not “Hello” nor any errors.
Any suggestions where the error could be? The code should be alright, I guess I messed something up during the setup of the library or so. Here’s what I did:
I managed to set up eclipse (Mars, 32bit) for C++ and MinGW (32bit) on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
I created a new C++-project with the mentioned code and did the following adjustments in the project-properties:
Project->Properties->C/C++ General->Paths and Symbols->Library
Paths-> Added “D:/… /tiff-4.0.4/libtiff/.libs”
Project->Properties->C/C++ Build->MinGW C++
Linker->Miscellaneous->Set Linkerflags to “-static-libgcc
-static-libstdc++”
Project->Properties->C/C++ Build->MinGW C++ Linker->Libraries-> Set
(-L) to “D:/… /tiff-4.0.4/libtiff/.libs” and (-l) to “libtiff”
I know the .tif is a valid file as I implemented parts of my program in C#, using the LibTiff.NET library by BitMiracle.
Thank you in advance!
Edit 1: The same error occures, even if TIFF* tif = TIFFOpen("path to .tif", "r"); is never called but written down in a dead part of the code. Debugging does not work either, the program seems to be terminated (exit value 0) before a single line is executed, without any error-reports.
I had the same issue and managed to get rid of it.
I set up eclipse (Mars) for C++ and MinGW on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
After the build, I got two directories containing files:
include
tiff.h
tiffconf.h
tiffio.h
tiffio.hxx
tiffvers.h
lib
libtiff.a
libtiff.dll.a
libtiff.la
libtiffxx.a
libtiffxx.dll.a
libtiffxx.la
I also linked all include files and only the libtiff.a to my project and that solved it, ie, the other lines are now executed.
I hope, I helped with this post.

Eclipse Kepler: #include "boost/filesystem.hpp" breaking debugging?

I have a fresh install of Eclipse Kepler on Windows 7 x64. My first "Hello world" worked great, including setting breakpoints and walking through code in debug mode. My second program links to Boost. I can set the breakpoints, build 'Debug' without errors, and run Debug, but unlike my Hello World, it blows through my breakpoints. The console window appears to flicker "Info: Nothing to build for..." and then clears. None of my std::cout calls appear.
With a little debugging I found that commenting out "filesystem.hpp" (below) enables the debugging, but of course I need it. I'm new to Eclipse. What would cause this behavior?
#include "boost/filesystem.hpp" <--- removing this enables debugging
#include "boost/regex.hpp"
#include <iostream>
int main()
{
std::cout << "Start search" << std::endl;
:
:
Look in the build output/problems views.
Most likely, you don't link to the required lib(s) for Boost Filesystem (-lboost_system -lboost_filesystem).
In effect this just means that no executable is built, and obviously therefore cannot be started.

No output in c++-program if object is instantiated using Eclipse

I got an issue after installing Eclipse Kepler on my Windows 7 32bit machine. I installed the CDT and the MinGW-compiler. I configured the installation by adding MinGW to the PATH and tested my configuration with a "Hello world"-program, which worked.
The strange thing is, that if I instantiate an object, nothing is outputted. It doesn't matter if it's a std::string or a custom made class. If I instantiate it, nothing is outputted, even if it should be outputted before the instantiation. The exact same code works fine, if I compile it with cygwin gcc from command line. If I change the toolchain to cygwin gcc nothing changes (I've rebuild the program with "build all").
There is no error displayed and no problem listed.
Here's the minimal working example:
#include <iostream>
#include <string>
using namespace std;
class SayWorld{
public:
SayWorld(){
cout << "World!" << endl;
}
};
int main() {
//Only gets outputted, if the lines, that don't work are commented out:
cout << "Hello ";
// Works:
cout << "World!" << endl;
// Doesn't work:
// SayWorld sw;
// Also doesn't work:
// string str("World!");
// cout << str << endl;
return 0;
}
Edit 2:
I narrowed the error to MinGW, as this picture of a Cygwin-Bash-Terminal demonstrates. (The file was not changed beetween the to g++ calls and contains the example above)
Edit 1 (Legacy)
Toolchain-picture:
-picture removed- (don't think it was necessary)
After reinstalling eclipse and MinGW again, step-by-step following this video tutorial:
http://www.youtube.com/watch?v=77xZOT3xer4
and having the same problem afterwards, I stumbled uppon this post in the eclipse forum:
http://www.eclipse.org/forums/index.php/u/104305/
which has brought me to this solution:
Right-click Project -> Properties -> Run/Debug Settings
Choose executable and hit the "New"-button.
Go to the Environment-Tab and create a new variable named PATH with the value: "C:\MinGW\bin".
As I am no expert, I can't explain to you why it works, but it worked for me. If someone knows, how to do this better or wholly avoid this problem, I'd be glad to listen.
This entry was definitively in my Windows-PATH...
PS.: The problem seems also to be known here:
unable to see the output of c++ program in the console

Codeblocks outputs broken executable

I have downloaded plenty of different versions of code blocks, and none of them compiles quite right. My hello world runs within code blocks just fine. However, when I run the executable outside of codeblocks, it says "Hello.exe has stopped working". There isn't anything wrong with my code (I don't think.) and my mingw compiles fine outside of codeblocks. What does codeblocks do to my executable? Is there some option to fix this? I am on windows 7 64 bit, and my current code blocks version is 10.05. My program:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
I solved the problem. I had a broken compiler (or something like that). My suggestion for other people with this problem is to experiment with different versions of the minGW compiler. Also, change the version of code blocks you are using, or even uninstall everything and restart. The problem with mine was I downloaded a bad compiler. [The truth is, codeblocks isn't the best ide.]