본문 바로가기
프로그래밍/C || CPP

[C || C++] Cpplint로 Google C++ Coding Style Guide 체크해보자.

by _BlankSpace 2019. 3. 24.

Google에서 제공하는 C++ Coding Style Guide는 프로그래밍을 한 사람이라면 한 번쯤은 봤을 것이다.


본 적이 없다 하더라도, 적어도 들어 본 경험은 있을 텐데.. (없다면, 지금이라도 보면 되는 것이고,)


여하튼, Cpplint는 Google의 C++ style guide에 따라 C/C++ 파일을 체크하는 커맨드라인 도구라고 생각하면 된다.


cpplint에 관한 GitHub 주소 링크는 아래와 같다.

https://github.com/cpplint/cpplint


그럼, cpplint를 설치하고, 실행해보자.


 # Cpplint를 설치하려면, pip 설치부터.. 


일단 Cpplint를 설치하기 위해서는, pip 설치부터 진행해야 한다.


설치는 굉장히 간단하다.


일단, pip가 미리 설치되어 있을 수도 있으므로, 아래 명령어로 확인해보자.

1
$ pip --version
cs


나의 경우에는 이미 설치가 되어 있으므로, 아래처럼 버전 정보가 나온다.

1
2
bs@ubuntu:~$ pip --version
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
cs


설치는 아래와 같은 명령어로 진행하자.

1
$ sudo apt-get install python-pip

cs


이제, cpplint를 설치하기 위한 준비는 모두 끝났다.


 # Cpplint를 설치하자.


위에서 cpplint를 설치하기 위해서는 pip가 필요하다고 하였는데, 아래와 같은 명령어로 설치를 진행하자.

1
$ pip install cpplint
cs


설치가 다 되었다면 실행을 해야 할텐데, 실행 역시나 간단하다.

1
$ cpplint [OPTIONS] files
cs


이제, 설치가 모두 끝났다. cpplint의 옵션 명령어를 확인하고자 한다면, help로 확인하면 된다.

1
$ cpplint --help
cs


 # Cpplint를 실행해보자. 


실제로 Cpplint를 실행해보자.


cpplint를 테스트 해보기 위해서 간단한 코드로 테스트 해보았다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> vt;
 
    vt.push_back(1);
    vt.push_back(2 );
    vt.push_back(3);
    vt.push_back(4);
    vt.push_back(5);
 
    std::vector<int>::iterator iter;
    for (iter = vt.begin(); iter != vt.end() ; iter++)
        std::cout << "vector : " << *iter << std::endl;
 
    iter = vt.begin();
    std::cout << iter[0<< std::endl;
    std::cout << iter[1<< std::endl;
    std::cout << iter[2<< std::endl;
    std::cout << iter[3<< std::endl;
    std::cout << iter[4<< std::endl// 주석처리.
 
    iter += 2;
    std::cout << *iter << std::endl;
 
    return 0;
}
 
csc


위의 코드로 cpplint를 실행한 결과는 아래와 같다.

1
2
3
4
5
6
7
8
bs@ubuntu:Vector$ cpplint ExVector.cpp 
ExVector.cpp:0:  No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>"  [legal/copyright] [5]
ExVector.cpp:5:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
ExVector.cpp:9:  Extra space before )  [whitespace/parens] [2]
ExVector.cpp:16:  Tab found; better to use spaces  [whitespace/tab] [1]
ExVector.cpp:23:  At least two spaces is best between code and comments  [whitespace/comments] [2]
Done processing ExVector.cpp
Total errors found: 5
cs


이처럼, 공백 및 CPP 코딩 스타일의 사소한 것들도 잡아내는 것들을 확인할 수 있다.


하지만, 이 중에서 굳이 고치고 싶지 않은 경우도 있을 것이다.


이럴 경우에는 주석처리를 하면 된다.


1
// CPPLINT

cs


이처럼 주석 처리를 // CPPLINT로 한다면, cpplint를 실행할 때에 따로 문법에 관련해서 문제가 되지 않는다.


코딩 스타일을 지키기 원한다면, 위에 구글에서 제공하는 cpplint를 사용하면 어떨까 싶다..

댓글