Eclipse C++ Running/Debugging problems with console IO - c++

I've been trying to get into C++ programming with Eclipse, but I'm having problems setting up Eclipse.
I have MinGW installed and in the environment path, and I created the simple C++ project with the following source code:
#include <iostream>
int main(void)
{
std::cout << "what is your name? ";
std::string name;
std::cin >> name;
std::cout << "Hello, " << name << std::endl;
std::cin.ignore();
return 0;
}
After successfully building the project (both debug and release configurations), I click the run button and there's no output. The program terminates immediately. I've also tried running in debug mode, but then it will wait for me to type in a name, then display all the output. example console:
scott
what is your name? Hello, scott
I've tried this with both the 32 and 64 bit Windows versions of Eclipse Helios and both versions have the same behavior. Does anyone know what is going on and how to fix this?
I've run the program from the command-line and it works as intended.
edit: After some tinkering around, I found that by copying the MinGW dll's into the same folder as the executable the program will run in Eclipse just fine. Is there an alternative method to have Windows find the dll's in MinGW's bin folder rather than have to copy them over each time?

Is there an alternative method to have Windows find the dll's in MinGW's bin folder rather than have to copy them over each time?
Set the PATH environment variable in the run configuration settings (Run -> Run Configurations... -> Environment).

Related

No output on std::cout while outside of IDE

I have this well known c++ program in my Visual Studio 2015 Prof:
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello World!" << std::endl;
return 0;
}
As expected, it shows "Hello World!" if I hit Ctrl+F5. However, if I go to the directory within an cmd.exe and execute the HelloWorld.exe file, it doesn't show anything as output, but does quit (I can type in again).
According to a similar question I checked the settings of the project but I did not need to change anything, the Configuration Properties -> Linker -> System -> SubSystem already is at Console (/SUBSYSTEM:CONSOLE).
Flushing the std::cout also didn't help anything. It is reproducable on every freshly created project of VS2015 on my Win 7 64-bit machine and seems to be presistent after rebooting.
What is wrong with my IDE / settings?
After the hint of #Scheff I threw the exe file into "Dependency Walker". It gave me a missing UCRTBASED.DLL and lots of second and third level missing dlls (I think this is usually the case on every application?).
I somehow think my (recently installed) anti virus did interfere with that, because it shows some messages in the log regarding my HelloWorld.exe.
However, deinstalling anti virus and restarting machine did the trick. I can finally see Hello World! on cmd.exe.

Xcode App No Longer Reads Input From the Folder The App is Stored In

The title is a bit long-winded, but basically, I've written an app that reads and writes its input and output to text files. The entire time, it would read and write the files directly in the same directory as my Xcode derived data->project->build->products->debug folder. This was where everything was being written to and read from. I don't have a custom path set up for the application, so it just saves wherever the app is located. For the first time ever, I ran Apple's Instruments app, to try to learn how to use a profiler. Not long after selecting this app as the target in Instruments, I went back to the Xcode app to run the program some more. Everything works fine in Xcode. It reads from the files and prints to files in the same location as the folder, but if I try to run the actual program itself by clicking on the file and having it open terminal, it no longer reads or prints to the directory that app is in. Instead, its printing and reading from my home folder. I don't know what changed or what caused it to change, but I'm hoping its a simple fix. I'd like for the application to read from files and print files from the directory its located in again. I'm not sure if its an Xcode setting or a Terminal setting.
Any help would be greatly appreciated.
Update 1: Tried this with no luck:
how to change the working directory to the location of the program
The directory field was blank, so I thought this would the solution, but filling it in with the suggestion did nothing to alleviate the issue.
Update 2:
Just tried deleting the preference file, still no solution. I'm willing to give someone reputation. I don't have a whole lot because I'm a newer member, but I'll give what the person thinks is fair, to whoever solves it. I'm desperate and really don't want to wait 2 days to have this issue solved.
Update 3:
Tried changing the default path in the "Profile (release)->options area in the scheme section to the default variable suggested in update 1. No luck. I'm beginning to lose my mind.
Update 4:
I've tried deleting the scheme entirely and making a new one, in hopes that maybe there was something botched with the scheme, but this did not solve the issue. Input and output while running the app in Xcode is still using the working directory, while running the executable in the debug folder is using the home folder.
Update 5:
Just tested this on an older iMac and Xcode setup (OS 10.8.5 and Xcode 5.1.1) and it seems to be working correctly, reading and writing to the current working directory of the application in the debug folder.
For whatever reason, the solution suggested by https://stackoverflow.com/a/15537436/1035008 no longer works. Maybe broken in Xcode 8.1. But this seems to work:
#include <iostream>
#include <fstream>
#include <unistd.h>
using namespace std;
int main (int argc, const char * argv[])
{
// argv[0] returns the full path to the program, in my case "/Users/yuchen/Library/Developer/Xcode/DerivedData/../Debug/test
string directory(argv[0]);
// And we want to get rid of the program name `test`
directory = directory.substr(0, directory.find_last_of("/"));
// Point the directory to the program directory
chdir(directory.c_str());
cout << "Current directory is: " << getcwd(NULL, 0) << endl; // /Users/yuchen/Library/Developer/Xcode/DerivedData/../Debug/
ifstream fin("hi.txt");
if (fin.is_open()) cout << "File is Open" << endl;
else cout << "File is not open" << endl;
fin.close();
return 0;
}
Also see SO and SO. Hope this helps.

