Jump to content

Is C++ OS dependant or does it go farther than that?

Go to solution Solved by igormp,
26 minutes ago, Hi P said:

1. Does it mean that it will work in just about any machine as long as it's Windows 11?

 

2. Would it work on any Windows version? Like Windows 10

Only if you static linked it. Otherwise, if you had dynamic links and libraries that you linked to (like the libc) had an API change across cresions, then your binary is going to break.

27 minutes ago, Hi P said:

3. What if I developed it in Ubuntu 22, would it work in future and past Ubuntu versions?

 

4. Would it work on different Linux distros?

Same as above, if you're running your compiled code that was compiled against X glibc and Y kernel ABI, then it should work in any distro or version that is compatible with those versions, otherwise it'll break. If you static link it (as in, those libraries get "copied" inside your binary), then it'll run as-is across most distroes, even with different versions.

28 minutes ago, Hi P said:

I have seen a handful of different compilers for C++ , hence the question

 

Compilers aren't really what makes a binary have more compatibility or not, but rather the libraries you link your code with and how you do so.

This is mostly out of curiosity 🙂

 

Let's say that I write some functionality in C++ and it works in Windows 11 just fine.

 

1. Does it mean that it will work in just about any machine as long as it's Windows 11?

 

2. Would it work on any Windows version? Like Windows 10

 

3. What if I developed it in Ubuntu 22, would it work in future and past Ubuntu versions?

 

4. Would it work on different Linux distros?

 

I have seen a handful of different compilers for C++ , hence the question

 

 

Tagging @wasab as I have seen him mention this language

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, Hi P said:

1. Does it mean that it will work in just about any machine as long as it's Windows 11?

 

2. Would it work on any Windows version? Like Windows 10

Only if you static linked it. Otherwise, if you had dynamic links and libraries that you linked to (like the libc) had an API change across cresions, then your binary is going to break.

27 minutes ago, Hi P said:

3. What if I developed it in Ubuntu 22, would it work in future and past Ubuntu versions?

 

4. Would it work on different Linux distros?

Same as above, if you're running your compiled code that was compiled against X glibc and Y kernel ABI, then it should work in any distro or version that is compatible with those versions, otherwise it'll break. If you static link it (as in, those libraries get "copied" inside your binary), then it'll run as-is across most distroes, even with different versions.

28 minutes ago, Hi P said:

I have seen a handful of different compilers for C++ , hence the question

 

Compilers aren't really what makes a binary have more compatibility or not, but rather the libraries you link your code with and how you do so.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

Everything @igormp said is correct - if the program uses shared libraries (.dll files on Windows and .so files on Linux), then the machine you want it to run on must have those libraries installed. In addition to that, there are library functions that Windows has while Linux doesn't and vice versa. Some examples of this would be the Win32 API on Windows and mmap() on POSIX compliant systems, although there are ways to get around those incompatibilities.

 

Lastly, different operating systems will use different executable file formats that aren't interchangeable for the most part. For example, Linux uses ELF files and Windows uses PE32+ files. The compiler you use will have to target one of these file formats to have its output be able to run on your system.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

really depends on the program or app that is being developed usually if the program uses something that is only specific to the OS such as the windows api it would not work on linux(unless its ported). That being said many factors can come into play such has hardware compatibility or the CPU architecture.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...
On 11/12/2023 at 3:33 AM, Hi P said:

This is mostly out of curiosity 🙂

 

Let's say that I write some functionality in C++ and it works in Windows 11 just fine.

 

1. Does it mean that it will work in just about any machine as long as it's Windows 11?

 

2. Would it work on any Windows version? Like Windows 10

 

3. What if I developed it in Ubuntu 22, would it work in future and past Ubuntu versions?

 

4. Would it work on different Linux distros?

 

I have seen a handful of different compilers for C++ , hence the question

 

 

Tagging @wasab as I have seen him mention this language

C++ Does not work the same. Borland was great back in the day…

still kind of exists in ASM remnants

however visual c++ requires runtimes.

gcc is not necessarily the same as many other C languages and variants (objC g++ etc)

depending on the library and headers and others required

 

however some IDEs etc are capable of porting to other OSes but may have some limitations…

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...
On 12/24/2023 at 8:58 PM, Red Nova said:

Making your code cross-platform is a hell you don't want to get into.

Meh, it isn't that bad nowadays. Unless you start using a lot of third-party libraries and they aren't cross-platform. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Ă—