Showing posts with label cmake. Show all posts
Showing posts with label cmake. Show all posts

CMakeLists.txt and cmake interview questions

What is a cmake?
cmake is a cross platform open source software for managing build system of a software. It generates native makefile which is the recipe file for native make tool to create any application in Linux. It is also used with other native build environments such as make Apple's Xcode, and Microsoft Visual Studio.
It supports directory hierarchies. Each directory contains a CMakeLists.txt file.
It supports applications that depends on multiple libraries.
What is CMakeLists.txt?
CMakeLists.txt file contains instruction to "cmake" tool to generate a native Makefile which the instructions file for "make" to create an application.

A Simple multi folder camke project example say MyProject.Simple cmake demo example can be found here.
MyProject
├── CMakeLists.txt
├── myapp
│   ├── CMakeLists.txt
│   └── main.cpp
└── mylibs
    ├── CMakeLists.txt
    ├── mylib.cpp
    └── mylib.h
 

Top most MyProject/CMakeList.txt
cmake_minimum_required(VERSION 2.8)
add_subdirectory(mylibs)
add_subdirectory(myapp)

myapp/CMakeLists.txt
add_executable(app main.cpp)
target_link_libraries(app mylib)

myapp/main.cpp
#include<iostream>
using namespace std;
#include "../mylibs/mylib.h"
int main()
{
cout<<"main"<<endl;
libfun1();
libfun2();
return 0;
}

mylibs/CMakeLists.txt
add_library(mylib mylib.cpp)

mylib/mylib.cpp
#include<iostream>
using namespace std;
void libfun1()
{
cout<<"libfun1"<<endl;
}
void libfun2()
{
cout<<"libfun2"<<endl;
}

mylib/mylib.h
void libfun1();
void libfun2();

How to build in a separate direcory
cd MyProject
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX:PATH=/tmp/foo
make
make install.
Summary:
    Using CMake with executables
    add_executable(myapp main.c)

    Using CMake with static libraries
    add_library(test STATIC test.c)

    Using CMake with dynamic libraries
    add_library(test SHARED test.c)

    Linking libraries to executables with CMake
    add_subdirectory(libtest_project)
    add_executable(myapp main.c)
    target_link_libraries(myapp test)

make and makefile interview questions

Source code can be found here .
 Just download or clone and build by running command "make".
Q)What is make?
->make is a build automation tool that automatically build executables and libraries by reading a file called Makefile which tells the make how to compile and link a program.
Q)What is a makefile?
Makefile is the recipe(rule to build) file, which tells the build automation tool "make" how to compile and link.
Q)What does a makefile contains?
A makefile mainly contains directives(rules) like target,dependency and and rule. It also contains "set" directive to set a variable and comments, statements stating with a #.
Syntax of a makefile:
target: dependencies
[ a tab ]recipe (system commands)

Where: target, is name of the executable(binary),object file or any action like "clean".
dependency, is the input files required to create the binary and
recipe, are the system commands that make carries out.
Q)What is the difference between makefile and Makefile?
Order of execution is the difference.
If for execution "make" is not given "-f" option then it first looks for a file "makefile" then "Makefile".
If for execution "make" is given "-f" option then it first looks for a file "Makefile" then "makefile".

A simple example of makefile:
hello: main.o factorial.o hello.o
g++ main.o factorial.o hello.o -o hello
main.o: main.cpp functions.h
g++ -c main.cpp
factorial.o: factorial.cpp functions.h
g++ -c factorial.cpp
hello.o: hello.cpp functions.h
g++ -c hello.cpp
clean:
-rm *.o
---------------------------
code:
//main.cpp
#include <iostream>
using namespace std;
#include "functions.h"

int main(){
   print_hello();
   cout << endl;
   cout << "The factorial of 5 is " << factorial(5) << endl;
   return 0;
}

//functions.h
void print_hello();
int factorial(int n);

//functions.cpp
#include "functions.h"
int factorial(int n){
   if(n!=1){
      return(n * factorial(n-1));
   }
   else return 1;
}
//hello.cpp
#include <iostream>
using namespace std;
#include "functions.h"
void print_hello(){
   cout << "Hello World!";
}

CMakeLists.txt and cmake interview questions