How to apply C code format from the command line

We believe that defining formatting rules and coding accordingly is important, especially in multi-person projects, to improve the readability of your code.

On the other hand, it is a costly task for each developer to remember the formatting rules and write the code according to them, and to check whether the code written by other developers follows the formatting rules, so it is automatic. It is better to use a code formatter that has the same format.

Regarding the timing and method of passing the code formatter, "The person who writes the code passes the formatter each time" "Set to be automatically formatted when saving the file" "Set to be automatically formatted when committing" " There are various possibilities such as "formatting using commands from the command line", and to prevent forgetting to format, it is better to format automatically at the time of saving or committing, but that is the command from the command line. In some cases it is useful to be able to format using. That's why (although it seems to be forcible), here I would like to introduce a method of formatting from the command line for C languages.

Eclipse

Eclipse is a well-known integrated development environment. It is mainly used for Java program development, but it can be used for program development in various languages by inserting a plug-in.

In Eclipse, you can set formatting rules for the entire workspace or for each project, and you can format them on the GUI, but you can actually format them from the command line as well.

To format from the command line to follow the formatting rules set in your project:

$ eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -config $PROJ_DIR/.settings/org.eclipse.jdt.core.prefs Hoe.java Fuga.java

Where $ PROJ_DIR is the top directory of the project where the format rules are set.

It seems that only Java can be formatted from the command line at the moment. For example, if you have installed a plugin for C / C ++ program development called CDT, you can format C / C ++ code from the GUI, but it seems that you cannot format it from the command line. Sorry.

ClangFormat

ClangFormat is a formatter for C / C ++ / Objective-C / Objective-C ++ that comes with clang. You can use it from the command line with the clang-format command.

For example, to format with Google's format style:

$ clang-format -i -style=google Hoe.h Hoe.cpp

Adding -i will overwrite the original file.

The drawback of ClangFormat is that it's relatively cumbersome to install (you need to build from the LLVM source code: as of March 2014). However, the built clang-format works independently, so when using it in a project, everyone uses the binary built by one person to prevent the difference in behavior due to the difference in the version of the original source code. Would be good.

Artistic Style

(Artistic Style) [http://astyle.sourceforge.net/] is a code formatting tool for C / C ++ / C # / Objective-C / Objective-C ++ / Java.

For example, to format C # code with Google's format style:

$ astyle --style=google --mode=cs --suffix=none Hoe.cs Fuga.cs

The original file will be overwritten by adding --suffix = none.

ʻA style --help` will give you a detailed description of the command line options.

SublimeAStyleFormatter

As a front end of Artistic Style, there is Sublime AStyle Formatter which is a plug-in for Sublime Text 2. For this, "Introducing formatter to Sublime Text 2" will be helpful.

The method of customizing the behavior of Sublime AStyle Formatter is relatively undocumented, but you can understand it by looking at the following page.

Suppression of formatting

When processing the source code with a script etc., you may want to suppress formatting for a part of the source code. This section describes the suppression method for each formatter.

Eclipse

Enclose the block for which you want to suppress formatting with // @formatter: off and // @formatter: on.

// @formatter:off
...
// @formatter:on

However, this tag must be enabled in the Java formatter settings.

スクリーンショット 2015-02-06 12.16.09.png

ClangFormat

Enclose the block for which you want to suppress formatting with // clang-format off and // clang-format on. (Reference: CLANG-FORMAT STYLE OPTIONS)

// clang-format off
...
// clang-format on

It seems that this specification is valid from 3.6 of ClangFormat. I tried it with 3.5 and it was not recognized.

Artistic Style

Enclose the block for which you want to suppress formatting with // * INDENT-OFF * and // * INDENT-ON *. (Reference: Artistic Style 2.05)

// clang-format off
...
// clang-format on

Summary

We have introduced Eclipse, ClangFormat, and Artistic Style as tools for formatting files from the command line. Personally, if you are developing with Java + Eclipse, Eclipse, if you are developing with a C language other than C #, Clang Format or Artistic Style, if you are developing with C #, I think it is Artistic Style.

Recommended Posts

How to apply C code format from the command line
How to run a Kotlin Coroutine sample from the command line
How to run a GIF file from the Linux command line (Ubuntu)
To use the "java" command line tool ... How to avoid popping up
How to call Swift 5.3 code from Objective-C
[Flutter] How to use C / C ++ from Dart?
Using JUnit from the command line on Ubuntu
How to download and run a Jar package directly from the Maven repository with just the command line
[IntelliJ IDEA] How to format only local changes when saving the source code
How to get the date from the JavaScript Date type that C # developers are addicted to
How to write Scala from the perspective of Java
How to determine the look-ahead request (Prefetch) from the browser
Notes on updating Ubuntu from the command line (16.04 → 18.04, 18.04 → 20.04 both)
How to transition from the [Swift5] app to the iPhone settings screen
How to specify character code and line feed code in JAXB
How to write good code
[IOS] How to get the table name from AWS DynamoDB
How to create a form to select a date from the calendar
How to set character code and line feed code in Eclipse
How to get the longest information from Twitter as of 12/12/2016
How to disable Set-Cookie from API on the front side
Learn how to customize the Navigation Bar from the sample app
How to migrate from JUnit4 to JUnit5
[Integration test code] How to select an element from date_select
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to make Ubuntu a fixed IP address on the command line (explained as carefully as possible)
How to create a jar file or war file using the jar command
Install lsb_release from the command line when lsb_release fails in docker environment
Only this! How to prevent forgetting code format in team development
[For beginners] How to get the Ruby delayed railway line name
[Java] How to convert from String to Path type and get the path
How to increment the value of Map in one line in Java
How to use the link_to method
How to use the include? method
How to push from Tarminal to GitHub
How to use the form_with method
How to find the average angle
How to use the wrapper class
Switch from Eclipse to VS Code
How to write easy-to-understand code [Summary 3]
How to add the delete function
The road from JavaScript to Java
[RSpec] How to write test code
How to change from HTML to Haml
How to narrow down the image format from the gallery on Android and then select and import multiple images
How to place geckodriver (Selenium WebDriver) on the path using Maven command
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
How to return a value from Model to Controller using the [Swift5] protocol
[Java] How to retrieve the parameters passed from html on the server side
[Java] How to get the current date and time and specify the display format
How to get the current date as a string in yyyyMMdd format
[Docker] How to build when the source code is bind-mounted on the container
How to compare only the time with Rails (from what time to what time, something like)
How to get the setting value (property value) from the database in Spring Framework
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
[swift5] How to change the color of TabBar or the color of item of TabBar with code
[Java: memorandum] Until the line feed code CRLF is changed to LF
[Java] How to use the File class
Investigate the replacement from Docker to Podman.
How to delete the wrong migration file