从二进制文件相对路径 [英] Relative paths from binary file

查看:221
本文介绍了从二进制文件相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件夹结构:

bin/ <-binary-file is in here
include/
src/
data/
Makefile

在我的code,我使用相对路径我的数据。因此,../data/xml/xmlFile.xml。如果我是执行从bin /文件夹中的二进制文件,这是好的:

In my code, I use relative paths to my data. So "../data/xml/xmlFile.xml". This is fine if I were executing the binary file from the bin/ folder:

brandonto@computer:~/PATH-TO-PROJECT/bin$ ./binary-file 
argv[0] = ./binary-file
dirname(argv[0]) = .

但是,如果我是执行从主文件夹中的二进制文件(或任何其他文件夹,不是的bin /文件夹):

But if I were executing the binary from the main folder (or any other folder that is not the bin/ folder):

brandonto@computer:~/PATH-TO-PROJECT$ bin/binary-file 
argv[0] = bin/binary-file
dirname(argv[0]) = bin

XML文件中不会被发现,因为../data现在会从主文件夹转到上一级目录(或者你是在执行程序时,在任何文件夹)。

The xml files would not be found because "../data" would now go up one directory from the main folder (or whatever folder you are in when executing the program).

我怎能让这个二进制文件可以从任何目录我的系统上执行?

How could I make it so that the binary file could be executed from any directory on my system?

为了使问题更加清楚一点:

To make the question a little more clear:

brandonto@brandonto-Aspire-S3-391:~/cpp-workspace/sdl-projects/sdl-space-shooter/bin$ ~/cpp-workspace/sdl-projects/sdl-space-shooter/bin/SpaceShooter 
argv[0] = /home/brandonto/cpp-workspace/sdl-projects/sdl-space-shooter/bin/SpaceShooter
dirname(argv[0]) = /home/brandonto/cpp-workspace/sdl-projects/sdl-space-shooter/bin

brandonto@brandonto-Aspire-S3-391:~/cpp-workspace/sdl-projects/sdl-space-shooter/bin$ cd ..
brandonto@brandonto-Aspire-S3-391:~/cpp-workspace/sdl-projects/sdl-space-shooter$ ~/cpp-workspace/sdl-projects/sdl-space-shooter/bin/SpaceShooter 
argv[0] = /home/brandonto/cpp-workspace/sdl-projects/sdl-space-shooter/bin/SpaceShooter
dirname(argv[0]) = /home/brandonto/cpp-workspace/sdl-projects/sdl-space-shooter/bin
Unable to load image ../data/graphics/background/darkPurple.png! SDL_image Error: Couldn't open ../data/graphics/background/darkPurple.png
Unable to load image ../data/graphics/sprites/meteorBrown_big1.png! SDL_image Error: Couldn't open ../data/graphics/sprites/meteorBrown_big1.png

在这里,我从bin /文件夹内从主文件夹中执行的二进制文件一次,然后一次。二进制运行从bin /文件夹内的罚款,但不能从主文件夹中找到的.png文件的相对路径。

Here, I executed the binary file once from inside the bin/ folder, then once from inside the main folder. The binary ran fine from inside the bin/ folder, but could not find the relative paths to the .png files from inside the main folder.

推荐答案

也许你问一个错误的问题:构建系统无关,与程序的执行

Probably you are asking a wrong question: the build system has nothing to do with program execution.

不过,如果你找一个答案,如何让我的程序正确地使用数据,即相对于位于程序的安装,比这里是一个答案。

However, if you look for an answer, how to make my program to correctly use data, that is located relative to program installation, than here is an answer.

在你的程序被执行,它得到的二进制文件的路径作为第一个参数(指数 0 )。这路径可以是相对或绝对的,但在任何情况下,它可以让你找到在基础目录。

When you program main gets executed, it gets the binary path as the first parameter (index 0). That path can be relative or absolute, but in any case it allows you to find the base directory.

这些也都是有用的链接:

These are also useful links:

  • how to find the location of the executable in C
  • Finding current executable's path without /proc/self/exe

在这里您可以如何使用第一个参数:

Here how you can use first argument:

#include <linux/limits.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>

int main(int argc, char *argv[])
{
  char datadir[PATH_MAX];
  strncpy(datadir, argv[0], sizeof(datadir));
  dirname(datadir);
  strncat(datadir, "/../data", sizeof(datadir));

  printf("Data dir: %s\n", datadir);

  return 0;
}

这篇关于从二进制文件相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