LoginSignup
0

More than 5 years have passed since last update.

gcc > マクロ定義時のみmain()を含める + コンパイル時に-Dにてマクロを指定

Posted at
動作環境
Ubuntu 16.04.3 LTS desktop amd64
tmux 2.1-3build1
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609

処理概要

  • Linuxと組込み環境で同じソースを使う
  • Linux上ではmain()を含め、組込みでは含めない

gccの-Dオプション

コード例

withMain_180830.c
#include <stdio.h>

void testFunc(void)
{

}

#ifdef DEBUG_WITH_MAIN
int main(void)
{
    printf("hello\n");
}
#endif

Linuxでmain()付きでコンパイルするには

$ gcc -DDEBUG_WITH_MAIN withMain_180830.c 

組込みではDEBUG_WITH_MAINマクロを指定せずに使う。

備考

他にも方法はあるかもしれないが、一つの方法として。

他には__GNUC____LINUX__を使う方法も考えたが、組込みの環境は将来GNUCと関わったり、Linuxで使うことになる可能性も残せるように、上記のようにしている。

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