Krishna-13-cyber / helloworld-cmake

A Hello World project that uses cmake to compile the source code to an executable file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

helloworld-cmake

This sample is showing you how you can use cmake and CMakeLists.txt file to compile your c++ application.

Environment Setup

Make sure g++ installed. If not go to Build and run your first application with g++ for more help.

Also make sure make command is installed. If not go to Build and run your first application with make for more help.

Run following command to see if cmake is installed:

$ cmake --version
zsh: command not found: cmake

If you face command not found: cmake that means it is not there.

Installing make

  • MacOS
    $ brew install cmake
    
  • Linux
    • Debian/Ubuntu
      $ sudo apt install cmake
      

Building Hello World application

Create a folder called build and then use cmake command to build the application. It uses Makefile compile the sources to object files.

$ mkdir build
$ cd build
$ cmake .. -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CC_COMPILER=gcc
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_CC_COMPILER

-- Build files have been written to: /Users/nkabiliravi/git/mycpptutorial/helloworld-cmake/build

Now use cmale --build command link the object files into a file called greet:

$ cmake --build .

You can also use make command:

$ make
Scanning dependencies of target greet
[ 33%] Building CXX object CMakeFiles/greet.dir/hello.cpp.o
[ 66%] Building CXX object CMakeFiles/greet.dir/main.cpp.o
[100%] Linking CXX executable greet
[100%] Built target greet

Running the app

Run greet command like this:

$ ./bin/greet
Hello World!

Visit Build and run your first application with cmake to learn more about this sample.

About

A Hello World project that uses cmake to compile the source code to an executable file

License:MIT License


Languages

Language:C++ 82.5%Language:CMake 11.0%Language:C 6.5%