ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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 ?
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
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.
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; ) ?
...
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).
"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.
"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.
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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.