查看C++ 預(yù)定義宏的方法
更新時間:2026年05月21日 09:08:36 作者:java葉新東
本文主要介紹了查看C++ 預(yù)定義宏的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
什么是預(yù)宏定義
預(yù)定義宏是C語言中標準編譯器預(yù)先定義的宏,當我們編寫跨平臺程序時,有時候需要知道對應(yīng)平臺的工具鏈GCC預(yù)定義宏,用來判斷一些平臺特性
預(yù)定義宏特征
預(yù)定義宏有兩個特征:
- 無需提供它們的定義,就可以直接使用。
- 預(yù)定義宏沒有參數(shù),且不可被重定義。
預(yù)定義的宏一般分為兩類:標準預(yù)定義宏、編譯器預(yù)定義宏。
常用的幾個標準預(yù)定義宏有以下幾個:
std::cout <<"當前行號:"<<__LINE__ << std::endl; // 當前行號:15
std::cout <<"當前文件:"<<__FILE__ << std::endl; // 當前文件:/Users/yexd/Documents/project_cpp/cpp_learn/lesson_26__LINE__.cpp
std::cout <<"當前日期:"<<__DATE__ << std::endl; // 當前日期:Apr 16 2023
std::cout <<"當前時間:"<<__TIME__ << std::endl; // 當前時間:10:18:11
std::cout <<"當前函數(shù):"<<__FUNCTION__ << std::endl; // 當前函數(shù):main
//__STDC__:判斷當前的編譯器是否為標準C編譯器,若是則返回值1
std::cout <<"當前編譯器:"<<__STDC__ << std::endl; // 當前編譯器:1
常見操作系統(tǒng)預(yù)定義宏
| OS | Macro | Description |
|---|---|---|
| UNIX Environment __ | unix__ | |
| UNIX Environment | __unix | |
| Linux kernel | linux | |
| GNU/Linux | gnu_linux | |
| Mac OS X & iOS | APPLE | 蘋果系統(tǒng) |
| Android | ANDROID | 安卓系統(tǒng) |
| Windows | _WIN32 | Defined for both 32-bit and 64-bit environments |
| Windows | _WIN64 | Defined for 64-bit environments |
gcc 查看gcc默認的內(nèi)置宏定義
gcc -dM -E - < /dev/null 或 cpp -dM /dev/null
指令說明
- -E 預(yù)處理后即停止,不進行編譯。預(yù)處理后的代碼送往標準輸出。GCC忽略任何不需要預(yù)處理的輸入文件。
- -dM 告訴預(yù)處理器輸出有效的宏定義列表(預(yù)處理結(jié)束時仍然有效的宏定義)。該選項需結(jié)合`-E’選項使用。
結(jié)果如下
yexd@yexddeMacBook-Pro lesson_27_namespace % gcc -dM -E - < /dev/null #define _LP64 1 #define __APPLE_CC__ 6000 #define __APPLE__ 1 #define __ATOMIC_ACQUIRE 2 #define __ATOMIC_ACQ_REL 4 #define __ATOMIC_CONSUME 1 ......
clang 查看所有預(yù)定義宏
clang -dM -E -x c /dev/null
結(jié)果如下
yexd@yexddeMacBook-Pro lesson_27_namespace % clang -dM -E -x c /dev/null #define _LP64 1 #define __APPLE_CC__ 6000 #define __APPLE__ 1 #define __ATOMIC_ACQUIRE 2 #define __ATOMIC_ACQ_REL 4 #define __ATOMIC_CONSUME 1 ......
查看用戶自行設(shè)定的宏定義
gcc -dM -E helloworld.c
預(yù)宏定義在c++代碼中的應(yīng)用
#include "iostream"
using namespace std;
#if defined(_MSC_VER) // 如果是windows系統(tǒng),引入direct.h,將 _getcwd 宏定義為 GetCurrentDir
#include <direct.h>
#define GetCurrentDir _getcwd
#elif defined(__unix__) || defined(__APPLE__) // 如果是unix系統(tǒng)或者蘋果系統(tǒng),引入unistd.h ,將 getcwd 宏定義為 GetCurrentDir
#include <unistd.h>
#define GetCurrentDir getcwd
#else
// 啥也不干
#endif
// 操作系統(tǒng)兼容,在不同的操作系統(tǒng)中獲取當前文件目錄的方法
std::string get_current_directory()
{
char buff[250];
// 如果是windows系統(tǒng),就用 _getcwd,如果是mac系統(tǒng)就用 getcwd
GetCurrentDir(buff, 250);
string current_working_directory(buff);
return current_working_directory;
}
int main(int argc, char* argv[])
{
std::cout << "當前工作目錄為: " << get_current_directory() << endl;
return 0;
}
到此這篇關(guān)于查看C++ 預(yù)定義宏的方法的文章就介紹到這了,更多相關(guān)C++ 預(yù)定義宏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
VSCode配置C/C++語言環(huán)境(2023最新版)
這篇文章主要介紹了VSCode配置C/C++語言環(huán)境(2023最新版)的全過程,本文給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11

