转载自 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. LinkedList源代码深入剖析

    第1部分 LinkedList介绍LinkedList简介 public class LinkedList<E> extends AbstractSequentialList<E&g ...

  2. 一步一步制作yaffs/yaffs2根文件系统(四)---构建etc、dev等剩余目录

    开发环境:Ubuntu 12.04 开发板:mini2440  256M NandFlash   64M SDRAM 交叉编译器:arm-linux-gcc 4.4.3点此可下载 BusyBox版本: ...

  3. DSP6455 DSP/BIOS中断配置问题(是否需要ECM-事件组合以及实例)

    2013-06-20 21:08:48 中断的配置有两种常用的方式: 一是通过CSL提供的API进行配置,这种方法相对DSP/BIOS偏底层,也比较麻烦:这种方法要求对中断系统的工作方式很清楚. 二是 ...

  4. poj 2531 Network Saboteur( dfs )

    题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...

  5. Asp.net调用百度搜索引擎

    ASP.NET 调用百度搜索引擎 百度搜索引擎提供了一段嵌入到页面中的代码 <form action="http://www.baidu.com/baidu" target= ...

  6. android LayoutInflater和inflate()方法的用法(转载)

    原文出处:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html 在实际开发中LayoutInflater这个类还是非常有用的,它的作用 ...

  7. 多线程程序设计学习(13)Active Object pattern

    Active Object[接收异步消息的对象] 一:Active Object的参与者--->客户端线程(发起某种操作请求处理)--->代理角色(工头)--->实际执行者(工人)- ...

  8. ExecuteStoreQuery

    using (var webdb = new kyj_NewHouseDBEntities()) { string sql = "select * from developer where ...

  9. css选择器,有箭头与没箭头的区别

    div > span 和 div span 的区别 ,即有箭头和没箭头的区别 div > span span 是 div 的下一层级关系 在这种情况下找得到span元素: <div& ...

  10. Spring配置bean的详细知识

    在Spring中配置bean的一些细节.具体信息请参考下面的代码及注释 applicationContext.xml文件 <?xml version="1.0" encodi ...