Giter Site home page Giter Site logo

c_1's Introduction

m1 macのnvimでcの開発環境を作る

概要

  • コンパイルに make, clangを使う
  • ユニットテストに、cpputestを使う
  • nvimの開発環境設定に, coc-clangd, clang-format, compiledbを使う

coc-clangdの設定

  • nvimにcocをインストールしている前提
  • coc-clangdをインストールする
    • :CocInstall coc-clangd
  • compile_commands.jsonが必要で、compiledbで作れる

coc-clangdで自動フォーマットさせる

ファイル保存時の自動フォーマット

  • nvimのコマンドで、:CocConfig でcocの設定画面を開いて、下記を追加する。
"coc.preferences.formatOnSaveFiletypes": ["c"],

clang-formatでフォーマット設定する

  • clang-formatをインストール
brew install clang-format
  • .clang-formatというファイルをプロジェクトルートに配置
  • 中身を下記のような感じにする
BasedOnStyle:  Google
ColumnLimit: 0
IndentWidth: 2

Language: Cpp
Standard: Auto
UseTab: Never
  • 上記設定が自動で読み込まれるので、ファイル保存時に上記設定でフォーマットされる。

compile_commnads.json作成

  • compiledbをインストール
pip3 install compiledb
  • makeを使う場合
compiledb make
  • 上記でmakeと同時に、makeの設定内容に合わせてcompile_commnads.jsonが作成される

ユニットテストでCppUTestを使う

  • Install
brew install cpputest
  • cpputestのパスを環境変数にセット
export CPPUTEST_HOME="$(brew --prefix cpputest)"
  • makeの設定例
CPPFLAGS += -I$(CPPUTEST_HOME)/include
LDFLAGS = -L$(CPPUTEST_HOME)/lib -lCppUTest -lCppUTestExt

../bin/test/test: test.cpp  
	clang++ -o $@ $(CPPFLAGS) $(LDFLAGS) $^ 
  • -I でヘッダファイルを探すフォルダを追加できる。
  • -I$(CPPUTEST_HOME)/includeによって、cpputestのヘッダファイルを追加している。
  • -L$(CPPUTEST_HOME)/libで、ライブラリのあるフォルダを追加できる。
  • -lCppUTestは、libCppUTestをリンクするってこと。
  • ヘッダファイルの読み込みがある部分しかリンクされないらしい。
  • だからライブラリ読み込む前にヘッダファイルの読み込みしているコードを先に持ってこないといけないらしい。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.