LinuxQuestions.org
Visit Jeremy's Blog.
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, 12:11 PM   #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, 12:19 PM   #2
JimBrewster
Member
 
Registered: Feb 2010
Location: usa:/dev/random
Distribution: Slackware, Salix
Posts: 237

Rep: Reputation: 59
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, 12:29 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:27 PM
static and dynamic linking zb10948 Programming 1 04-27-2009 04:48 AM
gcc errors on static, but NOT dynamic, linking MALDATA Programming 5 10-03-2008 12:20 AM
Linking a static library to a dynamic one delta4s Programming 2 09-28-2006 02:24 AM
Static/dynamic linking ugenn Linux - Software 0 12-16-2002 12:01 AM


All times are GMT -5. The time now is 02:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration