Linux 如何在 Makefile 中获取用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8234975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i get the user name in a Makefile?
提问by kure
I'm making a simple game for Ubuntu and to update the highscore list, it needs a single file at runtime, called 'highscores.bin'.
我正在为 Ubuntu 制作一个简单的游戏并更新高分列表,它在运行时需要一个名为“highscores.bin”的文件。
I wish to put this file at
我想把这个文件放在
/home/(USER)/.game_name
I've researched a little and found that from inside a Makefile i can get the environment variable $USER. So in the Makefile, at the 'install' target, i've added:
我进行了一些研究,发现从 Makefile 内部我可以获得环境变量 $USER。因此,在 Makefile 中的“安装”目标处,我添加了:
mkdir -p $(DESTDIR)home/$$USER/.game_name
But when i run 'sudo make install', the Makefile installs it as:
但是当我运行“sudo make install”时,Makefile 将其安装为:
/home/root/.game_name
How can i get the (non-root) user name in a Makefile?
如何在 Makefile 中获取(非 root)用户名?
P.S.: I'm writing the Makefile by hand. No ./configure
PS:我正在手工编写Makefile。没有 ./configure
P.S.2: I dont want to do
PS2:我不想做
mkdir -p ~/.game_name
because i want to be able to change DESTDIR if i want to install to a temporary directory.
因为如果我想安装到临时目录,我希望能够更改 DESTDIR。
采纳答案by Exp
Well, if you want that file during runtime I would recommend that you create it during runtime. The Makefile is considered only during compile time. This is obviously a problem if the program is compiled and then executed by different users, some of which may not even exist at compile time.
好吧,如果您在运行时需要该文件,我建议您在运行时创建它。Makefile 仅在编译时考虑。如果程序由不同的用户编译然后执行,这显然是一个问题,其中一些在编译时甚至可能不存在。
During runtime you can get the home directory as shown here(if you are using C/C++). Then you can check for existance of the highscore folder/file and create it if you need to. If you want a highscore file for all users, you should put it in the home directory of a special user, but rather somewhere else in the file system. This is for example done by the game xjump.
在运行时,你可以得到如图所示的home目录这里(如果你使用的是C / C ++)。然后,您可以检查高分文件夹/文件是否存在,并在需要时创建它。如果你想要一个所有用户的高分文件,你应该把它放在一个特殊用户的主目录中,而不是文件系统中的其他地方。例如,这是由游戏 xjump 完成的。
And about your Makefile, the $USER variable is translated to root because of the sudocommand on
关于您的 Makefile,由于sudo命令 ,$USER 变量被转换为 root
sudo make install
须藤制作安装
The user actually running make is the superuser: root.
实际运行 make 的用户是超级用户:root。
回答by Alessandro Pezzato
You want to install your program for every users? So it's better if you create the /home/user/.game_namedirectory when the user run the game, not when root installs it. Otherwise, only the user who call sudo make install
will have it's highscore list. If you have only one user, do not install it system-wide, but in the user directory.
您想为每个用户安装您的程序吗?所以最好在用户运行游戏时创建/home/user/.game_name目录,而不是在 root 安装它时创建。否则,只有呼叫的用户sudo make install
才会有它的高分列表。如果您只有一个用户,请不要将其安装在系统范围内,而是安装在用户目录中。
I suggest to initialize users filesat game first run. So every users, even users that didn't exist when the game was installed, can have their highscore list. You can add the initializing code into your game (c++, java or anything else) or just call an external script that does it (not so elegant). Avoid to create user-related files at installation time.
我建议在游戏第一次运行时初始化用户文件。因此,每个用户,甚至是安装游戏时不存在的用户,都可以拥有他们的高分列表。您可以将初始化代码添加到您的游戏中(c++、java 或其他任何东西),或者只是调用一个执行它的外部脚本(不太优雅)。避免在安装时创建与用户相关的文件。