Why do we have to recompile the C source code for different operating systems on the same machine?

When I compile my c source code (for example, in a Linux environment), the compiler generates the file in a “machine-readable” format.

>Why can’t the same file run on the same machine under different operating systems?
>Is there something wrong with the way we “execute” this file?

Sometimes it will work, depending on the format and library you use, etc. For example, assign OS functions are called for memory or window creation. Therefore, you must compile for the target operating system and link these libraries together (static or dynamic).

However, the description itself is the same So, if your program does not use any OS features (no standard or any other libraries), you can run it on another operating system. The second thing in question here is the executable format. Windows .exe and For example, ELF is very different. However, only the flat format of instructions (such as .com) is applicable to all systems.

Edit: An interesting experiment is to compile some functions on an operating system (such as Windows) In a flat format (for illustration only). For example:

int add(int x, int y) {return x + y; }

The instructions are saved in a file without any relocation or other temporary storage information. Then, a complete program is compiled on a different operating system (such as Linux), and it will perform the following operations:

typedef int (*PFUNC)(int, int); // pointer to a function like our add one

PFUNC p = malloc(200); // make sure you have enough space .
FILE *f = fopen("add.com", "rb");
fread(p, 200, 1, f); // Load the file contents into p
fclose (f);
int ten = p(4, 6);

In order to achieve this, you also need to tell the operating system/compiler that you want to be able to execute the allocated memory, I don’t I know how to do it, but I know it can be done.

When I compile my c source code (e.g. in a Linux environment), the compiler says “machine readable “Format to generate files.

>Why can’t the same file run on the same machine under different operating systems?
>Is there something wrong with the way we “execute” this file?

Sometimes it will work, depending on the format and library you use, etc. For example, allocating memory or creating a window will call OS functions. Therefore, You must compile for the target operating system and link these libraries together (static or dynamic).

However, the description itself is the same. Therefore, if your program does not use any OS Function (no standard or any other library), you can run it on another operating system. The second thing in question here is the executable format. Windows .exe is very different from e.g. ELF. However, there are only instructions (e.g.. com) is suitable for all systems.

Edit: An interesting experiment is to compile some functions into a flat format on an operating system (such as Windows) (just for instructions). For example:

p>

int add(int x, int y) {return x + y; }

Just save the instruction to the file without any relocation or other Temporary information. Then, compile a complete program on a different operating system (such as Linux), it will perform the following operations:

typedef int (*PFUNC)(int , int); // pointer to a function like our add one

PFUNC p = malloc(200); // make sure you have enough space.
FILE *f = fopen(" add.com", "rb");
fread(p, 200, 1, f); // Load the file contents into p
fclose(f);
int ten = p (4, 6);

In order to achieve this, you also need to tell the operating system/compiler that you want to be able to execute the allocated memory. I don’t know how to do it, but I know it can be done.< /p>

Leave a Comment

Your email address will not be published.