C言語 共通ヘッダー作成に関する事

田中 孝征 0 評価のポイント
2024-06-15T02:44:42.27+00:00

共通ヘッダー作成に関する質問

共通ヘッダー<getputch.h>

#ifndef __GETPUTCH
  
  #define __GETPUTCH

/* MS-Windows系 */
  #if defined(_MSC_VER) || (__TURBOC__) || (LSI_C) 

    #include <conio.h>

    #static void init_getputch(void) {}
    #static void term_getputch(void) {}

/* UNIX/ Linux/ macOS */
#else

   #include <curses.h>
   #include <stdio.h>
   #include <string.h>

   #undef getch
   #undef putchar
   #undef puts
   #undef printf
   #undef scanf

   static void init_getputch(void)
   {
       initscr();
       refresh();
   }      

   static void term_getputch(void)
   {
       endwin();
   }

   static int putch(int ch)
   {
       int result = addch(ch) == OK ? ch : EOF;
       refresh();
       return result;
   }

   static int __putchar(int ch)
   {
       return putch(ch);
   }

   static int __printf(const char *format, ...)
   {
       int count;
       va_list ap;
       static char __buf[4096];

       va_start(ap, format);
       vsprintf(__buf, format, ap);
       va_end(ap);

       count = printw("%s", __buf) == OK ? strlen(__buf) : EOF;
       refresh();
       return count; 
   }

   static int __puts(const char *s)
   {
       int count = printw("%s\n", s) == OK ? strlen(s) + 1 : EOF;
       refresh();
       return count;
   }

   static int __getch(void)
   {
       int ch;
       cbreak();    noecho();
       ch = getch();
       nocbreak();  echo();
       return ch;
   }

   #define getch   __getch
   #define putchar __putchar
   #define printf  __printf
   #define puts    __puts
   #define scanf   __scanw

  #endif

#endif

<動作環境>
  OS        : Windows 10
  エディタ  : Visual Studio Code 1.89.1
    コンパイラ: gcc

このソースコードは、「学習本」の抜粋です。
本の説明では、
_MSC_VER、__TURBOC__、LSI_Cは、それぞれVisual C++、Borlan C++(Turbo C++)、
LSI Cの処理系で、処理系識別のための独自に定義されているマクロです。
 上記以外のMS-Windows用の処理系をお使いであれば、その処理系で独自に定義されるマクロを追加する必要があります。
との事です。

マクロ定義が不適切な為、コンパイルすると<curses.h>が見つからない旨の
コンパイルエラーが発生します。

適切なマクロを知りたいです。
今後の学習に影響します。

宜しくお願いします。
    
                              以上
                                              


  


Windows 10
Windows 10
パーソナル コンピューターとタブレットで実行される Microsoft オペレーティング システム。
85 件の質問
Microsoft Q&A
Microsoft Q&A
このタグを使用して、提案、機能要求、バグを Microsoft Q&A チームと共有します。 Microsoft Q&A チームは、定期的にお客様のフィードバックを評価し、その過程で更新プログラムを提供します。
99 件の質問
{count} 件の投票

1 件の回答

並べ替え方法: 最も役に立つ
  1. gekka 8,061 評価のポイント MVP
    2024-06-16T16:00:09.07+00:00

    tasks.jsonのビルドタスクのgccへ渡す引数にincludeするヘッダ類のある場所のパスとリンクの指定をしてください。

    {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: g++.exe アクティブなファイルのビルド",
                "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
                "args": [
                    "-fdiagnostics-color=always",
                    "-g",
                    "${file}",
    
    "-I","C:\\msys64\\ucrt64\\include\\ncurses",
    "-l","ncurses",
    "-D","NCURSES_STATIC",
    
                    "-o",
                    "${fileDirname}\\${fileBasenameNoExtension}.exe",
                ],
                "options": {
                    "cwd": "C:\\msys64\\ucrt64\\bin"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "デバッガーによって生成されたタスク。"
            },
        ],
        "version": "2.0.0"
    }
    
    0 件のコメント コメントはありません