2

basically, I like to get the type of the CPU architecture the app is running on similar to node.js os.arch() or process.arch
it can be any C++/C library that i can link to my app but not Qt as i can't use it

update

this is what in node.js

 function arch(): string;
    /**
     * Returns a string identifying the kernel version.
     * On POSIX systems, the operating system release is determined by calling
     * [uname(3)][]. On Windows, `pRtlGetVersion` is used, and if it is not available,
     * `GetVersionExW()` will be used. See
     * https://en.wikipedia.org/wiki/Uname#Examples for more information.
     */
user63898
  • 29,839
  • 85
  • 272
  • 514
  • 2
    The only thing I've seen that's somewhat related is [getting number of threads supported](https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency). I would imagine that everything beyond that is dependant on the OS and would need to be dealt with for each case. – super Feb 04 '21 at 16:06
  • [`std::hardware_destructive_interference_size` & `std::hardware_constructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size) are also somewhat hardware related but I think gcc has refused to implement those. – Ted Lyngmo Feb 04 '21 at 16:18
  • 1
    @user63898 I didn't downvote - but it can't be done using only standard C++. You'll have to use OS specific functions. – Ted Lyngmo Feb 04 '21 at 16:19
  • 1
    what platform is this for? – Mansoor Feb 04 '21 at 16:20
  • 4
    Doesn't make much sense, unlike Javascript C++ is a compiled language. With cpu-specific machine code as the compile result. So you have to pick the architecture *before* you compile. At runtime you would just get back what you already knew before. – Hans Passant Feb 04 '21 at 16:45
  • Remember, there are desktops, laptops and *embedded systems*. I know a lot of embedded systems that run C++, but don't disclose any information about their processors or system on a chip. – Thomas Matthews Feb 04 '21 at 18:16
  • how the node.js do it ? it can be also C/C++ library that do it ( no Qt ) – user63898 Feb 04 '21 at 20:12
  • @user63898 they probably use OS Apis like Window's `GetSystemInfo`, or Linux's `/proc/cpuinfo`. So it depends on what platform you're compiling for – Mooing Duck Feb 04 '21 at 20:33
  • update the question on how nodejs do it – user63898 Feb 04 '21 at 20:46

1 Answers1

1

You can't do that with standard C++. If you use Qt, QSysInfo::currentCpuArchitecture() does exactly what you want.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 1
    How does this work? There are embedded systems that run C++ and don't broadcast anything about their processors or how the addressing is mapped. – Thomas Matthews Feb 04 '21 at 18:15
  • 2
    @ThomasMatthews it obviously only works on systems Qt supports. – Aykhan Hagverdili Feb 04 '21 at 18:16
  • how does Qt do it ? is there some c/c++ library that extract the code from them ? i can't use Qt – user63898 Feb 04 '21 at 20:13
  • @user63898 Qt probably wraps around the OS-specific code. Qt is opensource, so you can check out their source coded to see how it's exactly done. – Aykhan Hagverdili Feb 04 '21 at 20:14
  • 1
    @user63898: For just the arch, probably it's a big `#ifdef __x86_64__` / `#ifdef __arm__` / etc. chain for all the architectures Qt supports so hopefully that function can inline as a compile-time-constant string. (Maybe some per-compiler variation to which macros to check for.) OTOH the linked docs say " due to the nature of the operating system functions being used", which may mean at runtime (like `uname`), but might just mean at Qt config time to hard-code a value. – Peter Cordes Feb 04 '21 at 20:18
  • @user63898 Alternatively, they can use OS Apis like Window's [`GetSystemInfo`](https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo), or Linux's `/proc/cpuinfo` – Mooing Duck Feb 04 '21 at 20:30
  • @MooingDuck Yes, this has been asked for Windows previously [How can I dynamically get the system architecture?](https://stackoverflow.com/q/4073815/10147399). Qt probably does something like that on each system. – Aykhan Hagverdili Feb 04 '21 at 20:33
  • i updated ths question with the solution on how node.js do it – user63898 Feb 04 '21 at 20:45
  • 1
    @user63898 [Here](https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/global/qglobal.cpp?h=5.9#n2447) is how Qt does it. It's a messy bit of code. – Aykhan Hagverdili Feb 04 '21 at 20:55
  • here is how CommonCpp do it : https://www.programcreek.com/cpp/?code=chronoxor%2FCppCommon%2FCppCommon-master%2Fsource%2Fsystem%2Fenvironment.cpp – user63898 Feb 04 '21 at 21:42