LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Information on Libraries and Compilers (https://www.linuxquestions.org/questions/linux-newbie-8/information-on-libraries-and-compilers-4175467356/)

IneptCoder 06-25-2013 01:36 PM

Information on Libraries and Compilers
 
I am new to Linux (and working with computers in general) and I wanted to know more about the programming and libraries. Specifically, I wanted to know where the libraries are stored on the computer. Also, does the compiler come with libraries or does your compiler soley rely on the libraries stored on the computer (I am using GCC)? Also, I don't really understand the compiler process. From what I gather the compiler convertes the code you write into machine language (is that binary?) and it also "links" libraries (pre-compiled code?) and your different files together. I am not really sure how that linking works. So, it would be great if anyone has any good info they want to share or point me in the right direction.

jpollard 06-25-2013 02:22 PM

It all depends.

Libraries can be anywhere.

DEFAULT libraries are in one of (3?) places - /usr/lib64 (for 64 bit libraries), /usr/lib (32 bit libraries), /usr/local/lib ...

The /usr/local/lib is frequently used to to hold site (your site) specific application/libraries, hence the /usr/local directory tree usually includes a bin, etc, lib...

Some development libraries are not always installed. The normally installed libraries are shared libraries - meaning that only one memory resident version exists, but are mapped into many processes memory range. Development libraries are usually named xxx.a, meaning "archive". Sometimes these libraries include debugging modules, as well as including debugging symbols. Both can be useful during development.

It is up to the distribution to decide how to package such things. frequently the compiler will only include the base runtime libraries (the .a) because there are so MANY different libraries (X has a couple of dozen, then there are more for C++, Fortran, GUI toolkits...)

The compiler proper only translates from the source code to a ".o" file (meaning "object"). A separate linker will combine these together (usually, the link phase is automatically done for simple programs). Libraries are created using a different utility (ar - for "archiver"). Shared libraries are created by the linker (ld - for "link edit") by giving it specific options.

Nearly all compilers have multiple passes/phases:

1. preprocessor - scan the file creating/expanding macros, adding definitions (prototypes) for library functions. The output is then passed to
2. the scanner/tokenizer/compiler (may be all one unit). The output of this is usually passed to
3. an optimizer which tries to optimize speed/memory usage which sometimes outputs to a
4. code generator that generates the final object file.

If it is a relatively simple (or at the end of a long series of compiles) the compiler can then invoke a linker that combines all the listed object files, libraries referenced (or implied - gcc assumes you are going to use libc, so normally you don't have to give that one)

IneptCoder 06-25-2013 02:43 PM

Thank you for the information.
So in C++ do you "#include <somelibrary>",because "somelibrary" is a library that is not already included by the compiler?

frieza 06-25-2013 03:14 PM

yes and no
you don't 'include' libraries, you include HEADERS, which are re-usable parts of other parts of programs, such as libraries, that allow developers to interact with said programs/libraries at a programming level, or to perform tasks without 'reinventing the wheel', such as stdio.h
http://en.wikipedia.org/wiki/Header_file

and in answer to your second query, nothing is 'included' by the compiler by default, any header you need must be explicitly included.

jpollard 06-25-2013 04:04 PM

A #include causes the referenced file to be read as input to the compiler.

"include files" have definitions (macros, structures, enumeration,...), some for constants (such as flag values), some for function prototypes so that the compiler can verify parameter types.

A compiler command such as "gcc" can invoke the linker with standard libraries (gcc uses "libc"). All other libraries have to be referenced so that the linker can use the appropriate ones.

To this end, there are a lot of tools available (autoconf, make, ar, various editors - especially emacs which has a lot of IDE like attributes).


All times are GMT -5. The time now is 08:30 AM.