int v;
int f() { ... }
extern int v;
int f();
// $Id: hello.C,v 1.2 1999/08/02 10:47:11 dvermeir Exp $ #include <iostream> #include "message.h" int main(int argc,char *argv[]) { std::cout << message << std::endl; }In hello.C, the "#include" directive on message.h ensures that "message" is declared so that the compiler knows its type etc.
// $Id: message.C,v 1.2 1999/08/02 10:47:11 dvermeir Exp $ #include "message.h" const std::string message("hello world");Also message.C uses the "#include" directive on message.h. This is useful because, when compiling message.C, the compiler first sees a declaration (from message.h) and then a definition of "message" which allows it to check the consistency of one with the other. Chaos would ensue if message.C would not include message.h and e.g. "message" was declared as an integer in message.h and defined as a string in message.C).
#ifndef MESSAGE_H #define MESSAGE_H // $Id: message.h,v 1.3 1999/08/07 08:54:50 dvermeir Exp $ #include <string> extern const std::string message; #endifNote that the entire file message.h is conditionally included, using a preprocessor #ifndef...#endif directive. Together with the #define MESSAGE_H on the second line, this ensures that message.h will be included at most once in any compilation.
tinf2% ldd hello libstdc++.so.2.9.0 => /usr/lib/libstdc++.so.2.9.0 libm.so.1 => /usr/lib/libm.so.1 libc.so.1 => /usr/lib/libc.so.1 libdl.so.1 => /usr/lib/libdl.so.1 tinf2% echo $LD_LIBRARY_PATH /usr/lib:/usr/openwin/lib: tinf2%
g++ -c hello.C message.Cwill produce both hello.o and message.o.
#include <tbcc/err.h>where the full pathname of the file to be included is /usr/local/include/tbcc/err.h, one would use
g++ -c -I/usr/local/include hello.C
g++ -c -Wall -Wconversion hello.CThe -g option tells g++ to include in the object file extra debug information that is needed by run-time debuggers such as gdb or ddd.
g++ -c -g hello.C
g++ -c -O2 hello.C
g++ -o hello hello.o message.owill produce the executable file "hello". If you need to link with libraries4, use the -L and -l options. The -L option is followed by the directory of the library file while the -l option is followed by the identification of the library: use "-labc" to link with the library "libabc.a" (for static linking) or "libabc.so" (for dynamic linking).
g++ -o hello hello.o -L/usr/local/lib -ltbcc -L/usr/local/lib/mysql -lmysqlwill produce the executable file hello, which will be dynamically linked with libtbcc.so and libmysql.so. Note that, since we did not specify any runpath, at run time, the dynamic loader will attempt to find libtbcc.so and libmysql.so in a directory from the LD_LIBRARY_PATH variable. To set the runpath:
g++ -o hello hello.o -Wl,-rpathwill ensure that, at run time, the dynamic loader will try to find libtbcc.so in $HOME/mylib or /usr/local/lib (note that $HOME is expanded before it is stored in the runpath).
-Wl,$HOME/mylib:/usr/local/lib -L/usr/local/lib -ltbcc
g++ -o hello hello.o -R$HOME/mylib:/usr/local/lib -L/usr/local/lib -ltbccwill ensure that, at run time, the dynamic loader will try to find libtbcc.so in $HOME/mylib or /usr/local/lib.
g++ -o hello hello.o message.o -static -L/usr/local/lib -ltbccwill result in a "stand-alone" executable file. To statically link some static libraries, just list them as you would any object file:
g++ -o hello hello.o message.o mylibs/mylib.awill include the needed objects from the static libraries mylibs/mylib.a and mylibs/junk.a into the resulting executable.
mylibs/junk.a L/usr/local/lib -ltbcc
ar cru libmine.a f1.o f2.o f3.owill create a static library libmine.a.
g++ -c -fpic f1.C f2.C f3.CTo actually create the library, it suffices to use the -o and -shared options.
g++ -shared -o libmine.so f1.o f2.o f3.o
g++ -E message.C >message.e
g++ -pg -g -o hello hello.C message.CUse the gprof command to display this information in a textual format.
hello
gprof -C hello >hello.profile
# $Id: Makefile,v 1.1 1999/12/21 13:02:17 dvermeir Exp $ CCFILES= hello.C message.C hello: $(CCFILES:%.C=%.o) g++ -o hello $^ # Dependencies are supposed to be in a file ``make.depend'' # which is inclduded by make. include make.depend # Because of the following rule, ``make'' will attempt to # create ``make.depend'' if it does not exist or if one # of the files in $(CCFILES) is more recent than ``make.depend'' make.depend: $(CCFILES) g++ -M $^ >$@See the Section on make in "Unix for beginners" for more information on the make program.
76 dv2$ make Makefile:5: make.depend: No such file or directory g++ -M hello.C message.C >make.depend g++ -c hello.C -o hello.o g++ -c message.C -o message.o g++ -o hello hello.o message.o 77 dv2$A fragment of the make.depend file generated for the example is shown below.
hello.o: hello.C /usr/local/include/g++/iostream \ /usr/local/include/g++/iostream.h /usr/local/include/g++/streambuf.h \ /usr/local/include/g++/libio.h \ /usr/local/i386-pc-solaris2.7/include/_G_config.h \ /usr/local/lib/gcc-lib/i386-pc-solaris2.7/egcs-2.91.66/include/stddef.h \ message.h /usr/local/include/g++/string \ /usr/local/include/g++/std/bastring.h /usr/local/include/g++/cstddef \
| |
-Wl,rpath,/usr/local/lib6The fpic option causes g++ to create "position independent" code.