1

I have different c++ executable files compiled for different architectures(x86_64,arm32 etc) with different names in a folder inside my python3 project. I would be using one of these when i run the project on the corresponding architecture. Currently I change the path of the executable file before running in various architectures. Could someone suggest me the way to :

  • find the machine architecture info (whether arm32,x86 etc) of these executable or get the executable compiled for the architecture in which the code is running or In which machine or architecture is the each of these executable is complied.

I tried to check the platform library in python,but i didn't get any valid solution . How do i achieve any one the above in Python3

Ajith
  • 87
  • 3
  • 12

2 Answers2

0

To find the architecture of the current platform, you want uname from the platform lib:

import platform

uname = platform.uname()
uname.machine

'x86_64'

EDIT: To find the target architecture of a compiled binary is more difficult. There's always an OS call to tell you if the binary was compiled to run on a particular architecture for that OS, but no easy find to find the compilation target of an arbitrary binary. Here are a few things you can do, depending on what platform you're on:

Here's an example from macOS:

import subprocess

result = subprocess.run(['file', '/bin/cat'], stdout=subprocess.PIPE)
result.stdout.decode("utf-8").strip().split(' ')

['/bin/cat:', 'Mach-O', '64-bit', 'executable', 'x86_64']
Dave
  • 1,579
  • 14
  • 28
  • No, this is to get the system platform. But, i would like to pick one executable from multiple executable's which is compiled for one architecture? How do i get that ? How do i know which executable is compiled for which architecture – Ajith May 26 '20 at 07:33
  • It's not simple, but can be done. Take a look at my edit. – Dave May 26 '20 at 13:27
  • I'd start with magic numbers for starters always appearing as first bytes in the executable. `MZ` for Windows/DOS, `ELF` for Linux, `FEEDFACF`/`FEEDFACE` for MacOS – Kamil.S May 28 '20 at 07:42
0

you can just use the bash command file, example:

$ file myExecutable

myExecutable: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), 
dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, 
BuildID[sha1]=3c3776317ac80168ec7fb9b095e59c4ae256b6a5, for 
GNU/Linux 3.2.0, not stripped`
N. Joppi
  • 416
  • 5
  • 9