接續之前的
寫C就先不追求相容性了
一切都是從Hello World開始
在CentOS 7上使用Visual Studio Code開發標準C語言程式
在Windows上使用Visual Studio Code開發標準C語言程式
多方試錯,發現不同平臺的編譯設定有很多不同,終於讓同一份C語言專案,能在不同平臺編譯和執行
平臺:
Windows:使用MinGW
gcc --version: 12.2.0
g++ --version: 12.2.0
CentOS 7
gcc --version: 4.8.5
g++ --version: 4.8.5
AIX 7
gcc --version: 4.8.5
g++ --version: 4.8.5
編譯設定:
Windows:同之前寫的,不再贅述
CentOS 7 (Linux)
"-std=c++11"以上,使用近代的headers和語法,開發上會方便許多
"-pthread",才能#include <thread>,用近代語法開發多執行緒程式
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++11",
"-pthread",
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
AIX 7 (Unix)
系統較特殊,目前接觸到的環境沒桌面,沒IDE,只能下指令,像是只用筆記本在寫程式
編譯指令:
g++ -std=c++11 -maix64 -pthread -Wall -ggdb -O2 -MMD -MP ${file} -o ${fileDirname}/${fileBasenameNoExtension}
"-maix64",編譯成64位元程式
"-pthread",才能#include <thread>,用近代語法開發多執行緒程式
"-Wall",呈現編譯過程,針對程式碼提示警告或錯誤
"-ggdb",程式執行中如果不幸當掉,會產出core檔,能用gdb追蹤為何當掉
"-O2",編譯最佳化,優化程式執行效能
"-MMD -MP",產生.d檔,紀錄程式碼編譯順序
另外,當有複雜的功能要實現時,可能要配合不同平臺撰寫不同的程式碼,
要在同一份專案上管理不同平臺的程式碼,通常是用define做區隔和管理,
查詢編譯器支援哪些define,參考網頁:
指令:
"echo | gcc -dM -E -"
"echo | g++ -dM -E -"