注意:feof判断文件结束是通过读取函数fread/fscanf等返回错误来识别的,故而判断文件是否结束应该是在读取函数之后进行判断。比如,在while循环读取...
COO to CSR format #include <vector> #include <iostream> #include <mkl.h> #ifdef __linux__ #include <stdlib.h std::vector<T> &vec, const int mem_alignment) { size_t num_bytes = vec.size() * sizeof(T); #ifdef __linux__ float *x = (float *)aligned_alloc(mem_align, num_bytes_x); #else float *x = ( // delete matrices mkl_sparse_destroy(coo_mtx); mkl_sparse_destroy(csr_mtx); #ifdef __linux__ free(rows_aligned); free(cols_aligned); free(vals_aligned); free(b_aligned
热卖云产品新年特惠,2核2G轻量应用服务器9元/月起,更多上云必备产品助力您轻松上云
#ifdef 和 #ifndef常用于解决头文件重复包含的问题。 #ifdef的使用 #ifdef的使用和#if defined()的用法一致 #ifndef又和#if !
zhangchiytu/article/details/7563329 先看个例子: #define TARGET_LITTLE_ENDINA 1 #define TARGET_BIG_ENDINA 0 #ifdef #ifdef的使用 #if后面接的是一个宏 #ifdef (x) ...code... #endif 这个#ifdef 它不管里面的“x”的逻辑是“真”还是“假”,它只管这个程序前面的宏定义里面有没有定义“x”这个宏(即在这里#define x 1和#define x 0等效,都等同于 #if defined的使用 #if defined() 的使用和#ifdef的用法一致 #if !defined()又和 #ifndef 的用法一致。 第二:别忘了#endif 明白了之间的区别问题就变得非常的容易,修改上述代码只要把其中的#ifdef改成#if就可以达到目的。
编译器 GCC #ifdef __GNUC__ #if __GNUC__ >= 3 // GCC3.0以上 Visual C++ #ifdef _MSC_VER #if _MSC_VER >=1000 __BORLANDC__ Cygwin #ifdef __CYGWIN__ #ifdef __CYGWIN32__ MinGW #ifdef __MINGW32__ 操作系统 Windows #ifdef _WIN32 //32bit #ifdef _WIN64 //64bit #ifdef _WINDOWS //图形界面程序 #ifdef _CONSOLE //控制台程序 Windows(95/98/ __unix //or #ifdef __unix__ Linux #ifdef __linux //or #ifdef __linux__ FreeBSD #ifdef __FreeBSD__ NetBSD #ifdef __NetBSD__
__LINUX__ //换个格式 sprintf( szTemp, "[%s][%d][%s][%s]\n", file, line, func, expr ) ; #else sprintf( ", file, line, func, expr ) ; #endif __show__(szTemp) ; } VOID __show__( const CHAR* szTemp ) { #ifdef __LINUX__ printf("Assert:%s",szTemp); #endif //保存日志 #ifndef GAME_CLIENT FILE* f = fopen( ". () ; INT iRet = ::MessageBoxA( NULL, szTemp, "异常", MB_OK ) ; lock.Unlock() ; } #elif defined(__LINUX __) #endif #ifdef GAME_CLIENT throw(std::string(szTemp)); #else throw(1); #endif } 最终会在__show__中
GCC #ifdef __GNUC__ #if __GNUC__ >= 3 // GCC3.0以上 Visual C++ #ifdef _MSC_VER #if _MSC_VER >=1000 // VC __BORLANDC__ Cygwin #ifdef __CYGWIN__ #ifdef __CYGWIN32__ // MinGW #ifdef __MINGW32__ 操作系统 Windows # ifdef _WIN32 //32bit #ifdef _WIN64 //64bit #ifdef _WINDOWS //图形界面程序 #ifdef _CONSOLE //控制台程序 //Windows __unix //or #ifdef __unix__ Linux #ifdef __linux //or #ifdef __linux__ FreeBSD #ifdef __FreeBSD__ NetBSD #ifdef __NetBSD__
根据不同情况编译不同代码、产生不同目标文件的机制,称为条件编译 有这些预处理命令:#if、#elif、#else #endif ;#ifdef #else #endif PHP源码: #ifdef SERIALIZE_HEADERS smart_str_free(&vec_str) //宏定义VEC_FREE() #else //如果不存在 # define VEC_FREE() do {} while (0) //宏定义空操作 #endif #ifdef //宏定义空操作 #endif 练习: #if _WIN32 printf("windows系统 \n"); #elif __linux__ printf("linux系统 \n"); #else printf("其他系统 \n"); #endif #ifdef N
#if #ifdef和#ifndef用法移位运算符的优先级高于条件运算符,重载是不能改变运算符优先级的,这点要注意,所以代码应当像下面这样调整,写宏的时候一定要注意优先级,尽量用括号来屏蔽运算符优先级。 x:y int main() { int a=10,b=20; #ifdef MAX printf("40:The larger one is %dn",MAXIMUM(a,b)); 和#ifndef区别#ifdef 标识符程序段1#else程序段2#endif它的作用是:当标识符已经被定义过(一般是用#define命令定义),则对程序段1进行编译,否则编译程序段2。 其中#else部分也可以没有,即:#ifdef程序段1#denif这里的“程序段”可以是语句组,也可以是命令行。这种条件编译可以提高C源程序的通用性。 有时也采用下面的形式:#ifndef 标识符程序段1#else程序段2#endif只是第一行与第一种形式不同:将“ifdef”改为“ifndef”。
再完善下代码: #include "stdio.h" int main() { if(1 == 1) { printf("Hello, If\n"); #ifdef 差别就是多了中间那段#ifdef.... stdio.h" #define HEHE 123 int main() { if(1 == 1) { printf("Hello, If\n"); #ifdef
) || defined(__NT__) //define something for Windows (32-bit and 64-bit, this part is common) #ifdef // Other kinds of Mac OS #else # error "Unknown Apple platform" #endif #elif __linux
#ifdef 就是”if define”的意思,即”如果定义了” #ifdef 标识符1 程序段1 #else 程序段2 #endif 如果定义过标识符1,那就执行程序段1,负责执行程序段2.也可以有如下用法 : #ifdef 标识符1 程序段1 #endif #ifndef “if not define”,如果没有定义过. ; } #ifdef CONFIG_DEBUG printf("open test.txt ok"); #endif return 0; } 当不想让程序执行输出语句时,把#define
. // Enabled #ifdef SPDLOG_DEBUG_ON or #ifdef SPDLOG_TRACE_ON // SPDLOG_TRACE(console, “Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}”, 1, 3.23); SPDLOG_DEBUG(console, “Enabled only #ifdef SPDLOG_DEBUG_ON ; // // syslog example. linux only.. // #ifdef __linux__ std::string ident = “spdlog-example”; auto syslog_logger
#ifdef宏名: #include<iostream> #include<conio.h> using namespace std; #define dhy //定义宏名 int main() { #ifdef dhy //如果宏名定义了执行ifdef代码 cout << "你好,世界" << endl; #else //未定义宏名则会执行else后面的代码 cout << "再见,世界" << endl; #endif //dhy _getch(); return 0; } #ifndef宏名:与ifdef宏名类似且相反 #include<iostream> #include<conio.h
makefile这样写: ifdef DEBUG CFLAGS += -DDEBUG=1 -O0 -ggdb RELMODE = debug else CFLAGS += -DDEBUG=0 - O0 -s RELMODE = release endif 编译可以这样: make DEBUG=1 #可以用命令行传递变量 RELEASE = abc #ifdef 变量名称不能加$() ifdef RELEASE $(warning RELEASE defined) else $(warning RELEASE not defined) endif #ifeq 后面参数要叫 比如,你在代码里面需要定义一个宏DEBUG来打开调试开关,代码如下:int main(){int i=9;#ifdef DEBUGi=1;#elsei=0;#endifprintf("i=%d\n",
条件编译指令: #if 当条件为真,则编译这段代码 #ifdef(if define) 当宏被定义,则编译这段代码 #ifndef(if no define) 当宏未被定义,则编译这段代码 #elif( else-if) 若前面条件不满足,则执行#elif条件判断 #else 若前面条件不满足,则编译这段代码 #endif 条件编译指令结束标志 例如: #ifdef VERSIOIN_2 // 版本2代码 #endif // 判断手机系统版本 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0 #endif // 规定只能在ios系统下运行 #ifdef 参考: iOS 预编译指令#if #ifdef #elif #else #endif #import #define的简单使用
1、#if 和#ifdef 当asd_eee表达式存在而且,值为ture的时候接续向下执行 例如 #define TARGET_LITTLE_ENDINA 1 #define TARGET_BIG_ENDINA 0 #ifdef TARGET_LITTLE_ENDINA call little endina function #else call big endina function #endif 上面的今天写的代码 经过分析和查找相关的文档,原来是自己没有理解#if和#ifdef之间的区别。 对于#if需要是一个表达式,如果表达式为1则调用#if下面的代码。 对于#ifdef需要的只是这个值有没有定义,并不关心这个值是什么。 表明之间的区别问题就变得非常的容易,只要把上述代码中的#ifdef改成#if就可以达到目的。
WIN32) || defined(__WIN32__) #define MY_WIN32 #elif defined(linux) || defined(__linux) || defined(__linux __) #define MY_LINUX #endif #if defined(MY_WIN32) #ifdef MY_API_STATIC #ifdef __cplusplus #define MY_API extern "C" #else #define MY_API #endif #else #ifdef MY_API_EXPORTS #ifdef __cplusplus #define MY_API extern "C" __declspec(dllexport #else #define MY_API __declspec(dllexport) #endif #else #ifdef
这里举 3 个例子,在代码中经常看到的关于条件编译: 示例1:用来区分 C 和 C++ 代码 #ifdef __cplusplus extern "C" { #endif void hello hello 就不会被 g++ 编译器改写,因此就可以被 C 代码来调用; 示例2:用来区分不同的平台 #if defined(linux) || defined(__linux) || defined(__linux defined(_WIN32) Sleep(1000 * 1000); // 调用 Windows 平台下的库函数(第一个字母是大写) #endif 那么,这些 linux, __linux, __linux 示例3:在编写 Windows 平台下的动态库时,声明导出和导入函数 #if defined(linux) || defined(__linux) || defined(__linux__) # define LIBA_API #else #ifdef LIBA_STATIC #define LIBA_API #else #ifdef LIBA_API_EXPORTS
条件编译的几种格式 格式 1 #ifdef 标识符 程序段代码1 #else 程序段代码2 #endif 作用:当标识符被定义过,则对程序段代码 1 进行编译,否则对程序段 2 进行编译。 平时,在进行程序调试过程中,需要输出一些信息方便调试,在调试结束后,不需要这些信息输出,我们可以这样处理: #define DEBUG //然后在代码中需要输出调试信息的地方,写一些输出信息,例如: #ifdef 例如; if __Linux__ //Linux专有函数代码 #elif _Win32 //windows专有函数代码 #else //其他平台专有函数代码 #endif 2 头文件防卫式声明
扫码关注腾讯云开发者
领取腾讯云代金券