Start Blogaria Bored bsgen cconf Cookies CopyForward CyclicLog Dialwhatever DNSBalancer fch HammerServer jpeginfo kalk Lectures Microproxy msc Nasapics PGPkey SafeEdit Simple listserv Wallpapers
A little movie
An animation can't be properly rendered. You have probably a too old version of Flash player. |
c-confIn short...
Use CasesYou'll find a number of typical use cases for c-conf below. All these are written from the perspective of a Makefile: c-conf gets called in the Makefile to determine a couple of relevant settings. The settings are then later used to construct a program. Note that not all use cases are described below - just the most often used ones.Caching resultsC-conf will optionally cache results in a file. This greatly speeds up the compilation of programs. The cache is always selected using flag -c of c-conf (all below examples do this). Note that a Makefile should also remove the cache during a "make clean":
# Makefile: Get construction options
MY_OPT = $(shell c-conf -c conf.cache ... whatever ...)
clean:
# Remember to kill the c-conf cache
rm -f conf.cache
Getting the name of the C++ compilerThis finds out the name of the C++ compiler. When more than one version exists on the system, the highest one is selected. Similarly, c-conf can find the C-compiler.
# Makefile: Get the compiler name.
CPP_COMP = $(shell c-conf -c conf.cache c++-compiler)
# Now use it:
foo.o: foo.cc
$(CPP_COMP) -c -o foo.o -c foo.cc
# Remember to remove the cache file during cleanup:
clean:
rm -f foo.o conf.cache
Optimization FlagsThis tries to guess the optimization flags given the system and/or compiler.
# Makefile: get the optimization flags
OPTFLAGS = $(shell c-conf -c conf.cache optflags)
CPP_COMP = $(shell c-conf -c conf.cache c++-compiler)
# Now use it:
foo.o: foo.cc
$(CPP_COMP) $(OPTFLAGS) -o foo.o -c foo.cc
What libs do we have?On some systems you might need to add -lpthread to the linkage command to get threads. On others you don't. How do you know whether you need the library flag? And where the library resides?
# Makefile: Find out whether we need socket and pthread libs
LIBS = $(shell c-conf -c conf.cache lib socket pthread)
CPP_COMP = $(shell c-conf -c conf.cache c++-compiler)
# Now use it:
myprog: myprog.o
$(CPP_COMP) -o myprog myprog.o $(LIBS)
In the above example, $(LIBS) might be expanded to "-L/usr/local/lib
-lpthread" or similar.
Do we have a header?Some systems have e.g. "string.h", others don't. Here's how to find out:
# Makefile: find out whether we have string.h
STRINGH = $(shell c-conf -c conf.cache ifheader string.h GOT_STRING_H
CPP_COMP = $(shell c-conf -c conf.cache c++-compiler)
# Now use it:
foo.o: foo.cc
$(CPP_COMP) -c $(STRINGH) foo.cc
The macro $(STRINGH) will either get expanded to "-DGOT_STRING_H" or
to nothing. In the C++ source (or a header) it is used as follows:
// foo.cc: let's see if we need to bring string.h aboard #ifdef GOT_STRING_H #include <string.h> #endif Do we have a C/C++ function?Sometimes you need to find whether a system has a function (else you might need to define it yourself). Here's how. For example, the following determines whether strnstr() is available or not:
# Makefile: do we have strnstr() available?
STRNSTR = $(shell c-conf -c conf.cache libfunction01 strnstr HAVE_STRNSTR)
CPP_COMP = $(shell c-conf -c conf.cache c++-compiler)
# Let's use it
foo.o: foo.cc
$(CPP_COMP) -c $(STRNSTR) foo.cc
The macro $(STRNSTR) will be expanded to either "-DHAVE_STRNSTR=1" or
to "-DHAVE_STRNSTR=0". This can be used in the code:
// foo.cc: If we don't have strnstr(), define it so we can use it later
#if HAVE_STRNSTR == 0
char *strnstr(char const *stack, char const *needle, size_t len) {
// code goes here
}
#endif
Shared library constructionShared libraries have different names on different systems, and there are different compilation flags and linkage flags involved. Here's an example:
# In the Makefile:
CPP_COMP = $(shell c-conf -c conf.cache c++-compiler)
# What is the filename of a shared library "test" (might be libtest.so etc).
SHLIB = $(shell -c conf.cache so-name test)
# What are the compilation flags to make an object that goes into a
# shared library, might be -fPIC and so on
COMPFLAGS = $(shell -c conf.cache so-cflags)
# What are the linkage flags when making a shared object out of .o
# files (might be -shared, or -dynamiclib and so on)
LINKFLAGS = $(shell -c conf.cache so-lflags)
# Construction of the shared lib out of foo.cc and bar.cc:
$(SHLIB): foo.o bar.o
$(CPP_COMP) -o $(SHLIB) $(LINKFLAGS) foo.o bar.o
%.o: %.cc
$(CPP_COMP) -c $(COMPFLAGS) $<
SynopsisBelow is the "usage info" that c-conf gives when invoked without arguments:
This is c-conf, the C compilation configuration helper V1.15
Copyright (c) e-tunity. Contact <info@e-tunity.com> for information.
Usage:
c-conf [flags] header FILE.H [FILE.H...] Searches for directories
containing the named header(s), returns appropriate -I flags.
c-conf [flags] headerdir DIR [DIR...]: Searches for directory
containing headers, returns appropriate -I flags.
c-conf [flags] ifheader FILE.H DEFINE Searches for the named header.
If found, a compilation flag -DDEFINE is returned, indicating that
the header is found.
c-conf [flags] ifheader01 FILE.H DEFINE Similar to ifheader, but the
returned define is either DEFINE=0 or DEFINE=1
c-conf [flags] lib NAME [NAME...]: Searches for libNAME.{a,so,...},
returns appropriate -L and -l flags.
c-conf [flags] libfunction FUNC DEFINE Creates a small program that
tries to use FUNC. If this succeeds, a -DDEFINE=1 flag is returned.
c-conf [flags] libfunction01 FUNC DEFINE Similar to libfunction, but
the returned define is DEFINE=0 or DEFINE=1
c-conf [flags] libvariable01 VAR DEFINE Creates a small program that
accesses variable VAR. If this succeeds, -DDEFINE=1 is returned.
c-conf [flags] so-name NAME: Returns filename of a shared-object for NAME,
e.g. libNAME.so
c-conf [flags] so-cflags: Returns compilation flags to build shared
objects
c-conf [flags] so-lflags: Returns linkage flags to produce a shared-object
library
c-conf [flags] c-compiler: Returns name of C compiler
c-conf [flags] c++-compiler: Returns name of C++ compiler
c-conf [flags] optflags: Returns fast-code optimization flags.
Optional flags:
-c CACHE: settings will be dynamically determined unless present in
CACHE; new settings will be added to CACHE
-h: to show short help for an action, e.g. try 'c-conf -h so-name'
-s: to suppress showing of warnings
-v: to show verbose messages
-I DIR[,DIR..]: to add DIR(s) to the searchpath for headers, default
searchpath is /usr/include /usr/local/include /opt/local/include /sw/include /root/include
-L DIR[,DIR..]: to add DIR(s) to the searchpath for libraries, default
searchpath is /usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64 /opt/local/lib /opt/local/lib64 /usr/ucblib /sw/lib /sw/lib64 /root/lib
-l LIB[,LIB..]: to add LIB(s) to C compiler invocations, when checking
for lib functions or variables
Meaningful output is returned on stdout. Verbose messages, warnings and
errors go to stderr.
|