0

I'm learning C++ and trying to run a simple hello world program. It compiles but it won't execute. It worked on Windows, but it won't run on Zorin OS.

I read online that the command to run it is ./test or ./test.exe.

This is what is looks like on the terminal:

$ g++ test.cpp -o test.exe
$ ./test
bash: ./test: No such file or directory

I looked at the questions similar to this, but none have helped me.

jww
  • 97,681
  • 90
  • 411
  • 885
  • 3
    Linux executables does not have to have extentions (unlike dos/windows). If you give it one the you have to use it as part of file ie ./test.exe – Slava Jun 05 '18 at 23:00
  • Hmmm your generating `test.exe` on bash !! and trying to run it as `test` ?? – Mohammad Kanan Jun 05 '18 at 23:00
  • 2
    Bash is trying to tell you useful information. `./test: no such file or directory` Why aren't you reading this message? List all the files you have in that directory. Do you see something called `test`? – MPops Jun 05 '18 at 23:03
  • `-o test.exe` says the output/program name is `test.exe`. Run the program as `./test.exe`. Also see [How to run a .exe file with command prompt?](https://stackoverflow.com/q/13549877/608639), [How to run .exe executable file from linux command line?](https://stackoverflow.com/q/24452447/608639), [How to run an .exe from linux command prompt](https://superuser.com/q/48773/173513), etc. – jww Jun 05 '18 at 23:07
  • Related, you should probably be using Ubuntu rather than Zorin. Ubuntu is Linux on training wheels, and it will be easier for a beginner to use and find support. Zorin appears to be a paid product and I am guessing it is not as well supported. – jww Jun 05 '18 at 23:15

1 Answers1

0

You can not expect to be able to execute the same commands on both Windows and Linux. They use different shells with different syntax and different behaviors.

Here's a typical example of compiling a file on GNU/Linux:

dir$ g++ myfile.cpp -o myfile
dir$ ./myfile

Here's a typical example of compiling the same file on Windows:

dir> g++ myfile.cpp -o myfile.exe
dir> myfile

Note in particular:

  • Linux doesn't use .exe or other extensions on executables, but Windows does.
  • Windows doesn't require specifying directory to run files in the working directory, but Bash on GNU/Linux generally does.
  • The only reason why the compilation command is as similar as it is is that g++ is a Unix tool ported to both platforms. Windows normally uses / instead of - for flags like -o

As commands get more complex, they start diverging even further.

that other guy
  • 116,971
  • 11
  • 170
  • 194