0

I'm trying to understand how Linux works and how to build modules.

So far I saw that Linux headers are stored in /usr/include and that the compiled implementation of these interfaces are located in /usr/lib/x86_64-linux-gnu. I have a few questions:

  1. How does Linux or any C program know where to look for the headers and the .so files? Is there a file where this is defined? Is it possible to modify these references in case I wanted to add another default sources or headers folder (just to horse around)?

  2. How are conflicts avoided? For example, in the libprocps-dev there is a header file /usr/include/proc/numa.hwith the definition void numa_uninit (void);. The implementation of this file is in /usr/lib/x86_64-linux-gnu/libprocps.so. What if someone also compiled another .so file with the same function definition but with another implementation (or code) and added it to /usr/lib/x86_64-linux-gnu? How would the Linker know which is the proper .so file that has to be linked to that header definition?

Thanks.

1
  • headers are just used to compile programs. Distribution try to have compatible headers. Sometime you can have more version of same libraries, but so, you should explicitly specify where which header you should use. .so have soname and symbol version so loader (ld.so) can find them. Look man page of ld.so on how to change the paths to look .so files Commented Nov 25, 2020 at 16:58

1 Answer 1

2

I'm trying to understand how Linux works and how to build modules.

Building kernel modules doesn’t involve the “standard” C compiler directories; instead, see /lib/modules/$(uname -r)/build.

  1. C programs don’t know where to look for headers and libraries; the C preprocessor and compiler do. You can see the standard include directories using

    gcc -xc -E -v - < /dev/null
    

    (replacing -xc with -xc++ for C++), and the library search directories using

    gcc -print-search-dirs
    
  2. Conflicts aren’t avoided by the compiler; it’s up to whatever or whoever is running the compiler to make sure that the search paths don’t contain conflicting headers. Libraries aren’t linked in automatically, you have to add libraries to the linker command-line (-lprocps in your example); any other library is ignored. In both cases, when conflicts arise, compilation and/or linking stops with an error.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .