简体   繁体   中英

Terminal will not use updated a.out, how do I fix that?

I'm compiling a C file on Terminal on my mac, but when I run the a.out file, it will only compile an old version of my file.

For example, let's say my C file is made to print out "Hello, World!", I compile it using gcc and I run the a.out file, the a.out file will print out "Hello, World!".

If I then change the C file to print out "Goodbye", compile it and then run the a.out file, the Terminal will still print out "Hello, World". Does anyone know how to fix this?

All I am typing into the Terminal is

gcc main.c
~/a.out

Which is all I should be inputting right?

Please let me know if I didn't make anything clear enough. Thanks!

You are typing in

~/a.out

Which means "run the a.out program from the home directory." I think you want to type

./a.out

Which means "run the a.out program from the current directory." If you are using gcc , the newly-created program will be placed into the current directory (which, unless you're in the home directory, is not the same place), so the new version will run the right program.

Hope this helps!

gcc main.c
~/a.out

The first line will compile to a.out in your current directory, while the second line executes a.out from your HOME directory. Are you in your home directory?

If not, change the second line to

./a.out

. means current directory. So ./a.out means execute the a.out file in my current directory.

With ~/a.out you run a binary from your home directory (see echo "$HOME" ), which may be not the one where you're compiling your new main.c .

Try this: ./a.out (run from your current directory).

You should use

gcc main.c
./a.out

Notice the . (current directory) rather than ~ (home directory). The gcc command will write a.out to the current directory when it compiles the code, which is not necessarily the home directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM