The simplest way to compile a single c/c++ file is as follows:
gcc hello.c
or
g++ hello.cpp
This will create an executable a.out in the current directory. This can be executed with the command ./a.out to see if it works.
Both gcc and g++ use almost the same set of options. In the rest of this page, we use only g++ in our examples.
To create an executable foo instead of a.out by compiling hello.cpp:
To dynamically link your executable with libraries libxx01.a, libxx02.so, lying in directory /path/to/dir:
g++ -o foo hello.cpp -L /path/to/dir -lxx01 -lxx02
Whenever you execute foo, the /path/to/dir should be in your $LD_LIBRARY_PATH or in one of the system libraries path. Note that you should not write the whole name of the library file (libxx01.a).
To dynamically link your executable with libraries libxx01.a, libxx02.so, lying in directory /path/to/dir and also include their path in the runtime library search path:
g++ -Wl,rpath /path/to/dir -o foo hello.cpp -L /path/to/dir -lxx01 -lxx02
You don't need to have /path/to/dir in your $LD_LIBRARY_PATH.
To avoid linking your executable with libraries libxx01.a, libxx02.a dynamically, lying in directory /path/to/dir:
g++ -static -o foo hello.cpp -L /path/to/dir -lxx01 -lxx02
You don't need library files to execute foo now. Note that you can not use .so (shared object) libraries anymore.
To link your executable with both shared and static libraries:
g++ -o foo hello.cpp -L /path/to/dir -Wl,-Bstatic -lxx01 Wl,-Bdynamic -lxx02
This will link to libxx01.a even if libxx01.so is available.
To compile your program so that you are able to use gdb or valgrind to debug it:
g++ -ggdb -o foo hello.cpp -L /path/to/dir -lxx01 -lxx02
See the debugging section in the Development Tools page for more information on using gdb or valgrind.
To compile your program in the Compute Server or the Blaze Cluster so that it can be run on 32bit machines:
g++ -m32 -o foo hello.cpp -L /path/to/dir -lxx01 -lxx02
Note that libraries libxx01.a, libxx02.so should be present in the 32bit machine. You can also use -static flag to avoid this problem.
The correct version of MPI should first be put into the PATH. To do this, do:
export PATH=/usr/local/mpi/bin/:$PATHThen check if correct version of MPI is being used. Do:
which mpirunIt should display /usr/local/mpi/bin/mpirun as the output. The user should proceed only when this has been done and the correct output is visible. If the user makes a mistake while exporting the path, she should log out and log in again. This will reset the PATH variable. To compile a program for MPI, just replace gcc or g++ in the above commands with mpicc or mpiCC and it should work. e.g.:
mpiCC -ggdb -o foo hello.cpp -L /path/to/dir -lxx01 -lxx02### An example makefile ### ### default: g++ -shared -ggdb -Wl,-rpath /home/asm4/src/symphony/lib clp.cpp \ -I /home/asm4/src/symphony/include -L /home/asm4/src/symphony/lib \ -lOsiClp -lOsi -lClp -lCoinUtils -o clp.so ###If you save this file as Makefile and type make, it will execute the default command which is to compile a shared library. This is a very basic use of Makefile. Read man make or see this online howto.