LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-07-2015, 01:08 AM   #1
Claudio_Baldo
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Rep: Reputation: Disabled
Problem Understanding the Library - Linux OS


Good day everybody,
I am new to linux OS and I have looked around for a topic similar to what I need, I got lost on the thousand of pages so I have decided to make a new thread.
Please indicate me the relevant post if someone has already asked what I am asking right now.

I am reading the `Linux Programming Interface` Manual (Micheal Kerrisk) which I find very interesting but I am afraid I need a kick start on this....

I want to call the stdio.c function `open()` but I am not sure if I should invoke it from the bash or from an executable c file.
Thank you in advance to anybody how can help me out.

Claudio

Last edited by Claudio_Baldo; 09-07-2015 at 03:24 AM.
 
Old 09-07-2015, 03:19 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
The libraries are there or your system would not run.

The libraries themselves are in one of two places depending on your architecture: /lib (and /usr/lib) for 32 bit code; /lib64 (and /usr/lib64) for 64 bit code. The reason for two locations is that a 64 bit install can run 32 bit binaries, if the appropriate 32 bit libraries are installed. Most (if not all) 64 bit linux systems have SOME 32 bit libraries installed, and some 32 bit programs. Additional libraries may be located in other places, as defined by the projects that use them (such as /usr/share, or /usr/local/...).

If you are trying to do program development you need other tools/packages installed.

Depending on your distribution (I mostly use Fedora), the packages are grouped under "program development", and will include header files defining interfaces, extra libraries (some for debugging, others for various development targets - like X Window, Gnome, ...) and include the usual toosls: compilers, make, autoconf, version control...

You likely need to refer to the installation documentation for your distribution. Usually it will have various sections to help select what you need.

Last edited by jpollard; 09-07-2015 at 03:21 AM.
 
1 members found this post helpful.
Old 09-07-2015, 03:45 AM   #3
Claudio_Baldo
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi jPollard,

yes, I understand the libraries are there, what I am not quite understanding is how the environment works in linux.
For example, if I want to open a file using the open() function defined by the stdio.c library, how should I do it?
Load the stdio.c library from somewhere?
I am using Ubuntu 32bit, where should I find the stdio.c library and how should I install the package?
 
Old 09-07-2015, 04:28 AM   #4
wigry
Member
 
Registered: Jul 2004
Distribution: slackware
Posts: 222

Rep: Reputation: 52
You are reading the "Linux programming Interface" guide, meaning, you are dealing with programming. That in turn implies you to write source code and compile it into executable and run the resulting executable from the shell.

So the open() is a method in standard C library and you need to make a C program to use it. If you have never done any C programming before, then I suggest scrapping the Linux Programming Interface for now and pick up a C tutorial (not C++, but plain C)

For example this one:
http://www.cprogramming.com/tutorial/c-tutorial.html
 
Old 09-07-2015, 04:30 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Ah. no need to do anything.

A linked executable image has within it references to the libraries it uses. An executable gets loaded by a small executable called ld.so - it is invoked by the kernel to provide the runtime linking. It reads the executables header to locate the referenced libraries, and loads them and the executable making final address resolution for the symbols, then it calls the executables starting address (which is just a subroutine that sets everything else up for the application (environment variables, the command parameters) then that calls the main function of the application.

Normally, you don't need the stdio.c source - you can't execute that, and is only useful if you are adding/modifying the runtime for some reason. The library is already compiled (it is the /lib/libc.so library file) and is ready for use.

A C program uses prototypes that describes the interface to the library (the "#include <stdio.h>") which causes the compiler to read the include file (which is stored in /usr/include...). This tells the compiler the data types for parameters, and return type for the functions used, which in turn, allows the detection of basic errors.

The compiled object code (not yet an executable) contains a list of undefined symbols (usually the variables and functions are defined in other object files or libraries). The linker (ld) then combines specified object files to come up with a remaining list of undefined symbols. ld searches through a list of known libraries (locations are specified by /etc/ld.so.cache, configured by the /etc/ld.so.conf configuration file and other files it designates). Any symbols defined by one of those libraries gets the library reference included in the executable.

The "ldd" utility can be used to list the libraries used, and what file is being referenced by an executable. If there are no references (it can happen - via static linking, which requires the use of an object library instead of a shared library) then the reference will not exist - as the linker has already resolved any symbols needed by using other object files or object library. Object libraries would be identified by a ".a" extension - for "archive", as it is a file containing an archive of other object files.

Last edited by jpollard; 09-07-2015 at 04:41 AM.
 
Old 09-09-2015, 08:59 PM   #6
Claudio_Baldo
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks jpollard...
I understand how the #include works, it is used to load data types and functions so once I call a function from an executable C program (or I declare a specific data type), this function (or object) is found in the libraries.

I have been using the C programming in closed environments, now I don`t understand the boundaries between Kernel, shell and C programs.
I understand the kernel is the body responsible of allocating HW resources (CPU/RAM/VMEM etc...), I understand C program is used as high level language to allow the programmer to make the PC to perform some tasks, I know the shell for some OS is an integral part of the kernel, this is not the case of UNIX systems, for UNIX systems the shell is just a user program...

When I compile a C program from the shell I use the compiler gcc, so I `translate` the C language in a shell executable...
1) Why the shell cannot run program in C code?
Now I run the C code from the shell using the ./a.out, I can also pass some data with the argc and *argv[].
Let`s assume I want to run the function open(), in order to open a file, from the shell, how can I do it?
2) The glibc (or libc) should be already built at start up, so, how can I have access to the open() function from the stdio.c?

