Eclipse CDT - how to run compiled .exe in external console (cmd.exe) - c++

Is there a way to run compiled CDT program inside Eclipse IDE, but not in Eclipse terminal, but rather in new cmd.exe window? Some Run configuration or External Tools configuration?
Just like in QT, when you run compiled console application.
I googled this, on my mind, simple question, but (what is strange for me) I didn't find the answer.

From Eclipse CDT forum, from Mr Klaus:
Edit the Run Configuration:
On the Main page set C/C++ Application to:
${system_path:cmd.exe}
On the Arguments page add (adjust path and program name):
/C "start .\Debug\hello.exe"
On the Common page untick "Allocate console".
In the example above the console windows will close immediately if hello.exe ends. If you don't want that, use this version on the Arguments page:
/C "start ${system_path:cmd.exe} /K .\Debug\hello.exe"
Btw, you could use the same concept for External Tools configuration also!

In my case i was need to change arguments:
/C "start /WAIT .\Debug\hello.exe"

Related

How to execute a CLion program in gnome terminal?

This question already has an answer here but I am using CLion version 1.2.1 and the answer in that post doesn't help. It is possible to run a CLion program in gnome terminal instead of its own console ? If yes, how ? Thanks.
EDIT : I know where the executable file are stored but want to know if it is possible to run the file in terminal directly from the ide i.e. by selecting the run option in ide.
Yes, you can execute a Clion c/c++ program in a gnome terminal from the IDE. Here is how:
In the top right of your clion application you should see your project name or "Build All". Click on that and go to "edit configurations"
there click on the plus sign(top left) and then on "Application"
Now go to "Executable:" and click on "select others". There you should go to where your gnome-terminal is stored (mine is at "/usr/bin/gnome-terminal" on ubuntu)
Next go to "Program arguments:" and type -e ./myProjectName (For newer versions of gnome-terminal -e is deprecated, use -- ./myProjectName)
go to "Working Directory:" and type in the location to your cmake-build-debug folder found in your project folder
You can now press okay and go to the top right to select the name of your application that you created in step 2
This will run your program in the gnome terminal.
Hope this helped :)

Running CLion on the System console (like Visual Studio)

I am trying to run a simple C++ program on CLion. The problem is that when I try to compile and run it, it shows the result in the application console. I want to run on the System console like the Visual Studio runs the output of the console apps. Is there a way of doing this. I use MINGW compiler.
You can use an external terminal, do these steps.
(top menu) run\ edit Configurations
tick 'run in external console'
Well, if anyone is still around wanting to open an external cmd window on run, there is a way to do it on Windows as well:
Go to Run > Edit Configurations
For the executable select C:\Windows\System32\cmd.exe
For program arguments use: /c "start cmd.exe #cmd /k "ProjectName.exe""
For working directory set the cmake debug (or release) folder
Save and select the profile from the dropdown right next to the run button
Note: In the third step /k can be replaced with /c to make the window close after the program is run (with /c its more like visual studio and with /k its more like code blocks)
I am not sure about Windows but in linux you can do it using this answer.
You might be able to change the gnome-terminal to cmd if you are using windows but you will probably need to change the "Program Arguments" too.
the above answer works fine in windows also. In working directory you can set this $CMakeCurrentBuildDir$
enter image description here

Qt - Qt Command Prompt

So I found an excellent video where it teaches me about static compile. But in there it doesn't show where he got the "Qt Command Prompt" from. I searched alot but didn't find it in the directory. What do I do? Can anybody tell me where it's located? I even searched for it in Explorer but didn't find it.
Just add the following paths:
<qtdir>\mingw\bin;
<qtdir>\Desktop\Qt\<qtver>\mingw\bin;
<qtdir>Madde\bin\
to your %PATH% environment variable, and the default command prompt (cmd.exe) will become what you need. Don't forget to restart an already opened cmd.exe after editing %PATH%.
What OS do you use?
For exanple, here you can find how to edit %PATH% on win7.
The Qt command prompt is simply a normal cmd.exe session where the paths to the Qt libraries and binaries have been added to the environment.
It depends on how you installed Qt (using the SDK, build from source, etc.). If you used the SDK, a shortcut should have been installed in your Start menu. My Qt command prompt is at:
C:\Qt\4.8.2\bin\qtvars.bat vsvars
That being said, I rarely use the Qt command prompt. If you use the normal Visual Studio command prompt, then navigate to your Qt "bin" folder, you should be able to execute the command you need, like configure.
If you are using Windows 8, just type "Qt 5.4 for Desktop" after pressing the windows button.
Actually in windows 8 just typing "qt" in start menu, will give you the suggestion.

