user3881923
user3881923

Reputation: 93

'.' is not recognized as an internal or external command

I just started learning C++, and I've been trying to run my program from the command line using:

g++ helloworld.cpp

which works, then I typed

./a.out

then it returns the error '.' is not recognized as an internal or external command. I tried doing a.out, but it returns:

'a.out' is not recognized as an internal or external command.

I'm pretty new to the command line so it might be quite a novice problem. I'm using the gnu gcc compiler. My code is just a simple code for printing "helloworld", and it doesn't seem to be a problem with the code since the line g++ helloworld.cpp doesn't throw up any error. Just to add, I'm using windows 8.

Upvotes: 1

Views: 5175

Answers (3)

kfsone
kfsone

Reputation: 24269

In the Windows world, "\" is used to separate files and directories:

C:\Windows\System32\Etc

However most other operating systems, and the web, use "/"

file:///c/windows/system32/etc
/etc/motd

In Unix "." refers to the current directory, and Windows/DOS mostly support this.

The Unix-based compilers expect you to specify an output file name for a compilation, and the default is "a.out". But you can override it with "-o"

g++ test.cpp -o test.exe

This creates a file called "test.exe" in the current directory. If you are using MinGW's "bash" command line, you should be able to run the above executable by typing:

./test.exe   # note: no spaces!

at a "$" prompt

$ ./test.exe

However, if you are in a directory, say C:\Dev in the DOS command prompt, that won't work. DOS thinks '/' means "start of a parameter":

C:\Dev\> dir /w

outputs "wide" format dir

So, if you're using DOS, you just need to type:

test.exe

or

.\test.exe

e.g.

C:\Dev\> test.exe
C:\Dev\> .\test.exe
C:\Dev\> c:\dev\test.exe

or if you're relying on "a.out"

C:\Dev\> a.out
C:\Dev\> .\a.out

Upvotes: 0

Olivier Poulin
Olivier Poulin

Reputation: 1796

My best guess would be that a.out is not in your directory. Usually, when compiling your program from the command line, add the -o flag and name your executable (like helloworld.exe). Then you'll be sure that an executable of that name is actually being created.

In your case, since you're most likely running Windows, without specifying a -o flag, the default is a.exe and not a.out, so when you used ./a.out that executable didn't exist. In this case, you can run your program by typing a or a.exe. You don't need the leading ./ on Windows.

Upvotes: 4

Steephen
Steephen

Reputation: 15824

./a.out

If you are in *NIX world, using linux or any other UNIX related platforms .(dot) means current directory and a.out is an executable.

ls -l a.out 

list its permissions and make sure it has executable permission. If it dont have use following command to give it permission; usually it should have when your generated the a.out file.

chmod 755 a.out

If your file is not in current directory use the absolute path to invoke the executable file

<absolute_path>/a.out

It should work if you have taken care all above criteria.

Upvotes: 0

Related Questions