UNIX cc 可执行文件位置

发布于 2024-09-16 20:02:34 字数 392 浏览 2 评论 0原文

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

定格我的天空 2024-09-23 20:02:34

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

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

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

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

然后您可以调用

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

它,它会反过来调用,

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

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

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

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
伴梦长久 2024-09-23 20:02:34

您可以给出“-o”标志来定义输出文件。例如:

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

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
爱你是孤单的心事 2024-09-23 20:02:34

是的,使用 -o

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
空‖城人不在 2024-09-23 20:02:34

它可能不是您正在寻找的内容,但是您可以使用 -o siwtch 轻松重定向编译的输出,如下所示:

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

我不知道,如何归档,您想要什么(自动替换),但我想你可以用一些 bash 来做到这一点,如下所示:(我的脚本编写很差,未经测试,可能是错误的):

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

这应该将 i 中指定的文件编译到同一目录中的输出文件中,但使用.c 后缀已删除。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文