Last edited by Claudio_Baldo; 09-10-2015 at 01:34 AM.
 
Old 09-10-2015, 02:21 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
It seems to me you're over thinking it; you just want a basic user mode C program.
See 2nd answer here https://stackoverflow.com/questions/...ith-c-in-linux
Note the doc quote about fopen() vs open().
 
Old 09-10-2015, 06:57 AM   #8
Claudio_Baldo
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
hi chrism01,
the point is not the open() function written in C but more general.
I am trying to draw lines between the shell environment and the C programming language, trying to understand why I cannot run C code directly from the shell but I need to compile the C code and then to run the executable.
Things start being a bit more clearer now that I am starting with some basic stuff.
The problem is that is always hard to troubleshoot the errors if you don`t have a clear idea of the whole environment and system.

i.e. Are the systems calls all the calls made from the shell?
Can I write in C++? What about portability? Is the C++ compiler available for all the UNIX system?
 
Old 09-10-2015, 07:12 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by claudioba@aruba.it View Post
Thanks jpollard...
I understand how the #include works, it is used to load data types and functions so once I call a function from an executable C program (or I declare a specific data type), this function (or object) is found in the libraries.

I have been using the C programming in closed environments, now I don`t understand the boundaries between Kernel, shell and C programs.
I understand the kernel is the body responsible of allocating HW resources (CPU/RAM/VMEM etc...), I understand C program is used as high level language to allow the programmer to make the PC to perform some tasks, I know the shell for some OS is an integral part of the kernel, this is not the case of UNIX systems, for UNIX systems the shell is just a user program...
Not exactly.

The kernel does provide the allocation of resources - it is still just a program, but one that uses system calls/traps to enforce an interface from applications. These can be thought of as equivalent to function calls, but do not share their memory (code/data) with the application. Libraries (such as libc) provide a function call interface to the system calls so that a standard definition of the functions can exist - different hardware provide different ways for system calls to work - the libc functions provide code to rearrange the parameters into the structure required by the hardware. As such, libc provides a "wrapper" to present a standard interface for the programmer.

Other libraries provide functions unrelated to the kernel (such as database access, keyed files, string search). The libc library contains what is considered the "standard" libraries for C programming - no keyed files, no database, but can include string search (string.h - no system calls), buffered I/O support(stdio.h), memory allocation (stdlib.h, does include system calls to allocate memory). Math functions are in a separate library libm (math.h).

The decision to use separate libraries is generally to isolate more special purpose functions from general use functions. libc is used by NEARLY every program (I say nearly because it IS possible to not use it, though awkward) but math functions, like keyed files, are not.

A shell is just a program that takes input (from a file), produces output (to another file), and reports errors (to a third file), and carries out actions directed by that input. Here, I say "from a file" because for UNIX/Linux systems, everything is considered a file - even a terminal. The shell language is designed to provide a relatively simple interface for a user. As such, its primary function is to translate the users command into something that invokes a separate program (such as "cat"). The language implemented by the shell is a direct interpreter - it reads a line, identifies the parts of the line (the program to run, parameters), and initiates the program using various system calls, then waits for it to finish.

In addition to the simple translation, additional features are added to make the language used more useful - thus the addition of various loop control, variables (everything is a string), some variable substitution capability...

The "everything is a file" becomes very useful by being able to use the shell itself as a command to process files (a "shell script" if you will). This extends the generality of the shell to becoming a basic programming environment. The shell can now invoke programs... or treat text files as programs...

A compiler is just another program. It takes input from one (usually more) files, and produces a new file for output. By design the C compiler itself is actually composed of multiple files. A preprocessor (which takes the file to translate, and looks for macros, expands them, combines include files...) and passes the output to a translator (which itself can be multiple files performing different translation/optimization/code generation phases); eventually producing an object file. By default it passes the result to a linker to produce an executable.

