LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-04-2010, 04:45 AM   #1
TomBackton
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
Question Creating C++ Shared Libraries on Linux


I have 2 questions... I couldn't find answers by Googling (more precisely, by Blackling) and in GCC documents, so I'm asking here.

1. There are 2 flags for position independent code, -fpic and -fPIC. All the examples I read use -fPIC... so when is -fpic useful and what is the difference?

2. Some tutorials use "g++ -shared" while others use "ld" for the creation of the shared library. Why? Does it matter which method I use? Is there a difference? Why does nobody mention both options?
 
Old 04-04-2010, 04:55 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by TomBackton View Post
I have 2 questions... I couldn't find answers by Googling (more precisely, by Blackling) and in GCC documents, so I'm asking here.

1. There are 2 flags for position independent code, -fpic and -fPIC. All the examples I read use -fPIC... so when is -fpic useful and what is the difference?

2. Some tutorials use "g++ -shared" while others use "ld" for the creation of the shared library. Why? Does it matter which method I use? Is there a difference? Why does nobody mention both options?
Do you in general understand what gcc/g++ is ?

Do you want me to prove here you actually haven't read 'gcc' documents ?
 
Old 04-04-2010, 05:46 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You may want to try this page...
 
Old 04-04-2010, 07:51 AM   #4
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 TomBackton View Post
I have 2 questions.
I think people in this forum are usually more helpful than was demonstrated by the first two replies you got.

Quote:
I couldn't find ... in GCC documents
Quote:
2 flags for position independent code, -fpic and -fPIC.
I often have trouble finding info in GCC documents even when that info is actually there. But the -fpic vs. -fPIC discussion is right where you would expect it to be as graemef linked.

It tells you the difference for SPARC, m68k and RS/6000 and it tells you there is no difference for X86. IIUC, it doesn't mention X86_64 (I don't know whether you care about X86_64. I do.) So I don't know whether -fpic vs. -fPIC makes a difference for X86_64.

Quote:
2. Some tutorials use "g++ -shared" while others use "ld" for the creation of the shared library. Why? Does it matter which method I use? Is there a difference? Why does nobody mention both options?
I don't know either. But I think a few experiments with gcc -v -shared would tell you (-v is documented in 3.2).

Quote:
Originally Posted by Sergei Steshenko View Post
Do you want me to prove here you actually haven't read 'gcc' documents ?
I (probably not the OP) have read every page of the gcc documents for some 4.x.x version at some point. But that doesn't mean I can remember every detail. It doesn't even mean I can find them again when I need them. Much of GCC documentation is very poorly organized.

For a recent thread at LQ, I search GCC docs again to find the documentation of -shared. (My own use of -shared has been buried in unchanging build control files for so long I forget the reasons behind the choices). I found -shared documented, not in the section where I would have expected it and not with enough explanation to understand it.

So your proof that the OP hasn't read the documents probably wouldn't be very convincing and more probably wouldn't be very helpful.

Last edited by johnsfine; 04-04-2010 at 07:58 AM.
 
Old 04-04-2010, 12:41 PM   #5
TomBackton
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I still don't know what's the difference between ld and gcc -shared, but anyway, I have a new question: On Windows, variables can be exported and imported from DLLs using non-standard syntax: __declspec(dllexport). But I didn't see anything like that for Linux. Are all the global variables in the cpp files exported to the library? And when I write a program that uses the library, do I declare the imported variables with extern
(extern int globalVariable; ) ?
 
Old 04-04-2010, 12:47 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by TomBackton View Post
I still don't know what's the difference between ld and gcc -shared
...
No, first and foremost you don't know how 'gcc' works at the very top level and what 'gcc' as an executable really is.
 
Old 04-04-2010, 12:49 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by TomBackton View Post
...
Are all the global variables in the cpp files exported to the library? And when I write a program that uses the library, do I declare the imported variables with extern
(extern int globalVariable; ) ?
Consider gcc/g++ as compilers supporting standard C99/C++ (with the appropriate command line switches).
 
Old 04-04-2010, 12:50 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
GCC is actually a binary that calls ether ld or cc1.
It just makes it easier.
 
Old 04-06-2010, 12:03 AM   #9
TomBackton
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
"No, first and foremost you don't know how 'gcc' works at the very top level and what 'gcc' as an executable really is."

