纯C代码+ C编译器=跨平台代码?

时间:2022-03-06 12:13:51

If I have a code, fully written in C, using only libraries written also in C, and I have a compiler, like GCC, supporting many platforms, can I be sure that this code will run for any architecture supported by compiler? For example, can I take Flex or CPython , compile and use it, let say, at AVR?

如果我有一个完全用C编写的代码,只使用同样用C编写的库,并且我有一个编译器(如GCC),它支持许多平台,我能确保这个代码可以运行在任何编译器支持的架构中吗?例如,我可以使用Flex或CPython来编译并使用它,比方说,在AVR上?

Edit:

编辑:

  • compile and run, of course
  • 当然是编译和运行
  • no GUI
  • 没有GUI

5 个解决方案

#1


3  

"fully written in C" doesn't guarantee in any way that the code is portable. A portable compiler like GCC abstracts away the CPU architecture details, but the moment you use a system call specific to a particular OS, your code becomes unportable unless you surround the fragment in an #ifdef WHATEVER_OS. This is why standards like POSIX have emerged to unify the system call interface across different operating systems.

“完全用C编写”并不能保证代码是可移植的。像GCC这样的可移植编译器会抽象出CPU体系结构的细节,但是当您使用特定于特定OS的系统调用时,您的代码将变得不可移植,除非您将片段包围在#ifdef WHATEVER_OS中。这就是为什么出现了POSIX这样的标准来统一跨不同操作系统的系统调用接口。

Limiting your code to the POSIX-defined system calls and using a POSIX-compliant operating system should generally stop you from worrying, with little exceptions.

将代码限制在posix定义的系统调用,并使用与posix兼容的操作系统通常会使您不必担心,几乎没有例外。

#2


5  

No, it's hard to say without seeing your code. For example, if you depend on that long is 4 bytes, it won't be right on 64bit machine.

不,如果没有看到您的代码,很难说。例如,如果您依赖的长是4字节,那么它在64位机器上就不是正确的。

#3


3  

source code portability and compilability should be granted in your scenario.

在您的场景中应该授予源代码的可移植性和可编译性。

things might change if you would use external libraries or GUI frameworks which depend on the specific OS but this is not your case you you should be good to go.

如果您使用依赖于特定操作系统的外部库或GUI框架,那么事情可能会发生变化,但这不是您应该去做的事情。

#4


2  

The response is "they'll probably compile but there is a possibility they won't run". The problem is the resources available to you. Let's say you program for your 2gb machine. Will your program run on a 256mb machine? Could a DOS machine run CPython? But they'll probably compile :-) (technically you could have a program (the code) too much big to fit in the address space of the target machine. If your .exe/.out is 18mb and the target machine has an address space of 16mb, you can't even compile)

他们的回答是“他们可能会编译,但也有可能不会运行”。问题在于你拥有的资源。假设你为2gb的机器编程。你的程序会在256mb的机器上运行吗?DOS机器能运行CPython吗?但是它们可能会编译:-)(技术上来说,您可能会有一个程序(代码)太大,无法适应目标机器的地址空间。如果你的. exe /。输出是18mb,目标机器的地址空间是16mb,甚至不能编译)

#5


2  

Simply using C doesn't guarantee that the code is portable to any platform that supports C. There's a boat-load of traps to step into, like dependence on type-sizes, endianess or undefined behavior.

简单地使用C并不保证代码可以移植到任何支持C的平台上。有大量的陷阱可以进入,比如对类型大小的依赖、意外的或未定义的行为。

In reality, a non-trivial program is rarely portable to much except the platforms you have actually verified that it runs on. But you can certainly take measures to try and decrease the chance of problems, though.

实际上,一个非平凡的程序很少可移植到很多地方,除了您实际验证过它运行的平台。但是你当然可以采取措施来减少问题的发生。

#1


3  

"fully written in C" doesn't guarantee in any way that the code is portable. A portable compiler like GCC abstracts away the CPU architecture details, but the moment you use a system call specific to a particular OS, your code becomes unportable unless you surround the fragment in an #ifdef WHATEVER_OS. This is why standards like POSIX have emerged to unify the system call interface across different operating systems.

“完全用C编写”并不能保证代码是可移植的。像GCC这样的可移植编译器会抽象出CPU体系结构的细节,但是当您使用特定于特定OS的系统调用时,您的代码将变得不可移植,除非您将片段包围在#ifdef WHATEVER_OS中。这就是为什么出现了POSIX这样的标准来统一跨不同操作系统的系统调用接口。

Limiting your code to the POSIX-defined system calls and using a POSIX-compliant operating system should generally stop you from worrying, with little exceptions.

将代码限制在posix定义的系统调用,并使用与posix兼容的操作系统通常会使您不必担心,几乎没有例外。

#2


5  

No, it's hard to say without seeing your code. For example, if you depend on that long is 4 bytes, it won't be right on 64bit machine.

不,如果没有看到您的代码,很难说。例如,如果您依赖的长是4字节,那么它在64位机器上就不是正确的。

#3


3  

source code portability and compilability should be granted in your scenario.

在您的场景中应该授予源代码的可移植性和可编译性。

things might change if you would use external libraries or GUI frameworks which depend on the specific OS but this is not your case you you should be good to go.

如果您使用依赖于特定操作系统的外部库或GUI框架,那么事情可能会发生变化,但这不是您应该去做的事情。

#4


2  

The response is "they'll probably compile but there is a possibility they won't run". The problem is the resources available to you. Let's say you program for your 2gb machine. Will your program run on a 256mb machine? Could a DOS machine run CPython? But they'll probably compile :-) (technically you could have a program (the code) too much big to fit in the address space of the target machine. If your .exe/.out is 18mb and the target machine has an address space of 16mb, you can't even compile)

他们的回答是“他们可能会编译,但也有可能不会运行”。问题在于你拥有的资源。假设你为2gb的机器编程。你的程序会在256mb的机器上运行吗?DOS机器能运行CPython吗?但是它们可能会编译:-)(技术上来说,您可能会有一个程序(代码)太大,无法适应目标机器的地址空间。如果你的. exe /。输出是18mb,目标机器的地址空间是16mb,甚至不能编译)

#5


2  

Simply using C doesn't guarantee that the code is portable to any platform that supports C. There's a boat-load of traps to step into, like dependence on type-sizes, endianess or undefined behavior.

简单地使用C并不保证代码可以移植到任何支持C的平台上。有大量的陷阱可以进入,比如对类型大小的依赖、意外的或未定义的行为。

In reality, a non-trivial program is rarely portable to much except the platforms you have actually verified that it runs on. But you can certainly take measures to try and decrease the chance of problems, though.

实际上,一个非平凡的程序很少可移植到很多地方,除了您实际验证过它运行的平台。但是你当然可以采取措施来减少问题的发生。