Saturday, 21 October 2017

Quick basic guide for compiling in C

* Basic simplest way to compile a c++ file

$ g++  file.cpp
This will generate an executable 'a.out' which we can execute to run the program
$ ./a.out
output of the program here.

* If we desire to have a specific name for the generated file we can do:
$ g++ file.cpp -o specificname.o
$ ./specificname.o

* Make
By running the command make, we can organize the compilation in a single file named Makefile. Makefiles are a simple way to organize code compilation.

Run make in the location where your Makefile is to compile and link everything.
If you want verbose output, you can type VERBOSE=1 make (helpful if something goes wrong).
--> A guide for doing Makefiles:

* CMake
CMake is another way to compile the code specially if you need to link several libraries and the project start getting more complex. You just need to have a CMakeLists.txt file.

First, create a build folder and go inside the folder.
Inside your build directory, point cmake to the directory containing the  CMakeLists.txt file. 
$ cmake ..the .. will point to the parent location where your code is.

If all goes well, cmake will process your CMakeLists.txt files, find the location of all libraries and include paths and spew a bunch of configuration information including a traditional Makefile in your build directory.
From here, you can use the traditional make stated in the previous item.
Run make in your build directory

--> A guide for cmake can be found here:

** ccmake 
ccmake is an interface for cmake. Project configuration can be specified interactively through this GUI.

--> A guide for ccmake

CMakeLists.txt: You can create a file called 'CMakelists.txt'.

Useful resources