0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CubeIDE(CubeMX)でmain.cppを生成する

Last updated at Posted at 2024-01-09

はじめに

初投稿です。CubeIDEのコード生成機能で「main.c」ではなく「main.cpp」を生成する方法を解説します。

CubeIDEではmain.cが生成される

CubeIDEでは生成されたmain.cをmain.cppにリネームするとC++ファイルとして扱うことができます。しかし、コード生成機能はmain.cppを認識してくれません。したがって、以下のような手順が必要になります。

  • main.cppをmain.cにリネーム
  • コード生成
  • main.cをmain.cppにリネーム

自動でファイル名を変更する

CubeIDEにはコード生成の開始前や終了後に任意のファイルを実行する機能があります。コード生成開始前にmain.cppをmain.cにリネームするプログラム、終了後にmain.cをmain.cppを生成するプログラムを登録します。

before.py
#!/usr/bin/python3
import os

os.chdir(os.path.dirname(os.path.abspath(__file__)))
if os.path.exists("./../Core/Src/main.cpp"):
	os.rename("./../Core/Src/main.cpp", "./../Core/Src/main.c")
after.py
#!/usr/bin/python3
import os

os.chdir(os.path.dirname(os.path.abspath(__file__)))
os.rename("./../Core/Src/main.c", "./../Core/Src/main.cpp")

image.png

ファイル構造はこんな感じです。

image.png

ProjectManagerのCode Generatorタブに「User Action」という項目があります。ここに今作ったファイルの絶対パスを入力します。

今回はPythonで書きましたが、他の言語でも良いと思います。実行場所が実行ファイルの場所ではなく、CubeIDEのインストールパスになるので注意が必要です。

おわりに

少し無理矢理な方法ですが、main.cppを生成できるようになりました。

最後まで読んでいただきありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?