On the University central Linux servers (gul3, gul4, gul5), the C/C++ compiler command is g++
though for simple compilations you can use the command: c
This will output "Hello World!".
Notes:
Creating and using libraries
sub1.c , sub2.c , sub3.c and
main.c
$ edit hello.c
----------------------------------------
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
}
----------------------------------------
To compile the file 'hello.c'
$ c hello.c
or use g++ with -o option
$ g++ hello.c -o hello
To execute the file 'hello'
$ hello
can be gained by using the g++ command
(try g++ --help or man g++ etc)
system@gantep.edu.tr
$ g++ main.c sub1.c sub2.c sub3.c -o main
Then, the command
main
will output
The sum is 5
The product is 6
The largest is 3
$ g++ sub1.c -c
$ g++ sub2.c -c
$ g++ sub3.c -c
These commands create the object files sub1.o, sub2.o and sub3.o
A library whose name is test of these functions can be created as follows:
$ ar rcvf libtest.a sub1.o sub2.o sub3.o
(see man ar)
You can add more objects to the library with:
$ ar rcvf libtest.a sub4.o
To list the contents of the library:
$ ar tv libtest.a
To list functions in a library:
$ nm libtest.a
(see man nm)
To delete an object from the library:
$ ar dv libtest.a sub3.o
$ g++ main.c -o main -L. -ltest
Note that lib and .a are dropped.The -L. options tells the linker
that the library can be found in the current directory.
If the library is not in the current directory then you should give the path:
$ g++ main.c -o main -L/home/yilmaz/C/lib -ltest
or
$ g++ main.c -o main -L$MYLIB -ltest
with
MYLIB=/home/yilmaz/C/lib