Dude, this forum is for answering questions. With all the respect, and I have no doubt you have lots of knowledge and experience, those replies of your do nothing more than waste the servers' memory. Also, I only USE gcc. I don't DEVELOP it, so I'm not ecev supposed know how it works. It's just a bonus. Having documentation of all the command-line options and all the other things listed in the manual is enough.
I just asked a simple question. I WILL learn more about GCC gruadually, but right now what I need is the answer to my question. If you're not planning to help, I don't understand why you posted here in the first place...

Also, I think I already know the answer (gcc -shared makes sure constructors of exported objects are executed, while ld doesn't), so if you don't know the full answer it's ok if you don't bother, King Sergei.
 
Old 04-06-2010, 04:00 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by TomBackton View Post
"No, first and foremost you don't know how 'gcc' works at the very top level and what 'gcc' as an executable really is."

Dude, this forum is for answering questions. With all the respect, and I have no doubt you have lots of knowledge and experience, those replies of your do nothing more than waste the servers' memory. Also, I only USE gcc. I don't DEVELOP it, so I'm not ecev supposed know how it works. It's just a bonus. Having documentation of all the command-line options and all the other things listed in the manual is enough.
I just asked a simple question. I WILL learn more about GCC gruadually, but right now what I need is the answer to my question. If you're not planning to help, I don't understand why you posted here in the first place...

Also, I think I already know the answer (gcc -shared makes sure constructors of exported objects are executed, while ld doesn't), so if you don't know the full answer it's ok if you don't bother, King Sergei.

Dude, this forum is also about not answering questions asked by those, who don't deserve it.

This is your very first statement:

http://www.linuxquestions.org/questi...6/#post3923626 :

Quote:
I have 2 questions... I couldn't find answers by Googling (more precisely, by Blackling) and in GCC documents, so I'm asking here.
The items in bold indicate one or more of the following:
  1. you didn't even read the 'gcc' documentation, but you lied stating you did;
  2. you were incapable of using "Find" function in the documentation browser you used;
  3. you were lazy to use "Find" function sufficient number of times while reading the documentation;
  4. you can't comprehend what you are reading.
.

Neither of the above possibilities encourages answering your questions.
 
Old 04-06-2010, 07:44 AM   #11
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 TomBackton View Post
I think I already know the answer (gcc -shared makes sure constructors of exported objects are executed, while ld doesn't)
Could you tell me where you found that in the documentation and/or what experiment gave you that result.

I would not have expected that difference. I probably wouldn't know, because I rarely export any C++ interface from a .so. I generally use only extern "C" interfaces on the boundaries of my .so files, even though the main executable and the .so are both internally C++. But I have browsed enough documentation, I would have expected to remember something about that difference if it is correct.

Quote:
Originally Posted by TomBackton View Post
Are all the global variables in the cpp files exported to the library?
I believe that is a more complicated topic than you would expect. The keyword "visibility" shows up in most discussions of that topic. You might try some google searches including that with other appropriate keywords.

The Unix compatible defaults make much more visible on .so boundaries. That also happens by default in both directions, so a function statically linked into a .so can be interposed at load time so that .so instead uses the same named function linked into the main executable or into some .so that was loaded earlier.

Quote:
And when I write a program that uses the library, do I declare the imported variables with extern
(extern int globalVariable; ) ?
Normally a symbol you intend to resolve from a .so would appear in the calling source code exactly the same as one resolved from a static library.

At link time, the linker must be told to read the .so files along with any static libraries. Anything the linker decides should be resolved at load time from a .so will be left unresolved at link time. (So in reading the .so files, the linker does not resolve anything from .so files, it just decides what can be left unresolved).

Last edited by johnsfine; 04-06-2010 at 08:26 AM.
 
Old 04-06-2010, 08:19 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Folks;
Please tone down the personal comments and criticisms. thank you.
 
  


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
error while loading shared libraries: libhid.so.0: cannot open shared object file: No misungs Linux - Software 4 06-10-2009 12:05 PM
Urgent !!! rpm: error while loading shared libraries: libelf.so.1: cannot open shared tinaa Linux - Software 5 12-02-2008 03:19 PM
error while loading shared libraries: libstdc++.so.5: cannot open shared object file PaulyWally Debian 2 10-18-2008 05:59 PM
error while loading shared libraries: libdb-4.1.so: cannot open shared object file putquery8581 Linux - Software 1 10-01-2004 07:03 AM
linux init error in loading shared shared libraries akaran Linux - Software 1 05-28-2003 04:40 AM

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

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