为 C 代码创建 makefile,运行时无需 ./
我需要创建一个 makefile,将我的 simpleprogram.c
编译为 sp
,并且可以像 ls
、等 unix 命令一样调用它ps
等,无需显式编写 ./sp
。我上网查了一下,没有找到解决办法,或者搜索的方式不对。我无法像“executable without ./”这样进行搜索,因为我不知道这叫什么=> “<代码>./”
I need to create a makefile that will compile my simpleprogram.c
to sp
and it can be called like unix commands like ls
,ps
etc, without writing explicitly ./sp
. I looked upon the web and cannot find a solution, or searching it in a wrong way. I cannot search like "executable without ./" , because I do not know what is this called => "./
"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将二进制文件放入
PATH
中的目录中。http://www.cs.purdue.edu/homes/cs348/unix_path.html
Put the binary in a directory that's in your
PATH
.http://www.cs.purdue.edu/homes/cs348/unix_path.html
只需将程序复制到系统 bin(可执行二进制文件)目录即可。
最常见的是
/usr/bin
,用于所有用户都可以使用的程序。如果应用程序仅供管理员使用,则应使用
/usr/sbin/
目录。请记住使用 chmod 设置“可执行文件”标志:
chmod +x your_app
Just copy your program to your systems bin (executable binaries) directory.
Most commonly its
/usr/bin
for programs which can be used by all user.If the app is only for admins, you should use
/usr/sbin/
directory.Remember to set the "executable" flag with chmod:
chmod +x your_app
对此的正确解决方案(假设您不希望从 makefile 外部运行
sp
)是使用完整路径名而不是./
调用您的程序(这是相对的,并且可以在多目录创建期间更改)。在你的 makefile 中执行如下操作:其中
$(shell pwd)
将扩展到运行 makefile 的目录。如果您的 sp 目录位于其父目录中,则也可以在路径中使用..
:例如。如果您确实想从 makefile 外部运行 sp,那么您需要将 sp 复制到 PATH 变量中指定的目录(执行
echo $PATH
来查看这些),或者修改您的 .bashrc或等效文件以使 PATH 包含 sp 内置的目录。John
The proper solution to this (assuming you don't want
sp
to be run from outside of your makefile) is to call your program using the full path name instead of./
(which is relative, and can change during multi-directory makes). In your makefile do something like:Where
$(shell pwd)
will expand to the directory the makefile is being run from. If your sp directory is in a parent directory of this, it is possible to use..
in the path as well: eg.If you do want to run sp from outside of the makefile, then you need to either copy sp to a directory specified in your PATH variable (do
echo $PATH
to see these), or modify your .bashrc or equivalent file to make PATH include the directory that sp is built in.John
您可以这样做:
但这通常不是一个好主意。
You can just do:
But this is not a good idea, in general.