I know when I install a Linux app from source ,I execute ./configure --sysconfdir=/etc, then this app’s conf file(such as httpd.conf) will goto /etc.
But from the view of source code, how do source code know the conf file is under /etc when parse it. I mean the code like fopen("/../../app.conf", "r"); is determined before we install it, will the configure file change source code or some other mechanism exist ?
The
configurescript will generate the necessaryMakefilethat will use the C compiler’s-DMACRO=contentfunctionality to essentially inject C preprocessor#define MACRO contentstatements into the compilation units. So,sysconfdircould be used via Make rules:(That says to build the
foo.oobject file whenfoo.cis updated; to build it, use the$(CC)variable to run the C compiler, defineCONFDIRwith the contents of$(sysconfdir)(supplied via the./configurescript), put output into the target file ($@) and give the source file ($<) as the lone input to the compiler.))Then the C code in
foo.ccould use it like this:Note the C string concatenation is performed before compiling the program — super convenient for exactly this kind of use.
More details here: http://www.gnu.org/software/hello/manual/autoconf/Installation-Directory-Variables.html#Installation-Directory-Variables