简体   繁体   English

是否可以为多平台(嵌入式或所有操作系统)编写C代码和库?

[英]is it possible to write c code and library, for multiplatform: embedded or all operating systems?

Is it possible to run a written code or written library that only uses initial c libraries, on every platform? 是否可以在每个平台上运行仅使用初始c库的书面代码或书面库?

For example: Windows, ARM Microprocessors, PIC microprocessors, 例如:Windows,ARM微处理器,PIC微处理器,

They have their compilers seperately and this difference is not important for me, I can compile in different compilers for need. 他们有各自的编译器,这种区别对我而言并不重要,我可以根据需要使用不同的编译器进行编译。 But do I have to change code totally or partially to run on this platforms? 但是我是否必须全部或部分更改代码才能在此平台上运行?

Note: For libraries, I will just use default c libraries. 注意:对于库,我将只使用默认的c库。

It depends. 这取决于。 If your library use only standard C and the multiplatform you are about to port has a compiler compatiable to standard C, you can always write the library code. 如果您的库仅使用标准C,并且要移植的多平台具有可与标准C兼容的编译器,则始终可以编写库代码。 But if your library have to call native API of each platform, you have to encapsulate these code seperately. 但是,如果您的库必须调用每个平台的本机API,则必须分别封装这些代码。

In embedded systems you will need to implement certain functions that the operating system gives you (like _sbrk , _read , etc.) for standard library functions like malloc and printf . 在嵌入式系统中,您将需要实现操作系统为您提供的某些函数(例如_sbrk_read等),以实现诸如mallocprintf类的标准库函数。

If you take care of that I don't see a reason for your code not to work so long as you take GREAT CARE in how you write it. 如果您能做到这一点,只要您对代码的编写方式非常满意,就不会发现您的代码无法正常工作的原因。 By GREAT CARE, I mean be very careful with floating points, processor word size and any other things that are not common between your desired targets. 我所说的“伟大的关怀”是指对浮点数,处理器字长以及所需目标之间不常见的任何其他事项要非常小心。

Short answer: possible, but not easy. 简短答案:可能,但并不容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM