This is a set up question. I have a beginner's book which uses the MinGW compiler. After installing the MinGW program I am instructed to add the path C:\MinGW to the Path Variable of the Environmental Variables table of Windows 10. That allows entering the g++ command to initiate the MinGW compiler in the Console window without changing the directory of the Console ( from C:\Users\Username> to C:\MinGW). That works ok. I would like to put all my C++ .cpp and .exe programs in a directory C:\cpp, and then put that path C:\cpp into the Path of the Environmental Variables table also. Then I could just type in the Console g++ filename.cpp -o filename to generate an executable program or just type in the filename to execute it. That doesn't work. Why doesn't that work? I can live with the directory change but it seems like globalizing any command this way should work.
the operating system uses the path, programs may or may not.
so typing g++ anywhere works: you asked the operating system to find g++.exe somewhere in the path, and it did.
but g++ foo.cpp does not work: g++ is not the OS, it is its own program, and it does not search the system path for foo.cpp, it looks in the current path (where it was invoked from).
you can do g++ c:\cpp\foo.cpp though. That tells g++ where it is.
g++ COULD search your path if coded that way but then it could also find the wrong copy of the file... similar to dll problems where it finds the file but not the right version of it for the program at hand.
you can even play with this. what happens with a c++ program with command line args that include a file name? Do you see the file if they do not give the path, but its in the system path? (you will not, file.open() will fail). You can get the system path and iterate it, stuffing each chunk in front of the file until you find it.... in your code .. if you want.. (and you can do that to make a 'compiler' that calls g++ correctly and makes a bridge between what you want to do and g++ by finding the file and passing the full path along). Its probably doable in a batch file, too, but it would be a bit to string it together.
You have a nice idea, but as you have found only part of it works.
Add the "C:\MinGW" to the environment variable is fine and works the way it should.
I believe, but 100% sure that the environment variable is used to find ".exe" or ".bat" type files.
Even if "C:\cpp" would work the sub directory where you called "g++" and the source file from would end up containing the final ".exe" file.
You would probably be better creating a batch file, (.bat), to change to the "C:\cpp" directory that you need and then call "g++" from there.
This when the source code is compiled and linked the ".exe" will be in the same place as the source code.
If you know anything about batch files you could set it up to change to the proper director then run "g++" using a command line argument from calling the batch file.
Andy, the modern command line can find any file on the path. If the file has a defaulted opening program it will even invoke it, eg if you type foo.docx it will happily start word and open the file.
Checked, I was wrong. It finds the programs but not the files, same as the OP issue. You have to path the files explicitly or in the current path. It does seem to only find executable types :(