LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2011, 11:03 AM   #1
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Rep: Reputation: 0
Statically linking an extremely simple c++ program using g++.


This is the file, test.cpp:
Code:
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello!" << endl;
    return 0;
}
This at the command-line:
Code:
[c_moriarty@home Haskell]$ g++ -static -Wall test.cpp
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
So, I've been searching on Google for an hour. How do I make it work?
 
Old 04-30-2011, 11:17 AM   #2
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Code:
[c_moriarty@home Haskell]$ g++ -static -Wall -o test test.cpp -lm -lstdc++ -lc
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status

Last edited by c_moriarty; 04-30-2011 at 11:21 AM.
 
Old 04-30-2011, 11:28 AM   #3
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Code:
[c_moriarty@home Haskell]$ g++ -static -Wall -o testcpp test.cpp -L/usr/lib -lstdc++ -lm -lc
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
Code:
[c_moriarty@home Haskell]$ g++ -static -lstdc++ -lm -lc -Wall -o testcpp test.cpp
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
Code:
[c_moriarty@home Haskell]$ g++ -static -L/usr/lib -lstdc++ -lm -lc -Wall -o testcpp test.cpp
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
Code:
[c_moriarty@home Haskell]$ g++ -static-libstdc++ -L/usr/lib -Wall -o testcpp test.cpp
/usr/bin/ld: cannot find -lstdc++
collect2: ld returned 1 exit status

Last edited by c_moriarty; 04-30-2011 at 11:30 AM.
 
Old 04-30-2011, 11:34 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Perhaps you're missing some packages. Which version of Fedora are you using and what does the following command say?

rpm -qa | grep -E "glibc|stdc++"
 
1 members found this post helpful.
Old 04-30-2011, 11:36 AM   #5
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Fedora 14.
Code:
[c_moriarty@home Haskell]$ rpm -qa | grep -E "glibc|stdc++"
glibc-devel-2.13-1.i686
libstdc++-devel-4.5.1-4.fc14.i686
glibc-common-2.13-1.i686
glibc-headers-2.13-1.i686
glibc-2.13-1.i686
libstdc++-4.5.1-4.fc14.i686
 
Old 04-30-2011, 11:40 AM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
As a quick test, does it work if you remove the -static flag?
 
Old 04-30-2011, 11:42 AM   #7
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Yes, it works fine...
Code:
[c_moriarty@home Haskell]$ g++ -Wall -o testcpp test.cpp
[c_moriarty@home Haskell]$ ./testcpp
Hello!
 
Old 04-30-2011, 11:45 AM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
This suggests (to me, at least) that the static versions of the relevant libraries aren't present on your system. Can you just run

ls /usr/lib | grep libstdc++.a

to check?
 
1 members found this post helpful.
Old 04-30-2011, 11:46 AM   #9
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Code:
[c_moriarty@home Haskell]$ ls /usr/lib | grep libstdc++.a
[c_moriarty@home Haskell]$
That seems strange. I have all the packages installed that I would think you'd have to have installed...
 
Old 04-30-2011, 11:48 AM   #10
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
They're separate, for some reason... I found it:
libstdc++-static.i686 : Static libraries for the GNU standard C++ library
glibc-static.i686 : C library static libraries for -static linking.

Last edited by c_moriarty; 04-30-2011 at 11:50 AM.
 
Old 04-30-2011, 11:50 AM   #11
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks...
 
Old 04-30-2011, 11:50 AM   #12
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Presumably you're missing the other static packages too (like for glibc, the GNU C library). I'm not sure which packages you'll need to install, though, as I don't know how Fedora splits everything. Hopefully another Fedora user will see this thread if you can't manage to work it out yourself .
 
Old 04-30-2011, 11:51 AM   #13
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Code:
[c_moriarty@home Haskell]$ g++ -static -Wall -o testcpp test.cpp
[c_moriarty@home Haskell]$ ./testcpp
Hello!
 
Old 04-30-2011, 11:52 AM   #14
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Cool, glad you got it sorted. Please mark the thread as solved (using the "Thread Tools" at the top).
 
Old 04-30-2011, 11:52 AM   #15
c_moriarty
LQ Newbie
 
Registered: Apr 2011
Distribution: Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
I had to install both of those,
libstdc++-static and glibc-static
 
  


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
Statically linking to libraries lazylogik Linux - Newbie 1 07-06-2009 11:43 AM
[SOLVED] How to build a shared object from other libraries (linking statically)? eantoranz Programming 4 10-26-2007 12:19 PM
implication of statically linking to libstdc++ zero79 Linux - General 3 04-08-2006 01:03 PM
Problems statically linking with libsdl jakobf Programming 3 08-27-2004 12:20 AM
a simple dynamic linking program kris273 Programming 3 04-18-2004 06:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:49 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