转载自 http://bbs.csdn.net/topics/390381450

#include_next仅用于特殊的场合. 它被用于头文件中(#include既可用于头文件中, 又可用于.c文件中)来包含其他的头文件

意思就是include下一个(除本文件)文件名为 filename.h 的头文件

作用是这样的,就是你想用自己的函数代替其他库函数,但是
1. 不想修改源代码,
2. 不能修改原来的头文件
这是就可以用#include_next了。
下面的例子用在不改变源代码和头文件的情况下,实现了记录malloc函数调用情况。

 
C/C++ code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * @file        stdlib.h
 * @author      mymtom
 */
#ifndef STDLIB_H
#define STDLIB_H
 
#include_next <stdlib.h>
 
#include <stdio.h>
 
 
static void * my_malloc(const char *file, int line, const char *func, size_t size)
{
        void *ptr;
        ptr = malloc(size);
 
        /* 记录调用函数名称和返回值 */
        printf("位于文件%s第%d行的函数%s调用了malloc, size=%d, 返回值为%p\n", file, line, func, size, ptr);
 
        return ptr;
}
 
#define malloc(size) my_malloc(__FILE__, __LINE__, __FUNCTION__, size)
 
#endif /* STDLIB_H */
C/C++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * @file        main.c
 * @author      mymtom
 */
 
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
        malloc(80);
 
        return 0;
}

注意自己的stdlib.h位于当前目录下,所以要加上-I.
$ gcc -I. main.c -o main
$ ./main
位于文件main.c第11行的函数main调用了malloc, size=80, 返回值为0x28201060

【转】#include_next <filename.h>的更多相关文章

  1. #include<filename.h> 与 #include“filename.h”

    #include<filename.h>:从标准库路径去寻找该文件,对于VC来说,应该还包括VC环境设置选项中的包含目录以及工程属性中指定的目录. #include“filename.h” ...

  2. 用 #include “filename.h” 格式来引用非标准库的头文件

    用 #include “filename.h” 格式来引用非标准库的头文件(编译器将 从用户的工作目录开始搜索) #include <iostream> /* run this progr ...

  3. 用 #include <filename.h> 格式来引用标准库的头文件

    用 #include <filename.h> 格式来引用标准库的头文件(编译器将从 标准库目录开始搜索). #include <iostream> /* run this p ...

  4. #include <filename.h> 和 #include“filename.h” 有什么区别

    对于#include <filename.h> ,编译器从标准库路径开始搜索filename.h,对于#include “filename.h” ,编译器从用户的工作路径开始搜索filen ...

  5. mwc config.h 中文注释

    #ifndef CONFIG_H_ #define CONFIG_H_ /*************************************************************** ...

  6. mac 下 clang++ 找不到头文件 stdlib.h

    因为要用 openmp库,用 clang++ 编译 c++程序,出现了如下报错: clang++ xx.cpp -o xx -fopenmp /usr/local/Cellar/llvm/7.0.0/ ...

  7. 编译错误: file not found with angled include use quotes instead #include <lualib.h> 和 #include "lualib.h"

    http://stackoverflow.com/questions/17465902/use-of-external-c-headers-in-objective-c 问题: 7down votef ...

  8. 《Linux/Unix系统编程手册》读书笔记7 (/proc文件的简介和运用)

    <Linux/Unix系统编程手册>读书笔记 目录 第11章 这章主要讲了关于Linux和UNIX的系统资源的限制. 关于限制都存在一个最小值,这些最小值为<limits.h> ...

  9. CURL HELP

    CURL下载 在windows的系统环境变量中,将CURL的路径(curl.exe存放的路径)复制到"Path"变量的结尾 Usage: curl [options...] < ...

随机推荐

  1. 【leetcode】Permutations II (middle)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. 解析php中die(),exit(),return的区别

    die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值die是遇到错误才停止exit是直接停止,并且不运行后续代码,exit()可以显示内容.return就是纯粹的返回 ...

  3. 使用SpringAop 验证方法参数是否合法

    (原文地址:http://blog.csdn.net/is_zhoufeng/article/details/7683194) 1.依赖包    aspectjweaver.jar 其中Maven的配 ...

  4. HeadFirst设计模式之门面模式

    一. 1.The Facade Pattern provides a unifi ed interface to a set of interfaces in a subsytem. Facade d ...

  5. Linux下的动态连接库及其实现机制

    Linux与Windows的动态连接库概念相似,但是实现机制不同.它引入了GOT表和PLT表的概念,综合使用了多种重定位项,实现了"浮动代码",达到了更好的共享性能.本文对这些技术 ...

  6. 非常实用的15款开源PHP类库

    PHP库给开发者提供了一个标准接口,它帮助开发者在PHP里充分利用面向对象编程.这些库为特定类型的内置功能提供了一个标准的API,允许类可以与PHP引擎进行无缝的交互.此外,开发者使用这些类库还可以简 ...

  7. 【HDOJ】4043 FXTZ II

    1. 题目描述有n个球,第i个球的伤害值为$2^i-1, i \in [1,n]$.有甲乙两个人,每次由甲选择n个球中的一个,用它以相同概率攻击自己或者乙,同时彻底消耗这个球.这样的攻击最多进行n次. ...

  8. IOS,Object C学习过程中遇到的attributes

    @property 定义一个属性 @synthesize 告诉编译器自动为属性自动生成 getter 和setter方法 在定义属性的时候会用到如下@attributes nonatomic,告诉编译 ...

  9. sublime打开文件时自动生成并打开.dump文件

    GBK Encoding Support 没有安装前打开ASNI格式编码文件会乱码,安装成功重启则可以打开正常 关于.dump文件生成的解释: 当打开一个非utf-8格式且包含汉字的文件时,subli ...

  10. poj 1836 Alignment(dp)

    题目:http://poj.org/problem?id=1836 题意:最长上升子序列问题, 站队,求踢出最少的人数后,使得队列里的人都能看到 左边的无穷远处 或者 右边的无穷远处. 代码O(n^2 ...