CPATH

作者: 欧阳_z | 来源:发表于2020-10-14 23:31 被阅读0次

    1、简介
    C_INCLUDE_PATHCPLUS_INCLUDE_PATHCPATH 环境变量用于预处理 C/C++ 时的,添加 include 目录。
    其中 C_INCLUDE_PATH 只对预处理 C语言 有效,CPLUS_INCLUDE_PATH 只对预处理 C++ 有效,CPATH 对两者都有效。

    2、C语言测试代码:

    $ cat sub/fun.c 
    #include <stdio.h>
    void fun(void)
    {
        printf("%s %d \n", __FUNCTION__, __LINE__);
    }
    $ cat sub/fun.h 
    extern void fun(void);
    $ cat test.c 
    #include "fun.h"
    int main(void)
    {
        fun();
        return 0;
    }
    

    不设置环境变量时的编译报错:

    $ gcc test.c sub/fun.c
    test.c:1:10: fatal error: fun.h: No such file or directory
     #include "fun.h"
              ^~~~~~~
    compilation terminated.
    

    CPATH 对 C语言 有效:

    $ export CPATH=$(pwd)/sub
    $ gcc test.c sub/fun.c
    $ unset CPATH
    

    C_INCLUDE_PATH 对 C语言 有效:

    $ export C_INCLUDE_PATH=$(pwd)/sub
    $ gcc test.c sub/fun.c
    $ unset C_INCLUDE_PATH
    

    CPLUS_INCLUDE_PATH 对 C语言 无效:

    $ export CPLUS_INCLUDE_PATH=$(pwd)/sub
    $ gcc test.c sub/fun.c
    test.c:1:10: fatal error: fun.h: No such file or directory
     #include "fun.h"
              ^~~~~~~
    compilation terminated.
    $ unset CPLUS_INCLUDE_PATH
    

    3、重命名文件,修改为 C++ 的测试代码:

    $ mv test.c test.cpp
    $ mv sub/fun.c sub/fun.cpp
    

    不设置环境变量时的编译报错:

    $ g++ test.cpp sub/fun.cpp
    test.cpp:1:10: fatal error: fun.h: No such file or directory
     #include "fun.h"
              ^~~~~~~
    compilation terminated.
    

    CPATH 对 C++ 有效:

    $ export CPATH=$(pwd)/sub
    $ g++ test.cpp sub/fun.cpp
    $ unset CPATH
    

    C_INCLUDE_PATH 对 C++ 无效:

    $ export C_INCLUDE_PATH=$(pwd)/sub
    $ g++ test.cpp sub/fun.cpp
    test.cpp:1:10: fatal error: fun.h: No such file or directory
     #include "fun.h"
              ^~~~~~~
    compilation terminated.
    $ unset C_INCLUDE_PATH
    

    CPLUS_INCLUDE_PATH 对 C++ 有效:

    $ export CPLUS_INCLUDE_PATH=$(pwd)/sub
    $ g++ test.cpp sub/fun.cpp
    $ unset CPLUS_INCLUDE_PATH
    

    相关文章

      网友评论

          本文标题:CPATH

          本文链接:https://www.haomeiwen.com/subject/aswgpktx.html