Executing program without an IDE

Pages: 12
Apr 9, 2016 at 4:17pm
Hey sorry for reviving an ancient post but can I choose where the "output" file will be created? Now it is created in the same directory as the source code but I would like it to be created in the Desktop.
Thank you
Apr 10, 2016 at 1:38pm
can I choose where the "output" file will be created?

Before you open the file, ofstream fout(fname);
change the string fname to include the full path to the required directory.
Apr 10, 2016 at 2:05pm
So something like that?

1
2
3
4
 
const string fname = "/Users/User/Desktop/file.txt";
...
ofstream fout(fname);


Thanks
Apr 10, 2016 at 2:23pm
Yes, that looks reasonable. Of course there are lots of variations, you might
do something like this:
1
2
3
4
5
const string fname = "file.txt";
const string outpath = "/Users/User/Desktop/";

...
ofstream fout(outpath + fname);
Apr 10, 2016 at 2:31pm
Nice idea. I like that
Apr 10, 2016 at 3:02pm
1 more question. I currently don't have a windows machine to test it by myself but does the
g++ -o output main.cpp ... command works for windows too?
Apr 10, 2016 at 3:13pm
Yes, things work much the same.
Apr 10, 2016 at 4:47pm
Just realised something. How can I decide where the executable file will be created? I confused the file.txt with the executable, sorry. So can I make a small alteration to the command g++ -o main.cpp?
Apr 10, 2016 at 7:52pm
I tried to use g++ -o output main.cpp in a windows machine(not mine) and I got the error:
 
c++ is not recognised as an internal or external command operable programme or batch file

or something like that. From what I understand, the problem is that g++ is not recognised as a command and therefore the source code cannot be compiled. I tried to follow some guides but I couldn't figure it out. Any help appreciated.
Apr 10, 2016 at 7:53pm
g++ is a program. An executable. If you don't have it, you can't run it. Windows does not come with it as standard.
Apr 10, 2016 at 8:03pm
So how do I get this "program"? Or is there any other way to execute a .cpp file from the command prompt without any big effort?
Apr 11, 2016 at 2:22pm
So how do I get this "program"?
The program is a compiler. Several choices are suggested here: http://www.cplusplus.com/doc/tutorial/introduction/
Those will also include an IDE but you are not obliged to use it. You can also download just a compiler without any IDE - there are a number to choose from.

Or is there any other way to execute a .cpp file from the command prompt without any big effort?
A .cpp file cannot be executed. It is just text. In order to get a program which can be executed, it needs to go through the stages of compiling, to generate an object file, and linking to produce the executable program.

Perhaps this takes us full circle, back to the opening post of this thread:
My question is how can a user execute them without opening Xcode for instance?
and the answer is still the same, the user should not need to go anywhere near the cpp files, the user just needs the finished product, the executable program.

Note - even if you use an IDE during development, the end result is an executable program which does not need any IDE in order to run.
Last edited on Apr 11, 2016 at 2:41pm
Topic archived. No new replies allowed.
Pages: 12