ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The gcc command worked and the program executed
Thanks
Good, but I wanted to say something:
In another thread, you tried to execute C code using the shell. Did you try running the executable like this:
Code:
sh a.out
? That clearly won't work, because compiled code is machine code that runs on the CPU, not a bunch of shell commands. The shell doesn't run stuff (other than shell scripts), the kernel does.
I see that it's a common misconception that the shell is somehow strongly integrated into the OS, and that's simply wrong. It's just another program, there's nothing special about it. All it does is both provide an interactive environment that lets you easily run other programs, and let you automate things by putting the commands in a file instead of typing them interactively.
Hi
When I compile a program, The compiler throws out a binary file. SO my Question is "Can I take this binary file to any other PC and Run it there, Provided that, the new machine is a windows or linux based pc"????
If you have a 32bit Linux your program will run on any 32bit Linux-PC. 64bit respectively, but it is possible to run 32bit-programs on 64bit-systems when one has a multilib-environment.
Long story short: programs for Linux will only run on Linux. Programs for Windows will only run on Windows.
There are some exceptions: Java-programs run inside a virtual-machine and are independent of the operatingsystem. And Scripting-languages are not compiled but have an interpreter, so they are also independent of the OS.
Hi
When I compile a program, The compiler throws out a binary file. SO my Question is "Can I take this binary file to any other PC and Run it there, Provided that, the new machine is a windows or linux based pc"????
Quote:
Originally Posted by markush
It will run on a Linux-PC only.
If you have a 32bit Linux your program will run on any 32bit Linux-PC. 64bit respectively, but it is possible to run 32bit-programs on 64bit-systems when one has a multilib-environment.
Long story short: programs for Linux will only run on Linux. Programs for Windows will only run on Windows.
There are some exceptions: Java-programs run inside a virtual-machine and are independent of the operatingsystem. And Scripting-languages are not compiled but have an interpreter, so they are also independent of the OS.
Markus
Clarification: if may run on other Linux systems (if you compiled it under Linux), provided that the environments are similar. For example if the program uses libraries that are not present on the other system, then it won't run. If you're careful enough, there are good chances that it will. And if you write your code not to depend on any external libraries that are operating system specific, you can expect to be able to compile your code on Linux and Windows alike; typically, though, programs use system-specific libraries which means they cannot be "just compiled" on another OS (say, Linux -> Windows) and have it work. In such cases those "dependencies" have to be modified to work on the other system before compilation.
I had wrote and compiled this program tonight, although it got compiled but gave"segmentation fault" in its execution
Here is the program
Code:
#include <stdio.h>
int main(void)
{
int age;
char gender;
char name[10]=("\0");
char class[4]=("\0");
printf("Input your name \n");
scanf("%s",name);
printf("Input your Course Name \n");
scanf("%s",class);
printf("YOUR AGE???? \n");
scanf("%d",age);
printf("Gender \n");
scanf("%s",gender);
printf("Your Name is %s",name);
printf("You are studying in %s",class);
printf("Your gender is %s",gender);
printf("and you are %d years old",age);
getchar();
}
And this is the error
Code:
Input your name
Nabeel
Input your Course Name
BSc
YOUR AGE????
18
Gender
m
Segmentation fault
...
For example if the program uses libraries that are not present on the other system, then it won't run. If you're careful enough, there are good chances that it will.
...
This all depends on the sonames of the libraries you link to, which are generally associated with the major and minor release of the library. This means most binaries that link to even just libc might not work on a different release of the same distribution. With gcc, the default options will cause linking to at least libc; you have to go out of your way to have no run-time dependencies.
In general, you should assume that a compiled program will at best only work on the exact version of the distribution you compiled it on. This is why compiled software packages are usually specific to single versions of a distribution.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.