如何在Makefile中获取用户名?

时间:2022-12-03 22:51:32

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中,在'install'目标中,我添加了:

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

P.S。:我正在手工编写Makefile。没有./configure

P.S.2: I dont want to do

P.2.2:我不想这样做

mkdir -p ~/.game_name

because i want to be able to change DESTDIR if i want to install to a temporary directory.

因为如果我想安装到临时目录,我希望能够更改DESTDIR。

2 个解决方案

#1


11  

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.

在运行时,您可以获得如此处所示的主目录(如果您使用的是C / C ++)。然后,您可以检查highscore文件夹/文件是否存在,并在需要时创建它。如果您想为所有用户提供高分文件,则应将其放在特殊用户的主目录中,而不是文件系统中的其他位置。这例如由游戏xjump完成。

And about your Makefile, the $USER variable is translated to root because of the sudo command on

关于你的Makefile,由于sudo命令打开,$ USER变量被转换为root

sudo make install

sudo make install

The user actually running make is the superuser: root.

实际运行make的用户是超级用户:root。

#2


4  

You want to install your program for every users? So it's better if you create the /home/user/.game_name directory 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 files at 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或其他任何东西),或者只调用执行它的外部脚本(不那么优雅)。避免在安装时创建与用户相关的文件。

#1


11  

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.

在运行时,您可以获得如此处所示的主目录(如果您使用的是C / C ++)。然后,您可以检查highscore文件夹/文件是否存在,并在需要时创建它。如果您想为所有用户提供高分文件,则应将其放在特殊用户的主目录中,而不是文件系统中的其他位置。这例如由游戏xjump完成。

And about your Makefile, the $USER variable is translated to root because of the sudo command on

关于你的Makefile,由于sudo命令打开,$ USER变量被转换为root

sudo make install

sudo make install

The user actually running make is the superuser: root.

实际运行make的用户是超级用户:root。

#2


4  

You want to install your program for every users? So it's better if you create the /home/user/.game_name directory 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 files at 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或其他任何东西),或者只调用执行它的外部脚本(不那么优雅)。避免在安装时创建与用户相关的文件。