MinGW g++/gcc not compiling with proper path and version command working

Ego sphere :

i changed the path to the correct directory and when I typed "g++ --version" and "gcc --version" I get the version info. I saved simple.cpp into a folder I made in the directory from path and typed "g++ simple.cpp" into the console and it returned with

C:\Users\guede>g++ simple.cpp
g++: error: simple.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.

This is the code i am trying to compile as a g++ test

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
    return 0;
}
user4581301 :
g++ simple.cpp

will look for simple.cpp in the current directory. Since You are executing g++ from C:\MinGW\bin, the compiler will only look in C:\MinGW\bin. It will not look in subdirectories. Since simple.cpp is in C:\MinGW\bin\Cpp_prog, the compiler is not looking for the file in the right place. You could

g++ Cpp_prog\simple.cpp

to specify which directory the file is in, but you do not want the compiler output cluttering up GCC's bin directory. Instead, make yourself a "Workspace" directory somewhere else on the computer, could be in your Documents folder or anywhere else convenient. Inside this workspace make a separate directory for each project so you can keep things organized more easily. Execute the compiler from within the appropriate project directory.

Then either invoke the compiler with

C:\MinGW\bin\g++ simple.cpp

or add C:\MinGW\bin to the user path or system path. See Adding directory to PATH Environment Variable in Windows for help on that.

Later you may find yourself with many different compilers and sometimes several versions of GCC. You don't want to have many locations for g++ in the path because it's easy to call the wrong one. When you get to this point you will likely have had to learn about using build automation tools to assist you with complicated projects. Typically you'll specify the compiler location to the automation tool.

Side note: dir is the DOS /Windows directory listing command. In the command prompt, type dir it will list all of the files in the current directory. If you're going to take up programming, you'll find it very helpful to learn about the various commandline tools provided by your operating system and development environment.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=398504&siteId=1