GithubHelp home page GithubHelp logo

cmake_learn's Introduction

CMake_Learn

My note from CMake Tutorial

Episode 1 Summarized by Komsun Tamanakijprasart

Table of contents

makefile

  • Create and name a new file makefile in your working directory
  • makefile doesn't accept space, use tab instead

Easy example:

makefile

default:
	g++ main.cpp -o out

You just need to type make in the terminal to execute the makefile (compile and build in this case)

  • make to execute the makefile
  • ./out to run the application

Linux or Windows terminal

make
./out

Results image

CMakeLists.txt

  • CMake relies on a top-level file called CMakeLists.txt (exact spelling!)

Project Workflow
Step 1: Prepare your source file main.cpp
Step 2: Create CMakeLists.txt, a separate folder for built files, and run cmake
Step 3: Run make

cmake

2 Important points in CMakeList

  • Where your source file is (contain CMakeLists.txt)
  • Where your build folder is

Linux terminal

cmake -S <path-to-source> -B <path-to-build>

Ex:

Linux terminal

cmake -S . -B build/
  • -S . means the source (and CMakeList.txt) is located in the current directory

Example 1: Creating a Project

Step 1: Prepare main.cpp

#include <iostream>
int main()
{
std::cout << "Good morning!\n";
return 0;
}

Step 2: Create CMakeLists.txt and run cmake

CMakeLists.txt

cmake_minimum_required(VERSION 3.16.3)
project(myProject)
add_executable(${PROJECT_NAME} main.cpp)

What are these lines?

  • cmake_minimum(VERSION xxxxx) : Minimum required version
  • project(projectName) : Add a project
  • add_executable(programName source) : Build the executable file
  • ${PROJECT_NAME} : Built-in keyword to refer to the project name

Linux Terminal

cmake -S . -B obj/

Result

  • makefile is created along with other files image

Step 3: Run make and your program

Linux Terminal - at where the makefile is located

make

Results

image

image

Notes:

Check CMake version

Linux terminal

cmake --version

Example 2: Installing your program

Step 1 : Prepare main.cpp

Step 2 : Create CMakeLists.txt and run cmake

CMakeLists.txt

cmake_minimum_required(VERSION 3.16.3)
project(Trim)
add_executable(trim main.cpp)
install(TARGETS targetName DESTINATION bin)

What are these lines?

  • install(TARGETS targetName DESTINATION xxx) : specify where to install the executable file (or library), bin is a standard place to install (usually usr/local/bin/)
  • Usually, we create a separate folder for the built files (in this case, build)

Linux terminal

cmake -S ..
  • -S .. means the source (and CMakeLists.txt) is in the previous directory
  • no -B means the current directory is used for build

image

  • Now, in build, the makefile is created and configured along with many other files image

Step 3 : run make and your program

image

Not sure what to make? type make help image

install() on CMakeLists.txt

install(TARGETS <targetName> DESTINATION <destination>)

Example:

CMakeLists.txt

cmake_minimum_required(VERSION 3.16.3)
project(Trim)
add_executable(trim main.cpp)
install(TARGETS trim DESTINATION bin)

Install the program, as specified in CMakeLists.txt

Linux terminal

sudo make install

image

Because we install the program on the bin already, we can call this program ANYWHERE !! (Don't need to locate the program's directory anymore!) image

cmake_learn's People

Contributors

komxun avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.