How does Linux C code find the conf file (usually in / etc)

I know, when I install a Linux application from the source code, I execute ./configure --sysconfdir=/etc, then this conf app file (for example, httpd.conf) will be goto /etc.

But from the point of view of the source code , how does the source code know that the conf file is under /etcwhen analyzing it. I mean that code like this fopen("/../../app.conf", "r");is determined before it is installed, will the source code for the configuration file change, or does any other mechanism exist?

+3
source share
2 answers

configureThe script will generate the necessary Makefileone that will use the C compiler functionality -DMACRO=contentto essentially inject the C preprocessor statements #define MACRO contentinto compilation units. Thus, sysconfdirit can be used with Make rules:

foo.o: foo.c
        $(CC) -DCONFDIR=$(sysconfdir) -o $@ $<

(This suggests that to create an object file foo.owhen updating foo.cto create it, use the variable $(CC)to start the C compiler, define CONFDIRwith the contents $(sysconfdir)(complete via a ./configurescript) put the output in the target file ( $@) and specify the source file ( $<) as a single input to the compiler .))

Then C's code foo.ccan use it as follows:

 FILE *conf;
 if (conf = fopen(CONFDIR "/foo", "r")) {
     /* read config file */
 } else {
     /* unable to open config, either error and die or supply defaults */
 }

Note that concatenation of <is performed before compiling the program .

: http://www.gnu.org/software/hello/manual/autoconf/Installation-Directory-Variables.html#Installation-Directory-Variables

+5

./configure, makefile, C. -D..., ( ) "#define" CPP. "/etc", ./configure --sysconfdir=/etc.

"/etc" -, #defined.

+1

Source: https://habr.com/ru/post/1793351/


All Articles