Showing posts with label CMake. Show all posts
Showing posts with label CMake. Show all posts

Build a Project with CMake

In this article, I will talk about how to compile a project with CMake. I create a sample project directory via command promt from Windows:
cmake

After that I will create two different folders called source and build in this directory seperately:
Cmake
I will create a new file called CMakeLists.txt in Source directory. We must do it because we set how CMake should compile the project from the CMakeLists.txt file.

Let's go to the Build directory and run the CMake command:
Setting CMake
I typed cmake -G "MinGW Makefiles" ../Source command like in above. After this command, CMake will generate Makefiles into the Build directory. If you notice I used MinGW Makefiles. This is a generator that CMake supports one of the generators. I use Windows, therefore I used MinGW in my computer. If you use Ubuntu distro you can use Unix Makefiles as generator for CMake.

In the command, I specified the path of Source to reach CMakeLists.txt and we created Makefile script for our project via -G option.

I got a warning about usage of CMakeLists.txt, because I didn't add project() command in CMakeLists.txt. I'll mention it here shortly.

Let's open Build directory and we can see what CMake generated:
CMake
I opened CMakeLists.txt and I typed this lines:
cmake_minimum_required(VERSION 3.22.0)

project(sample)
So I want to build an example program. I will type a C++ program as an example:
#include <iostream>

int main()
{
    std::cout << "Hello World!" << std::endl;

    return 0;
}
Open CMakeLists.txt again and let's add executable as an output program with this command:
cmake_minimum_required(VERSION 3.22.0)

project(sample)

add_executable(${PROJECT_NAME} main.cpp)
That's fine, but I want to the create with different way now like in the below:
CMake build Source
Now we have to build target with make command. At first I need to go inside of Build directory:
CMake
We built target successfully. Now let's run it:
CMake Execute Command

Devamını Oku »

Quick Installation of CMake on Windows

CMake is a cross-platform build system. Firstly I have to install CMake on my computer. Let's download it from the official page. My operating system is Windows 10, and according to this I will download Windows x64 ZIP archive folder from binary distributions:

Installation of CMake

After the download, I copied the folder inside to the CMake folder I created on the C disk. After that, copy the path of this folder like in the below:

path of cmake
Open the environment variables on Windows and choose Path from environment variables panel and click Edit button. After that a window will be appeared, click New button and paste this path to empty cell and click Ok button twice.

Open the commant prompt to check if CMake is successfully installed.
CMake
Devamını Oku »