Executing program without an IDE

Pages: 12
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
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.
So something like that?

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


Thanks
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);
Nice idea. I like that
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?
Yes, things work much the same.
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?
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.
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.
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?
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
Topic archived. No new replies allowed.
Pages: 12