如何判断是否为当前机器编译了可执行文件?

时间:2022-10-08 04:54:35

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.

我有一些编译和运行的c代码,在一个可以从许多不同的unix计算机(各种linux和mac,有时是其他的)访问的目录中,不同的操作系统显然需要不同的可执行文件。

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.

我有一个简单的shell脚本调用适当的可执行文件prog。$ OSTYPE。$ MACHTYPE,如有必要,首先编译它。这非常简单(虽然它需要使用csh才能可靠地定义$ OSTYPE和$ MACHTYPE)并且它几乎可以工作。

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.

然而,事实证明即使是$ OSTYPE和$ MACHTYPE也是不够的:例如,在OSX 10.5上编译会产生一个可执行文件prog.darwin.i386,当在OS​​X 10.4上调用时,它会立即崩溃。

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).

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

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?

所以我的问题是,有更好的方法吗?编译器(通常是gcc)显然知道它正在编译什么类型的系统 - 是否有一种很好的可移植方式来找出我的脚本运行的“类型系统”,因此它可以调用正确的可执行文件,而不是一个未定义的行为?

3 个解决方案

#1


2  

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)

您可以使用gcc -v来确定安装/可运行的gcc认为是托管编译的目标arch(类似于$(gcc -v 2>&1 | grep目标:| sed的/.*:* //') )在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...

如果你真的想在没有特别安装的情况下做到这一点,你可以从gcc(它在任何gcc源包的*目录中)中提取config.guess脚本并运行它。不幸的是,这不适用于所有系统,可能与系统gcc包用于某些发行版的内容完全匹配,但这是用于配置gcc进行构建的脚本,除非您明确覆盖它...

#2


2  

try to use file command from shell prompt

尝试从shell提示符使用file命令

#3


0  

Download some open source packages written in C and have a look at the file ./configure which is basically a large shell script that collects info from many sources, often by compiling and running short C programs. This will tell you everything that you need to know.

下载一些用C编写的开源软件包,并查看文件./configure,它基本上是一个大型shell脚本,通常通过编译和运行短C程序从许多来源收集信息。这将告诉您需要知道的一切。

Since you are dealing with recent Mac OS X make sure you choose a package that is currently being maintained and supports OS X versions.

由于您正在处理最近的Mac OS X,请确保选择当前正在维护的软件包并支持OS X版本。

#1


2  

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)

您可以使用gcc -v来确定安装/可运行的gcc认为是托管编译的目标arch(类似于$(gcc -v 2>&1 | grep目标:| sed的/.*:* //') )在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...

如果你真的想在没有特别安装的情况下做到这一点,你可以从gcc(它在任何gcc源包的*目录中)中提取config.guess脚本并运行它。不幸的是,这不适用于所有系统,可能与系统gcc包用于某些发行版的内容完全匹配,但这是用于配置gcc进行构建的脚本,除非您明确覆盖它...

#2


2  

try to use file command from shell prompt

尝试从shell提示符使用file命令

#3


0  

Download some open source packages written in C and have a look at the file ./configure which is basically a large shell script that collects info from many sources, often by compiling and running short C programs. This will tell you everything that you need to know.

下载一些用C编写的开源软件包,并查看文件./configure,它基本上是一个大型shell脚本,通常通过编译和运行短C程序从许多来源收集信息。这将告诉您需要知道的一切。

Since you are dealing with recent Mac OS X make sure you choose a package that is currently being maintained and supports OS X versions.

由于您正在处理最近的Mac OS X,请确保选择当前正在维护的软件包并支持OS X版本。