1
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 5 years have passed since last update.

batファイルからmakeファイル経由で、cファイル内のプリプロ(#define XXX)の値を操作

1
Last updated at Posted at 2020-03-05

以下cファイルの例で言うと、外部からの設定(GSW_**)でY_IDの値を切り替えるというのが狙いです。
実装しようと思ったキッカケは、ROMを複数作りたくないのと、パターンの多いデバッグで役立つかな。。なんて思ったからです

■cファイル

hello.c
# include "stdio.h"

# if GSW_A==(0) && GSW_B==(0) && GSW_C ==(0)
# define Y_ID ("A0B0C0")
# elif GSW_A==(0) && GSW_B==(0) && GSW_C ==(1) 
# define Y_ID ("A0B0C1")                          /* ★今回の目標設定★ */
# elif GSW_A==(1) && GSW_B==(1) && GSW_C ==(1)
# define Y_ID ("A1B1C1")
# else 
 error"GSWの設定を確認して下さい"
# endif

int main(void) {
 printf("Y_ID:%s",Y_ID);
 while(1);
 return 0;
}

■batファイル

@echo off
@rem ++++++++++++++++Windows bat (jenkinsでも試したい) ++++++++++++++++
@rem ▼ここでGSW_*の値を設定▼
set a=0
set b=0
set c=1
@rem ▲ここでGSW_*の値を設定▲

@rem ひとまずインクルード用のファイルを生成します。-Dオプションが肝です。
echo ADDOP =-DGSW_A=%a% -DGSW_B=%b% -DGSW_C=%c% > IncludeForGSW.mk

del hello.o
del hello.exe

@rem make実行の際にインクルードファイルを読み込むので設定した値が反映される
make 
.\hello.exe
pause

■makeファイル <IncludeForGSW.mk>(batファイルにより自動生成されたもの)

ADDOP =-DGSW_A=0 -DGSW_B=0 -DGSW_C=1 

■makeファイル <GNUmakefile>

include IncludeForGSW.mk
hello: hello.o 
	gcc -Wall  -o hello hello.o 
hello.o: hello.c 
	gcc -Wall ${ADDOP}  -c hello.c

■実行結果

Y_ID:A0B0C1

もっと良い方法があれば知りたいです!よろしくお願いします!

1
0
4

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
1
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?