UNIX cc可执行文件位置

Ohn*_*ott 3 c unix executable cc

当我.c使用该cc命令编译文件时,它会创建一个a.out可执行文件.我注意到它a.out在我当前目录中创建了文件.有没有办法让a.out文件在与.c我在系统上的任何地方的文件相同的目录中创建?

例如,如果我当前的路径是~/desktop,我输入命令:

cc path/to/my/file/example.c
Run Code Online (Sandbox Code Playgroud)

a.out~/desktop目录中创建文件.我想在它中创建a.out文件path/to/my/file/a.out

aul*_*ron 5

每次调用cc时都必须使用-o开关:

cc -o path/to/my/file/a.out path/to/my/file/example.c
Run Code Online (Sandbox Code Playgroud)

或者你可以制作一个这样的包装脚本:

mycc

#!/bin/bash

dirname=`dirname "$1"`
#enquoted both input and output filenames to make it work with files that include spaces in their names.
cmd="cc -o \"$dirname/a.out\" \"$1\""

eval $cmd
Run Code Online (Sandbox Code Playgroud)

然后你可以调用

./mycc path/to/my/file/example.c
Run Code Online (Sandbox Code Playgroud)

反过来,它会打电话

cc -o "path/to/my/file/a.out" path/to/my/file/example.c
Run Code Online (Sandbox Code Playgroud)

当然你可以将mycc放在$ PATH中,这样你就可以这样称呼:

mycc path/to/my/file/example.c
Run Code Online (Sandbox Code Playgroud)