Quote:
When I compile a C program from the shell I use the compiler gcc, so I `translate` the C language in a shell executable...
1) Why the shell cannot run program in C code?
Now I run the C code from the shell using the ./a.out, I can also pass some data with the argc and *argv[].
Let`s assume I want to run the function open(), in order to open a file, from the shell, how can I do it?
The shell interpreter doesn't use "C" for the command language (too awkward to use for commands). The "everything is a file" comes in because the shell controls three default files - standard in, standard out, and standard error. For most things all you need to do is "redirect" input to a pre-existing program from something else. The base linux shell is "bash" - a tutorial is available at http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
Quote:

2) The glibc (or libc) should be already built at start up, so, how can I have access to the open() function from the stdio.c?
As stated, stdio.c doesn't exist. stdio.h is an include file for the C compiler that defines how standard I/O functions of the C language are to be used.

A tutorial on Linux is available at:
http://www.ee.surrey.ac.uk/Teaching/Unix/

This will give you a more complete understanding of how things work.
 
Old 09-10-2015, 07:27 AM   #10
Claudio_Baldo
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
It makes things clearer.
I only have one point which is not 100% clear yet.
If the stdio.c does not exist, where are stored the original C function calls?
If I invoke the open() function in C, the code for this must be stored somewhere otherwise how can it be compiled?
 
Old 09-10-2015, 08:34 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
It happens to be in /lib/libc.so... or in /lib64/libc.so...

And until the executable actually tries to run... it doesn't have to exist at all.

Remember. A compiler only knows about symbols and syntax. It doesn't have anything to do with actual executables. The linker is what combines a file with undefined symbols with definitions from other object files, and references to other code.

Last edited by jpollard; 09-10-2015 at 08:38 AM.
 
Old 09-10-2015, 12:09 PM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by claudioba@aruba.it View Post
If I invoke the open() function in C, the code for this must be stored somewhere otherwise how can it be compiled?
It's already been compiled, by another person on another computer. The pre-compiled binary blob (called a shared object library) that contains open() is at /lib/libc.so (or /lib64) as jpollard mentioned, this gets pulled in whenever any program that needs it is run. This is called dynamic linking. Your executable does not contain every function that is required to run, it only contains the "unique" code. Standard functions that your program calls are pulled in dynamically from the system libraries in /lib or /lib64 when needed.

If you run "ldd <executable>", replacing <executable> with the name of your executable, it will tell you which system libraries are required by your executable, which will be pulled in when you run it.

You can build your own shared object libraries as well. Say you write a function that's terrific, you want to use it all the time in all sorts of projects. You have two options:

1) Copy the source code over to every project you want to use it in and built it into each project's executable. If you find a bug or want to improve some aspect of your function, you need to update this source code file in every project and rebuild every project in order to update them.

2) Build your source code into a shared object library, and let your other projects dynamically link to it. If you find a bug or want to improve some aspect of your function, just rebuild the shared object library, and every project that uses it will instantly be using the newest version.

Last edited by suicidaleggroll; 09-10-2015 at 12:19 PM.
 
Old 09-10-2015, 03:07 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by suicidaleggroll View Post
It's already been compiled, by another person on another computer. The pre-compiled binary blob (called a shared object library) that contains open() is at /lib/libc.so (or /lib64) as jpollard mentioned, this gets pulled in whenever any program that needs it is run. This is called dynamic linking. Your executable does not contain every function that is required to run, it only contains the "unique" code. Standard functions that your program calls are pulled in dynamically from the system libraries in /lib or /lib64 when needed.

If you run "ldd <executable>", replacing <executable> with the name of your executable, it will tell you which system libraries are required by your executable, which will be pulled in when you run it.

You can build your own shared object libraries as well. Say you write a function that's terrific, you want to use it all the time in all sorts of projects. You have two options:

1) Copy the source code over to every project you want to use it in and built it into each project's executable. If you find a bug or want to improve some aspect of your function, you need to update this source code file in every project and rebuild every project in order to update them.

2) Build your source code into a shared object library, and let your other projects dynamically link to it. If you find a bug or want to improve some aspect of your function, just rebuild the shared object library, and every project that uses it will instantly be using the newest version.
3) create an object library that copies only the needed binary for use...
 
Old 09-11-2015, 12:12 AM   #14
Claudio_Baldo
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you to the both of you.
 
  


Reply


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
[SOLVED] Linux bootup chicken-egg problem understanding jeffmonte Linux - Newbie 5 09-26-2013 09:15 AM
[SOLVED] ./configure problem for libsf library due to apparently missing libdb library. vectrum Linux - Software 6 08-05-2011 03:11 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-05-2007 12:11 AM


All times are GMT -5. The time now is 06:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration