Skip to content

cnruby/w3h1_cmake

 
 

Repository files navigation

Hello, Package!

Finding an Executable Package For C++ Code



@Gitter :gitter.im/cnruby
Code ID: basic_117
Code Name: Hello, Package!

TABLE of CONTENTS

Demonstrate Finding an Executable Package

The Structures of Project

#<!-- markdown-exec(cmd:cat docs/output/tree.txt) -->#
.
├── cmake
│  ├── CMakeLists.txt
│  └── config.h.in
├── CMakeLists.txt
├── config
│  └── config.hxx
└── src
   ├── CMakeLists.txt
    └── main.cxx
#<!-- /markdown-exec -->

The Folder's Structure

_image

The Command's Structure

_image

The Process's Structure

CMake Files of The Project

_image

#<!-- markdown-exec(cmd:cat cmake/CMakeLists.txt) -->#
find_package(Git REQUIRED)
message(STATUS "GIT_FOUND\t\t= ${GIT_FOUND}")
if(GIT_FOUND)
  message(STATUS "GIT_EXECUTABLE\t= ${GIT_EXECUTABLE}")
  message(STATUS "GIT_VERSION_STRING\t= ${GIT_VERSION_STRING}")
  execute_process(
    #COMMAND git rev-list --max-count=1 HEAD
    COMMAND ${GIT_EXECUTABLE} rev-list --max-count=1 HEAD
    # User-defined Variable "GIT_REVISION"
    OUTPUT_VARIABLE GIT_REVISION
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  message(STATUS "GIT_REVISION\t\t= ${GIT_REVISION}")
else()
  message(STATUS "Git NOT FOUND")
endif(GIT_FOUND)

configure_file(
  ${PROJECT_SOURCE_DIR}/cmake/config.h.in
  ${PROJECT_SOURCE_DIR}/config/config.hxx
)

#<!-- /markdown-exec -->

The Listfile of Folder 'cmake'

//<!-- markdown-exec(cmd:cat cmake/config.h.in) -->//
#ifndef CMAKE_CONFIG_H
#define CMAKE_CONFIG_H

const char *GIT_VERSION   = "@GIT_VERSION_STRING@";
const char *GIT_REVISION  = "@GIT_REVISION@";

#endif
//<!-- /markdown-exec -->

The Template File of Folder 'cmake'

C++ Files of The Project

//<!-- markdown-exec(cmd:cat config/config.hxx) -->//
#ifndef CMAKE_CONFIG_H
#define CMAKE_CONFIG_H

const char *GIT_VERSION   = "2.21.0";
const char *GIT_REVISION  = "e5b2c4b722bad446d2b9673bc0daaa507aeaabd6";

#endif
//<!-- /markdown-exec -->

The C++ Config File of Folder 'config'

//<!-- markdown-exec(cmd:cat src/main.cxx) -->//
#include <iostream>
#include "config.hxx"

int main(int, char **)
{
    std::cout << "Hello, C++ Package!" << std::endl;

    std::cout << "git version:\t\t" << GIT_VERSION << std::endl;
    std::cout << "git revision hash:\t" << GIT_REVISION << std::endl;
}
//<!-- /markdown-exec -->

The C++ Main File of Folder 'src'

_image

Final Summary

感谢大家观看!

@Gitter: gitter.im/cnruby

@Github: github.com/cnruby

@Twitter: twitter.com/cnruby

@Blogspot: cnruby.blogspot.com

References

The Project's Commands

Formatting The Codes

ruby format-codes.rb

Get The Code with Shell Commands

git clone https://github.com/cnruby/w3h1_cmake.git basic_117
cd basic_117
git checkout basic_117
code .

Build and Run The Project

cmake -GNinja -Bbuild/
cmake --build build/ --clean-first -v
cmake --build build/ --target clean
cmake --build build/ --clean-first -v &> v11.txt
# generate the configure file
rm -rf config
cmake --build build/ --clean-first
cmake -Bbuild/
cmake --build build/ --target rebuild_cache
cmake --build build/ --clean-first
./bin/main_117
#  update the configure file
cmake --build build/ --clean-first
# others
markdown-exec README.md
code build/build.ninja
ruby format-codes.rb
cmake --check-system-vars --build build/
cmake --help-command find_package | less