Easy way to run command line programs C++ - Windows 7

I am trying to find an easier way to test my command line application. It has arguments so I run it from a command prompt. My process is this:
Build
Go to the output
Open command prompt
Change directory
Run it
There has to be an easier way not only to debug but to open a command prompt in the current folder.
Thank you for your time.
If you go to the project properties, Debugging settings, you can set the working directory and parameters. If you use the same parameters all of the time, you can enter those in on that screen as well. Then just F5 or Ctrl+F5 to run.
Set a breakpoint at the end of the code to keep it from going away after it is done.
See Debugging with command-line parameters in Visual Studio
Alternatively, you should be able to use a shell script (or Python os.system()) to automate some of those steps.
To open a command prompt in the current directory using explorer, you can shift+right click->Open Command Window Here. That will save a little time.
If you're using Visual Studio, pressing F5 will run the code in the debugger, and Ctrl+F5 will run the code normally. Just remember to include a cin.get() statement at the end or the terminal window will close before you can read the output.
Other IDEs should have similar functions. Check in the Run or Debug menu.
EDIT: Sorry, didn't see that you're asking about running it with arguments. In VS, in Project Properties there are the Debugging settings. Within that, there is a field called Command Arguments. These will get passed to the application when you run it from within VS.
In project properties under debugging you can set the command line arguments (and environment variables) when debugging,
There is an extension called PowerCommands for Visual Studio 2010 that can be installed from Tools -> Extension Manager. It includes a Open Containing Folder and Open Command Prompt functionality that gets added to your right-click menu in the Solution Explorer.

C++ program written in Eclipse using Windows and MinGW cannot display output to console view

