开发者

UNIX cc executable location

When I compile a .c file using the cc command, it creates an a.out executable. I've noticed that it creates the a.out file inside my current directory. Is there a way to make the a.out file be created in the same directory as the .c file wherever I happen to be开发者_开发技巧 on the system?

for example if my current path is ~/desktop and I type in the command:

cc path/to/my/file/example.c

It creates the a.out file in the ~/desktop directory. I would like it to create the a.out file in path/to/my/file/a.out


You will have to use the -o switch each time you call cc:

cc -o path/to/my/file/a.out path/to/my/file/example.c

or you can make a wrapper script like this:

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

then you can invoke

./mycc path/to/my/file/example.c

and it will, in turn, call

cc -o "path/to/my/file/a.out" path/to/my/file/example.c

of course you can put mycc in $PATH so you can call it like:

mycc path/to/my/file/example.c


You can give the "-o" flag to define the output file. For example:

cc path/to/my/file/example.c -o path/to/my/file/a.out


Yes, with -o.

cc path/to/my/file/example.c -o path/to/my/file/a.out


It may be not what you're looking for, but you can easily redirect the output of a compilation using the -o siwtch like this:

cc -o /a/dir/output b/dir/input.c

I don't know, how to archieve, what you want (auto replacement), but I guess you can do it with some bash like this: (I'm poor in scripting, untested and may be wrong):

i = "a/path/to/a/file.c" cc -o ${i%.c} $i

This should compile a file specified in i into an output file in same dir, but with the .c-suffix removed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