CMake and Useful Info

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:

  1. Download ZIP file from URL:?https://cmake.org/download/
  2. Unzip the CMake source
  3. Run " ./bootstrap"
  4. Run "make"
  5. 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:

https://en.wikipedia.org/wiki/CMake#Applications_that_use_CMake

?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......!!

GS Kumar

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

赞
回复

要查看或添加评论,请登录

Shrikant Badiger的更多文章

  • NVMe Over TCP

    NVMe Over TCP

    NVMe over TCP is enhanced feature of NVMe over Fabrics. It used the standard network stack(Ethernet) without any…

    1 条评论
  • Bazel Build for C++ Software Application

    Bazel Build for C++ Software Application

    Bazel Tool is developed by google to automate the build process. Now It's an open source and it can be used by anyone.

  • C++ Class Layout

    C++ Class Layout

    Class Layout: Only non-static data members will contribute to the size of the class object. If we have static and…

    1 条评论
  • High-performance Computing in C++ : Open Muti Processing(OpenMP)

    High-performance Computing in C++ : Open Muti Processing(OpenMP)

    Open Multi-Processing: Let's consider the parallelization approaches, basically, we can think of imperative…

  • High-performance Computing in C++

    High-performance Computing in C++

    Single Instruction Multiple Data (SIMD) Multiple core CPUs and Multithreading: Declarative(OpenMP), imperative…

  • vSocket Interface - Guest to Host Communication

    vSocket Interface - Guest to Host Communication

    vSocket: VMware vSocket provides a very similar API to the Unix Socker interface for communication. vSocket library is…

  • Custom Memory Management in C++

    Custom Memory Management in C++

    Memory Management: Process in which memory allocation and de-allocation to the variable in running program and handle…

  • Pointers in C

    Pointers in C

    Pointers in C: Pointers are fundamental parts of C Programming. Pointers provide the lots of power and flexibility in C…

  • Interrupt !!

    Interrupt !!

    Processors need to detect hardware activities. There are multiple solutions to detect hardware activities.

  • PXE: Preboot Execution Environment

    PXE: Preboot Execution Environment

    PXE: Preboot Execution Environment. Technology helps computers to boot up remotely through a network interface.

社区洞察