I'm using Windows 7 64bit.
I installed eclipse version 3.6.2, cdt, and MinGW. I have a C++ console program in Eclipse as follows:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
setbuf(stdout, NULL);
for (int i = 0; i < 10000000; i++) {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
}
int val;
cin >> val;
return 0;
}
If I run this console program, it should display Hello world to Console View in Eclipse, but nothing displays.
If I go to the debug folder and run the exe, it does print to the console.
If I make some syntax mistake, then the Eclipse Console View will show something, such as:
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\hh.o ..\src\hh.cpp
..\src\hh.cpp: In function 'int main()':
..\src\hh.cpp:17:3: error: expected ';' before 'return'
Build error occurred, build is stopped
Time consumed: 255 ms.
Why is nothing showing in the Eclipse console view and how can I make my C++ console program display output?
I found a workaround from this site:
http://www.eclipse.org/forums/index.php?=42e862594001fa4469bbc834885d545f&t=msg&th=197552
At that link, look at the reply from "No real Name".
In case the link goes down, here is the content:
Environment: jdk1.6u18 64bit + Eclipse Helios 64bit + win7 64bit
No console output at "Run", but output correctly at "Debug".
The following method worked for me:
1. Goto Project->Properties->Run/Debug Settings, choose the .exe file
and press "Edit"
2. In the "Environment" tag, press "New", set it as:
"Name:PATH"
"Value:C:\MinGW\bin"
In fact, I have already set "C:\MinGW\bin" in windows PATH environment
variable, but it seemed to not work.
The problem is that your program uses dll libraries from MinGW - try to start the exe file manually, it will report some error about missing dlls.
Solution can be, that you copy required dlls to .exe file in your project directory (and Release or Debug subdirectory, depends on what .exe are you executing with Run command).
Or, in menu Run -> Run Configuration select the configuration which you use for that .exe file (or create a new C/C++ Application configuration) and select Environment tab in the right panel. There add new variable named PATH with value c:\MinGW\bin (this is default path to mingw\bin directory, use a path valid for your instalation if it's somewhere else)
Edit: Now I'm looking at post by Vikyboss and it's in fact the same - setting the PATH variable in Run configuration. Setting PATH variable in Preferences > C/C++ (Expand it) > Environment as described by Sydraps didn't work for me.
But I think that static linking that libraries may be the best solution for you. In menu Project -> Properties select C/C++ Build -> Settings. In the right panel select Configuration which you want to change (you may select All). In the tab Tool Settings select MinGW C++ Linker -> Miscellaneous and in the right panel in the Linker flags type -static. Now the .exe will be bloated by the size of the libraries (in my case approx. +900kB for Hello world example, requiring 2 dlls), but it will be independent at any libraries.
I hope this will be helpful for anyone trying to start with Eclipse C/C++ and wondering why there's no Hello world in console.
Ales Chlubny
I fixed the problem on my windows 7 x64 PC. In the Eclipse window go to Preferences > C/C++ (Expand it) > Environment > Add:
"Name:PATH"
"Value:C:\MinGW\bin"
If this does not fix it. Try adding the above to the system environment variables on your PC in Computer > Advanced System settings
I find the reason, just because I'm using 64bit eclipse!
I turn to 32 bit eclipse, the same code works fine.
My problem with displaying hello world(64 bit Windows7) in console was solved when I ran eclipse as administrator.
I added the C:\MinGW\bin to environment variable path and then started eclipse as administrator
I created a Hello World C++ Project (MinGW GCC) app from the Eclipse wizard, cut and pasted your code and it compiled fine. Then prior to running I reduced your loop to 10 and it ran fine.
You don't say how you created your project but it would appear likely your missing some include directive or library path. Also I don't see the need to include or setbuf(stdout, NULL). I also find it helpful when troubleshooting to NOT bring an entire namespace into scope. Rather do this:
using std::cout;
using std::cin;
Finally, flushing the buffer each time with << endl; seems like overkill, adding a simple \n to the end of the string would be more efficient.
(I did all this on Win 7 64 bit - but I was using a 32 bit version of Eclipse Galileo)
If you are using MinGW compiler,
Add -static-libgcc -static-libstdc++ as Linker flags for your new
project. This text should be added to the Linker flags field, which
can be found by right-clicking on the new Project in the Project
Explorer and clicking on Properties. Under the Project Properties,
expand the C/C++ Build menu and click on Settings. Under the Tool
Settings tab, expand the MinGW C++ Linker menu and click on
Miscellaneous. Add the text to the Linker flags field, then click the
Apply button.
http://orfe.princeton.edu/help/article-296
For me installing the 32 bit versions of Eclipse (Indigo 3.7) and the 32 bit Java JDK/JRE did not work. I use the much quicker solution from the Eclipse CDT/User/FAQ:
Quote from Eclipse CDT/User/FAQ - Eclipse console does not show output on Windows:
Eclipse console does not show output on Windows In Eclipse CDT on
Windows, standard output of the program being run or debugged is fully
buffered, because it is not connected to a Windwos console, but to a
pipe. See bug 173732 for more details. Either add fflush calls after
every printf or add the following lines in the start of the main
function:
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
I had an issue with my eclipse-cdt, new C++ project > hello world (cygwin gcc), right click on exe file, run as and nothing was showing on console. It was the same with c project.
I saw that my eclipse version was already a 32bits one.
I figured this out and here is (the) my solution:
There are several compilation profiles in eclipse cdt: release and debug. The default profile in eclipse cdt is debug. So, launching exe using "run as" does not work, you should launch it using "debug as". In my case, I was using a really recent cygwin installation with gcc installed, but with gdb not yet installed. I had to install gdb package in cygwin using cygwin-setup. I reran the exe using "debug as" and it worked.
I guess using release profile instead of default debug profile, rebuilding also works, and I guess that it's the same with mingw environment.
Just added the C:\MinGW\bin to environment variable 'Path' in Windows 7 64-bit.
Now Console outputs messages
I encountered a similar problem.
Environment:
jdk1.6u18 64bit + Eclipse Helios 64bit + win7 64bit
No console output at "Run", but output correctly at "Debug".
The following method worked for me:
Go to Project > Properties > Run/Debug Settings
Choose the .exe file and press "Edit"
In the "Environment" tag, press "New", set it as:
"Name:PATH"
"Value:C:\MinGW\bin"
In fact, I have already set "C:\MinGW\bin" as the Windows PATH environment variable, but it seemed to not work.
Works for me by going Administator on Eclipse Kepler 64bit.
In case someone is interested, I found how to fix it forever on Windows XP (may work in other windows version though) without you having to specify the variables of each executable and such:
Start Menu > RightClick on MyComputer > Properties > Advanced Options > Environment Variables
There, in the "User variables" field,
If there's an environment variable called PATH, select Modify and add this information in the end of the Value field, separated by semicolon:
C:\MinGW\bin
Example:
C:\SomeDirectory; C:\Another; C:\MinGW\bin
If the environment variable PATH does not exist, add a new one with this information:
Name: PATH
Value: C:\MinGW\bin
Accept all and you should get console output :)