Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-01-2012, 02:00 AM   #1
LQ Newbie
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
gcc -linking

I can create a linux executable with libs linked in with gcc-static,is
told to me. That simple is it. Where can I find tutorials on how to do
this - getting away with one compiler switch looks like fun.
Old 03-01-2012, 02:33 AM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
To compile with static libraries you need two things:
1. You need to have the static library itself installed. In linux, dynamic libraries usually end with a ".so" extension, and static libraries usually end in a ".a" extension. For example:
Also, if you look, the ".so" libraries are usually symbolic links to a more specific version library version. For instance, my /usr/lib/ is a symbolic link to /usr/lib/

2. You need to add "-static" to your link command. Assume you have a file named "main.c" that uses the math library (libm). You could compile and statically link the file in one step:
gcc -static -o myprogram main.c -lm
At that point, "myprogram" will by statically linked against your libm. It will not need to dynamically load anything from the libm library. You can confirm this by running:
ldd myprogram
If everything was linked statically, you should see a message similar to:
	not a dynamic executable

If you want something to test:
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

int main( int argc, char *argv[] )
  float base;
  float exponent;
  double baseToExponent;

  if( argc < 3 )
    return 1;

  base = strtof( argv[1], NULL );
  exponent = strtof( argv[2], NULL );

  baseToExponent = pow( base, exponent );
  printf( "%.2f^%.2f = %.2f\n", base, exponent, baseToExponent );

  return 0;
Save the above as main.c, and issue the command I gave earlier:
gcc -static -o myprogram main.c -lm
NOTE: when dealing with static linking, gcc is a little picky on the ordering. The libraries used (e.g. '-lm') need to come after the list of your source code files. Otherwise, you may get an "undefined reference" error.

Last edited by Dark_Helmet; 03-01-2012 at 02:55 AM.
1 members found this post helpful.
Old 03-01-2012, 03:34 AM   #3
LQ 5k Club
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2017 CentOS6.9 CentOS7.4 + 50+ other Linux OS, for test only.
Posts: 16,743

Rep: Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319

Mandriva : The static libc.a, libm.a etc. are provided
by the package glibc-static-devel.
1 members found this post helpful.
Old 03-02-2012, 02:30 AM   #4
LQ Newbie
Registered: Oct 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Dark_Helmet-thanks a lot for your trouble and quick reply.I am familiar with linux as
operator but not so much as programmer. Brian


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 in gcc lavixu Linux - Newbie 1 11-27-2009 08:07 AM
linking problem with gcc tokernizer Programming 1 11-19-2005 01:56 PM
Problem with linking libraries using gcc shortyzms Linux - Software 1 03-29-2004 09:13 PM
gcc linking error. ugenn Linux - Software 0 01-23-2003 12:18 AM
Linking without gcc, but with ld gluon Programming 3 05-29-2002 06:11 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:17 AM.

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