CMake and Useful Info
CMake is an open-source tool to build, test, and package software applications. CMake provides control over the compilation process and platform-independent configuration file. So we can generate a Makefile of environmental choice.?
Installation Procedure for CMake:
- Download ZIP file from URL:?https://cmake.org/download/
- Unzip the CMake source
- Run " ./bootstrap"
- Run "make"
- Run "make install"
Let's look at the structure of CMake file - CMakeLists.txt?
cmake_minimum_required(VERSION 3.10
#setting the project name
Project(TestProject)
?
#adding the executable
add_executable(Runner, Runner.cpp))
Let's consider the basic program to print the array of elements and write the CMakeLists.txt file.?
Program ArrayOfElements.cxx:
#include<iostream
using namespace std;
int main() {
?? int list[]={1,2,3,4,5,6};
?? for(int i=0;i<6;i++) {
?????? cout<<"\t"<<list[i];
?? }
?? cout<<"\n";
?? return 0;
}
Writing the CMakeLists.txt file to generate the binary which prints the elements of the array
CMakeLists.txt:-
cmake_minimum_required(VERSION 3.10
project(ArrayList)
add_executable(Test ../ArrayOfElements.cxx))
?Let's see If we added a vector of elements and used templates to print variables for different types. So We need to c++11 version to compile the complete code. So If we run the above CMake file, it will give the compilation issues. To resolve that we need to add the c++11 version to the compilation. Eg: g++ -std=c++11 <fileName.cxx> -o <Executable>
How to provide such options to c++11 to Cmakelist.txt file.?
Consider the following CMakeLists.txt to compile with C++11
version.cmake_minimum_required(VERSION 3.10)
let's consider the following C++ Program required to compile with the C++11 Version.
#include<iostream
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
?
template<class T> void display_vector(std::vector<T> input) {
?? //Iterator need to be declared as typename before iterator otherwise error will come
?? typename vector<T>::iterator itr;
?? itr = input.begin(); // initializing the vector beging pointer to the iterator
?? while(itr != input.end()) // listing all elements untill the itr reaching to vectors
?? {
?????? cout<<"\t"<<*itr;
?????? ++itr; // increamenting the iterator to point to next memory location
?? }
?? cout<<"\n";
}
template<class T> void display_data(std::vector<T> input)
?? //auto iterator usage - checking each values inside the vectors
?? auto itr=input.begin();
?? while(itr != input.end()) {
?????? cout<<"\t"<<*itr;
?????? ++itr;
?? }
?? cout<<"\n";
}
?
int main() {
?? //calling the user utility funciton
?? std::vector<int> intList {1,2,3,4,5};
?? std::vector<char> charList {'a','b','c'};
?? std::vector<string> strList {"shriaknt","badiger"};
?? std::vector<float> floatList {1.1,1.5};
?
?? //calling the print functions
?? display_vector(intList);
?? display_vector(charList);
?? display_vector(strList);
?? display_vector(floatList);
?? //calling the print functions
?? display_data(intList);
?? display_data(charList);
?? display_data(strList);
?? display_data(floatList);
?
?? return 0;
}
CMake file for specifying the C++11 verision:
#setting the project name for compilation proces
project(Test)
#Setting the C++11 standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED Ture)
#adding the executable to main program
add_executable(Runner? ListingMaxSum.cxx)s
Reference for the long list of CMakeList.txt used in this projects:
?Now, Let's consider a utility function to print the elements of the vector of each type moved to the shared object. Create a separate utility folder and add the following files to create a shared object.
Use the shared object in the main function to print each element of the vector of different types.
?Make a folder "Utils" and?add the following .cxx for creating the shared object.
Utils/PrintVector.cxx:
#include<vector
#include<iostream>
using namespace std;
?
template<class T> void printElements(std::vector<T> inputs) {
??? auto itr = inputs.begin();
??? while(itr != inputs.end()) {
????? cout<<"\n Element - "<<*itr;
????? itr++;
??? }
??? cout<<"\n";
}
?Add the following CMakeLists.txt to create the shared object.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10
#Setting the C++11 standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED Ture)
add_library(Utils SHARED PrintVector.cxx))
?add_library function will create the shared object from the given file and include this shared object for further compilation. Include the "Utils/PrintVector.cxx" file in ListMaxSum.cpp file and update the CMakeLists.txt to utilize the shared object.
?CMakeLists.txt for shared object:
cmake_minimum_required(VERSION 3.10
?
#setting the project name for compilation process
project(Test)
?
#Setting the C++11 standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED Ture)
?
# Adding the Util functions to compilation Process
#below line indicates about the shared library folder inclusion
add_subdirectory(./Utils)
#adding the executable to main program
add_executable(Runner? ListingMaxSum.cpp)
?
#adding the target library for compilation process
target_link_libraries(Runner PUBLIC Utils)
?
#It will include complete Binary Directory and Share Library in single folder
target_include_directories(Runner PUBLIC
?????????????????????????? "${PROJECT_BINART_DIR}"
?????????????????????????? "${PROJECT_SOURCE_DIR}/Utils"
????????????????????????? )
CONTINUE......!!
Corporate Trainer - LinkedIn & Recruitment Trainer ? Job Hunt Coach ?? LinkedIn Marketing - Helping MNCs | MSME | Startups | Job Seekers ? I’ll help getting High-ticket Clients & MNC Interviews in 90 Days
2 å¹´#Hiring Freelance corporate IT Trainer Skills : #Cmake ,#Conan and #Docker Virtual Training 8+ years professional with hands on experience Must be training 4 hours per day Please share this requirement in your trainer circle and share the CV to senthilkumar.g@knowledgehut.co #opensourcesoftware #opensource #dockers #ittraining #training #devops #devopsjobs #freelancetrainer