操作系统编程

发布于 2024-12-01 01:03:02 字数 548 浏览 1 评论 0原文

首先:我是 C/C++ 的完全初学者(尽管我有 PHP/Javascript 经验)。我有一些关于语言和操作系统的问题。

C/C++是独立于操作系统的语言吗?例如,我用 C/C++ 编写的所有程序在所有操作系统中运行时都可以工作吗?

我是否需要特定于操作系统的编译器才能使我的程序在其上运行?

在为不同的操作系统编写程序时,有什么特定的事情需要解决吗? (任何语法更改,或者必须有特定的软件等)

将来我想创建自己的某种视频游戏,在阅读时,我注意到 DirectX 适用于 Windows,而 OpenGL 是多平台的。那么假设某些库依赖于操作系统是否正确?我在某处读到操作系统监视对某些位置的访问以提高安全性,从而防止直接访问视频卡。

为特定操作系统编写代码是否值得我花时间?或者是否会在某个时刻我应该对自己说,添加额外的代码会使我的程序进度变得复杂/混乱/阻碍并维护它以供将来使用?

最后,我应该如何设计我的程序以支持多个操作系统?我认为将操作系统特定的代码尽可能分开是理想的。对于很多程序,我会根据您的操作系统看到单独的下载链接。所以我很好奇他们如何管理(以供将来更新)每个代码。

First: I'm a complete beginner to C/C++ (although I have experience in PHP/Javascript). I have a few questions regarding the language and operating systems.

Is C/C++ an operating system independant language? As in, do all the programs that I write in C/C++ work when run in all operating systems?

Would I need an OS specific compiler to make my program run on it?

Are there any specific things that I need to address when writing a program for different OSes? (any syntax changes, or having to have specific software, etc)

In the future I would like to create my own video game of some sort, and while reading, I noted that DirectX works for windows and OpenGL is multiplatform. So is it correct to assume that some libraries are OS dependant? I have read somewhere that operating systems monitor access to certain places to increase security which prevents direct access of the video card.

Is coding for a particular OS considered worth my time? Or will there be a point in which I should say to myself that adding the extra code would complicate/clutter/hinder my progress with the program and maintaining it for future use?

And lastly, how should I design my program to work with multiple OSes? I would assume that keeping OS specific code as separate as possible would be ideal. For a lot of programs, I see separate download links based on your OS. So I'm curious how they manage (for future updates) the code for each one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-12-08 01:03:02

我的回答主要是关于C的,但是C++的可移植性应该是类似的。

C/C++ 是一种独立于操作系统的语言吗?

是的,语言本身(如 ISO 标准中所述)是独立于系统且可移植的。只有当您按照标准编写程序并且不使用任何外部函数时,才会出现这种情况(标准有一个函数列表,每个编译环境都必须实现这些函数)

例如,我用 C/C++ 编写的所有程序在所有操作系统中运行时都可以工作吗?

不。您可以编写不可移植的代码(例如,在标准中声明为未定义行为)。您还可以使用特定于操作系统的函数,例如 CreateProcessmmap,这将限制您的程序可移植性。

我需要特定于操作系统的编译器才能使我的程序在其上运行吗?

您需要一个针对您的操作系统的编译器。此外,您还需要一个标准 C 库(例如 BSD libc、GNU glibc、Android boinic 等)来实现标准函数。

在为不同操作系统编写程序时,有什么需要解决的具体问题吗? (任何语法更改,或必须有特定的软件等)

您应该以可移植的方式、良好的风格编写程序。有很多困难的时刻,例如,您不能假设 int 是 4 字节长,或者可以将 void* 转换为 int。您不需要任何额外的软件。

如果您想使用任何外部库,您应该知道该库是否可移植。

那么假设某些库依赖于操作系统是否正确?

有点儿。一些库被移植到许多操作系统。可移植性存在一些限制。

My answer mainly about C, but C++ portability should be similar.

Is C/C++ an operating system independant language?

Yes, the language itself (as described in ISO standard) is system-independent and portable. This is true only when you write programs according to standard and don't use any external functions (standard has a list of functions, which must be implemented by each compiling environment)