Can't print to console in c++ eclipse kepler

Running this code on windows using eclipse kepler
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
I've built it and run it with no errors, but I get nothing on the console. I've tried flushing the stream as well and that did not change anything.
Probably, this is because Eclipse does not add PATH to MINGW\bin (if you use MinGW, of course). In the "Environment" tag, press "New", set it as:
"Name:PATH"
"Value:C:\MinGW\bin"
I have not enough info to be sure it's your answer, but try.

Qt not building project file

I'm getting started with C++ and I use Qt Creator (I run Lubuntu 13.04).
Today, as any good start with programming, I wrote my Hello World program to see if things work after installing Qt5 and Qt Creator.
I created a new Console Application project and wrote:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
Saved my project as Test, and tried to run it.
I just keep getting
Starting /home/MYUSERNAME/Qt Programming/Test/Test...
Cannot change to working directory '/home/MYUSERNAME/Qt Programming/Test': No such file or directory
/home/MYUSERNAME/Qt Programming/Test/Test exited with code -1
Any ideas on how to fix this?
Thanks in advance.
Just maybe problem is a space in 'MYUSERNAME/Qt Programming/Test'.
Anyway take a look in project build settings. Something is wrong with path where compiler is looking for your project
Are you sure there is a "Test" directory in the "/home/MYUSERNAME/Qt Programming/Test" directory ?
Using my qt4 installation I've tried starting a new Qt Console Application, chosen the folder to create project in, typed out Test for the Project name hit next, deselected Debug, but kept release. Hit next. chose none for version control, hit finish.
Program skeleton shows up. Must delete the include statement for QCoreApplication. Replace it with iostream. Deleted "QCoreapplication a" and "return a.exec". put in std::cout<<"Hello World \n"; and return 0;. Then I saved everything hit the play button and xterm is opened and displays hello world. No problems here. I even tried with spaces in directory names. Again no problems.
Then I tried creating project non-qt-project->plain c++ project. After the same menus as above, found a skeleton with exactly your code except with an endl at the end of cout statement. Hit the play button. Again no problems here. Again it opened up xterm and printed out Hello World!
Don't know what else to tell you. If you changed the name to Test AFTER you created the project, I would say, start again and this time don't change the name.
I may install qt5 on my opensuse 12.3 x86_64 installation and will be able to see if there are problems with it.

unable to see the output of c++ program in the console

I have installed the eclipse ide(cdt) on my windows 8 laptop and tried writing a simple c program to check whether the program executes.
It did not execute and gave the error : binary not found .
So I did some searching online and realized that my system did not have a c/c++ compiler installed.
So I installed MinGW and selected the c and c++ compilers during installation.
Then I set the PATH environment variable to C:\MinGW.
I reopened eclipse, wrote a simple c program and it worked as expected!
I created a c++ project, wrote a simple piece of code and could not see the output in the console!
Here is the code:
#include<iostream>
using namespace std;
int main()
{
cout<<"sample text";
return 0;
}
Linker (Option) > Add Command (g++ -static-libgcc -static-libstdc++)
This is not the right solution.
You have in your path environment variable only c:\minGW .
But it should be c:\minGW;c:\minGW\bin . (Set the PATH before open eclipse)
Therefore, libstdc++-6.dll needed by the current program, can not be found.
In eclipse there is no error, but no output in the console !!
It to compile into the program may be regarded as a trick, but will only work for the standard libs .
your linker flags should not be set like :
--> MinGW C++ Linker (Option) > Command (g++ -static-libgcc -static-libstdc++)
should be set here :
I know in this case it is not necessary at the end << endl to write.
A good programming style should use << endl :
cout << "sample text" << endl;
You may simply need to flush the output, using flush or endl. Try this:
cout<<"sample text" << endl;
or
cout<<"sample text" << flush;