Skip to content

cnruby/w3h1_cmake

 
 

Repository files navigation

basic_108

Hello, C++ Style!

Formatting the 'C/C++'s Codes

@Gitter :gitter.im/cnruby

TABLE of CONTENTS

Requirements

How to Install A Tool 'clang-format' of Formatting C/C++ Code

# MacOS 12.10 above
brew install clang-format

Get the Project

git clone https://github.com/cnruby/w3h1_cmake.git basic_108
cd basic_108
git checkout basic_108
code .
cmake -GNinja -Bbuild/
cmake --build build/

Use the Tool 'clang-format'

How to Format A C/C++ File with A Style

# Clang format predefined styles
# styles = ( LLVM Google Chromium Mozilla WebKit Microsoft )
clang-format -style=Google src/main.cxx
clang-format -style=Google -i src/main.cxx

Demonstrate Formatting A C/C++ File

How to Create the Format File '.clang-format' with A Style

# the style file name must be ".clang-format"
clang-format -style=Google -dump-config
clang-format -style=Google -dump-config > .clang-format
clang-format -style=Mozilla -dump-config > .clang-format

Demonstrate Creating the Format File '.clang-format'

What does The Default Style of 'clang-format'?

# clang-format default style 'LLVM'
clang-format -style=file src/main.cxx

image

Demonstrate the Default Style of 'clang-format'

Compare the Different Formatting Styles

Code of Comparesion Styles

#<!-- markdown-exec(cmd:cat compare.cmake) -->#
# The File compare.cmake
set(STYLES LLVM Google Chromium Mozilla WebKit Microsoft)
foreach(style ${STYLES})
  message("C/C++ style = <<${style}>>")
  set(MAIN ${CMAKE_CURRENT_SOURCE_DIR}/src/main)
  set(CMD
      "clang-format -style=${style} ${MAIN}.cxx > \
                    ${MAIN}.${style}.cpp")
  message(${CMD})
  execute_process(COMMAND clang-format -style=${style} ${MAIN}.cxx
                  OUTPUT_FILE ${MAIN}.${style}.cpp)
endforeach()
#<!-- /markdown-exec -->

Run the CMake Scripting Program

# delete all files "src/main.*.c*"
# rm src/main.*.c*
# run the file 'compare.cmake'
cmake -P compare.cmake

Demonstrate Comparing the Different Formatting Styles

How to Format All C/C++ Files

Code of Formatting All Files

#<!-- markdown-exec(cmd:cat format-clang.rb) -->#
# The File format-clang.rb
# format-clang.rb
# ls src/main.*.c*
# rm src/main.*.c*
# cmake -P compare.cmake
# ruby format-clang.rb
#EXTENSIONS = %w[cxx cpp hpp cu c h]
EXTENSIONS = %w[cxx cpp]
EXTENSIONS.each do |ext|
    puts "ext = #{ext}"
    `clang-format -style=file -i $(find . -name "*.#{ext}")`
end
#<!-- /markdown-exec -->

Run the Ruby Scripting Program

# run the file 'format-clang.rb'
ruby format-clang.rb

Demonstrate Formatting All C/C++ Files

Disabling Formatting on a Piece of Code

image

Final Summary

image

感谢大家观看!

@Gitter: gitter.im/cnruby
@Github: github.com/cnruby
@Twitter: twitter.com/cnruby
@Blogspot: cnruby.blogspot.com

References

C/C++ Basic Concepts

  • The GNU Compiler Collection (GCC) is a collection of compilers and libraries for C, C++, Objective-C, Fortran, Ada, Go, and D programming languages.
  • LLVM is a C/C++ compiler toolset just like GCC
  • LLVM can compile C, C++ and Objective-C.
  • Clang provided by the LLVM toolset is able to compile C and C++ codes faster than GCC.
  • The LLVM debugger LLDB is much more memory efficient and very fast at loading symbols compared to GCC.
  • LLVM supports C++11, C++14 and C++17 through libc++ and libc++ ABI projects.
  • LLVM is available on Linux, Windows and Mac OS X. So it’s cross platform.
  • The default GCC compiler in Ubuntu
  • The default Clang compiler in MacOS

How to install the Tool 'clang-format' on Ubuntu 20.04

# https://cnruby.blogspot.com/2020/04/howto-install-clang-10include-clang-and.html
sudo apt-get install clang-tools-10