As in, do all the programs that I write in C/C++ work when run in all operating systems?

No. You may write a non-portable code (e.g. which is declared as undefined-behavior in standard). You may also use OS-specific functions, like CreateProcessor mmap, which will limit your program portability.

Would I need an OS specific compiler to make my program run on it?

You need a compiler, that targets your OS. Also you need a standard C library (e.g. BSD libc, GNU glibc, Android boinic, etc) which will implement functions from standard.

Are there any specific things that I need to address when writing a program for different OSes? (any syntax changes, or having to have specific software, etc)

You should write program in portable way, in good style. There are a lot hard moments, e.g. you can't assume that int is 4-byte long or that void* can be casted to int. You need no any additional software.

If you want to use any external library, you should know, is the library portable, or not.

So is it correct to assume that some libraries are OS dependant?

Kind of. Some libraries are ported to many OS. There are some limits on portability.

如痴如狂 2024-12-08 01:03:02

C/C++ 是独立于操作系统的语言。当然,您需要一个适合您要运行的架构和操作系统的编译器;为 Windows 编译的程序无法在 Linux 上运行,反之亦然。
(可以说)最好的编译器是 GCC,它也适用于 Windows(请参阅 MinGW),并且默认包含在大多数 Linux 发行版中(尽管您可能需要在某些发行版上单独安装它)。

关于库,是的,有些库仅在 Windows 上运行。。在 Visual Studio 中用 C++ 编写并使用其专有类的程序将无法在 Linux 上运行/编译。如果你想为游戏提供多平台支持,请使用 OpenGL 而不是 DirectX,这会让你的生活更轻松。

我会将程序分成多个分支,一个用于 Windows,一个用于 Linux,一个用于 Mac OS,因为所有这些都有不同的 GUI 设计方法(除非您决定使用 Qt 等跨平台工具包) 。

希望这有帮助:)

C/C++ are operating system independent languages. Of course you need a compiler for the architecture and OS you want to run; a program compiled for Windows will not run on Linux, and vice-versa.
The (arguably) best compiler is GCC, which is also available for Windows (see MinGW) and is included by default in most linux distributions (although you may have to install it separately on some).

Regarding the libraries, yes, there are libraries which work on Windows only. A program which you write in C++ in Visual Studio and which uses its proprietary classes will not work/compile on Linux. If you want to have multi-platform support for games, use OpenGL instead of DirectX, it will make life easier for you.

I would split the program into multiple branches, one for Windows, one for Linux and one for Mac OS, since there are different GUI design methods with all of these (except if you decide to go with a cross-platform toolkit such as Qt).

Hope this helps :)

有木有妳兜一样 2024-12-08 01:03:02

C/C++ 是一种独立于操作系统的语言吗?

是的。

例如,我用 C/C++ 编写的所有程序在所有操作系统中运行时都可以工作吗?

如果您的操作系统有编译器/链接器并且您仅使用纯 c 或 c++ - 是的。但请注意,有许多依赖于操作系统的库(包括来自操作系统本身的 API),这些库当然不可能在其他操作系统中使用。存在许多库来隐藏操作系统差异,例如 boost。

我需要特定于操作系统的编译器才能使我的程序在其上运行吗?

是的。

那么假设某些库依赖于操作系统是否正确?

是的,见上文。

在为不同操作系统编写程序时,有什么需要解决的具体问题吗? (任何语法更改,或必须有特定的软件等)

语法是相同的。请记住操作系统 API 或特定库。

Is C/C++ an operating system independant language?

Yes it is.

As in, do all the programs that I write in C/C++ work when run in all operating systems?

If there is a compiler / linker for your OS and if you are only using plain c or c++ - yes. But be aware that there are many OS depending libraries (including API's from the OS itself) which of course are not possible to use in an other OS. Many libraries are existing to hide the OS differences, e.g. boost.

Would I need an OS specific compiler to make my program run on it?

Yes.

So is it correct to assume that some libraries are OS dependant?

Yes, see above.

Are there any specific things that I need to address when writing a program for different OSes? (any syntax changes, or having to have specific software, etc)

The syntax is the same. Keep the OS API or specific libraries in mind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文