LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2011, 11:11 AM   #1
qwerty4061
Member
 
Registered: Nov 2011
Posts: 31

Rep: Reputation: Disabled
Static and dynamic linking


Hi,
I was trying to run a program, compiled on Linux on a windows machine(just for fun). I understand that a dynamically linked executable cannot be run on windows because of the linux c library, and windows gives some error saying something about illegal instructions. I then tried running the statically linked application, now windows is not giving me any errors, but it still is not giving the correct output, a command prompt windows appears and disappears after an instant. But the way I see it, I should be able to run a statically linked application on any x86 computer irrespective of the OS.

I compiled code using:
gcc -static loop.c

file ./a.out gives the result
./a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.15, not stripped


Source Code of the sample program:

Code:
#include <stdio.h>
int main()
{
  int i = 0;
  while(1)
  {
    printf("%d \n", i++);
  }
  return 0;
}
Thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-27-2011, 11:19 AM   #2
JimBrewster
Member
 
Registered: Feb 2010
Location: usa:/dev/random
Distribution: Slackware-15.0; -current
Posts: 245

Rep: Reputation: 60
Userland applications and statically linked libraries still must interface with the kernel, and Linux vs Windows kernels have very different system calls which is why we need tools like Wine to run Windows apps on Linux.
 
1 members found this post helpful.
Old 12-27-2011, 11:29 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by qwerty4061 View Post
But the way I see it, I should be able to run a statically linked application on any x86 computer irrespective of the OS.
Not true. JimBrewster gave you one sufficient reason. Another is that the file format is not likely to be compatible.

Quote:
file ./a.out gives the result
./a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.15, not stripped
Try taking a .exe file that works on your Windows system and copying it to the Linux system and using a similar file command to find out the format.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Static linking vs. dynamic linking? posix_memalign Programming 13 04-18-2010 02:27 PM
static and dynamic linking zb10948 Programming 1 04-27-2009 03:48 AM
gcc errors on static, but NOT dynamic, linking MALDATA Programming 5 10-02-2008 11:20 PM
Linking a static library to a dynamic one delta4s Programming 2 09-28-2006 01:24 AM
Static/dynamic linking ugenn Linux - Software 0 12-15-2002 11:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration