MacOS在vscode中调用curses.h头文件报错的解决办法

这是在C++实习过程遇到的一个小问题
我尝试在vscode中配置使用curse.h头文件,却发生报错,报错内容为:

Undefined symbols for architecture arm64:
    "_endwin", referenced from:
readChar() in Editor-c9114c.o
    "_initscr", referenced from:
readChar() in Editor-c9114c.o
    "_stdscr", referenced from:
readChar() in Editor-c9114c.o
    "_wgetch", referenced from:
readChar() in Editor-c9114c.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

呃,似乎是电脑中并没有安装curses的样子,那么首先在终端中输入

brew install ncurses

使用brew安装ncurses。
之后发现依然报错且报错内容相同。
于是查找原因,排除众多原因之后发现可能是虽然下载了ncurses,是vscode并不知道去调用他,因此需要在vscode的setting.json中对code runner的编译参数进行修改,让程序在编译的时候会主动去指定curses库。
于是使用shift + command + P呼出命令面板Command Palette,接着输入setting.json,

找到“打开默认设置”的选项,在其中找到code runner的编译参数的位置。

但是此时发现问题,想要修改参数却显示“无法在只读编辑器中编辑”,

这是因为defaultSettings.json文件是vscode的默认文件,一般不让修改,但是我们可以将想要修改的地方放到个人设置中。

具体方法是:将在defalut setting.json中想要修改的内容复制下来,打开搜索setting.json时跳出来的另外一个选项“打开设置”,将代码粘贴进去,并进行修改。
注意:如果上一个语句结尾没有逗号,一定要记得加上逗号。

那么此处要进行的修改是在cpp的编译参数后加上

-l ncurses

顺带一提,想要使用c++11的新特性,可以将编译参数修改为:

"cpp":"cd $dir && g++ $fileName -o $fileNameWithoutExt -std=c++11 && $dir$fileNameWithoutExt"

因此在添加了ncurses后的编译参数为:

"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -std=c++11 -l ncurses && $dir$fileNameWithoutExt"


接着保存setting.json,就能愉快使用curses.h啦~