如何判断可执行文件是否已针对本机进行编译? [英] How to tell whether an executable was compiled for the present machine?

查看:60
本文介绍了如何判断可执行文件是否已针对本机进行编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我可以编译并运行的c代码,可以在许多不同的unix计算机(各种linux和mac,有时是其他)上访问的目录中,并且不同的OS显然需要不同的可执行文件.

我有一个简单的shell脚本,可以调用相应的可执行文件 prog.$ OSTYPE.$ MACHTYPE ,并在必要时首先进行编译.这非常简单(尽管需要使用 csh 来可靠地定义 $ OSTYPE $ MACHTYPE ),并且几乎可以正常工作./p>

但是,事实证明,即使 $ OSTYPE $ MACHTYPE 还是不够的:例如,在OSX 10.5上进行编译会生成可执行文件 prog.darwin.i386 在OSX 10.4上调用时立即崩溃.

是的,每次我要运行程序时都重新编译是解决此问题的一种方法,但似乎过分了.我知道在每台计算机上都有一个 bin 目录是一种标准解决方案,但是非root用户可能在其主目录(这是所有计算机所共有的目录)之外没有太多写访问权限.

所以我的问题是,有没有更好的方法?编译器(通常是gcc)显然知道它要针对哪种系统进行编译-是否有一种很好的可移植方法来找出我的脚本正在运行的系统类型",因此它可以调用正确的可执行文件,而不是一个可执行文件.行为不确定?

解决方案

您可以使用 gcc -v 来确定已安装/可运行的gcc认为托管编译的目标是什么(类似 $(gcc -v 2>& 1 | grep目标:| sed's/.*:*//') in bash)

修改

如果您确实希望无需安装任何特殊软件就可以执行此操作,则可以从gcc(位于任何gcc源软件包的顶级目录中)提取 config.guess 脚本.并运行它.不幸的是,这不适用于所有系统,并且可能与gcc软件包在某些发行版中使用的系统不完全匹配,但这是用于配置gcc以进行构建的脚本,除非您显式覆盖它...

I have some c code that I compile and run, in a directory that is accessible from many different unix computers (various linux and mac, occasionally others), with different OS's obviously needing different executables.

I have a simple shell script that invokes the appropriate executable, prog.$OSTYPE.$MACHTYPE, compiling it first if necessary. This is very simple (although it requires using csh in order to have $OSTYPE and $MACHTYPE be reliably defined) and it almost works.

However, it turns out that even $OSTYPE and $MACHTYPE are not enough: for example, compiling on OSX 10.5 yields an executable prog.darwin.i386 which, when invoked on OSX 10.4, crashes instantly.

Yes, recompiling every time I want to run the program is one way to solve this, but it seems excessive. I know having a bin directory on every machine is a standard solution, but a non-root user may not have much write access outside their home directory (which is common to all the machines).

So my question is, is there a better approach? The compiler (often gcc) obviously knows what kind of system it is compiling for -- is there a good portable way to find out what "kind of system" my script is running on, so it can invoke the correct executable, instead of one with undefined behavior?

解决方案

You could use gcc -v to figure out what the installed/runnable gcc thinks is the target arch for hosted compiling (something like $(gcc -v 2>&1 | grep Target: | sed 's/.*: *//') in bash)

edit

If you really want to be able to do this without having anything in particular installed, you could extract the config.guess script from gcc (its in the top level directory of any gcc source package) and run that. Unfortunately this won't work for all systems and might not exactly match what the system gcc package uses for some distributions, but this is the script used to configure gcc for building unless you explicitly override it...

这篇关于如何判断可执行文件是否已针对本机进行编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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