Tag Archives: C_INCLUDE_PATHCPLUS_INCLUDE_PATHCPATH

Linux: Environment Variables C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, CPATH Common Error [How to Solve]

C_ INCLUDE_ PATHCPLUS_ INCLUDE_ Path and cpath are often used to globally add the include directory when preprocessing C/C + +, where C + + is used as the include directory_ INCLUDE_ Path is only effective for pretreatment C, cplus_ INCLUDE_ Path is only valid for preprocessing C + +, while cpath is valid for all languages. Next, we only use C_ INCLUDE_ Path as an example

The common error prone setting method is to simply use recursive assignment in ~ /. Bashrc and other files

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/somewhere/include

The command of this statement will change C_ INCLUDE_ Path is assigned to its original value, followed by the value of :/somewhere/include

For example, if the original $C_ INCLUDE_ The value of path is /previous/include , then after executing this shell statement, $C_ INCLUDE_ The value of path will change to /previous/include/somewhere/include , and if the environment variable C has not been defined before_ INCLUDE_ Path or defined but empty, then $C_ INCLUDE_ The value of path will change to :/somewhere/include .

But why is it easy to make mistakes

We know that colon : is used to separate two directories in Linux (semicolon is commonly used in Windows)</The expected effect of using this statement should be to add /somewhere/include to the included directory when preprocessing C

There is a sentence in the official document of GCC about the influence of environment variables on GCC

In all these variables, an empty element instructs the compiler to search its current working directory. Empty elements can appear at the beginning or end of a path. For instance, if the value of CPATH is :/special/include, that has the same effect as -I. -I/special/include.

In all of these variables, an empty element instructs the compiler to search the current working directory empty elements can appear at the beginning or end of a path . For example, if the value of cpath is :/special/include , this would be equivalent to - I. - I/special/include

Therefore, in the previous example, we just want to add a /somewhere/include , the expected C, to the preprocessed search path_ INCLUDE_ The value of path is /somewhere/include , but actually C_ INCLUDE_ Path is assigned as :/somewhere/include . Although the colon at the beginning is empty, the current working directory will be added to the preprocessing search path, that is, , in Linux, and the priority is the highest in all directories

This is often not the result we want, which will lead to the change of the included directory of preprocessing with the change of the current working directory. If there is a file with the same name as the file contained in the source code in the current working directory, it will lead to unexpected errors

Unfortunately, tasks. Options. CWD happens to be /usr/bin in the tasks. JSON automatically generated by vscode for us, so once C is set incorrectly_ INCLUDE_ Path , and there is a file with the same name in /usr/bin (you should know that most of the files here are executable programs). When compiling C programs, you will mistakenly include the file with the same name as the source code file, and then you can appreciate a large number of compilation errors. For example, manjaro, the most popular Linux distribution at present, will have an executable program named array in /usr/bin after installation, which is similar to & lt; array> duplicate name

Correct method:

First of all, to be clear, these environment variables that contain the directory are not part of the Linux operating system, so in general, Linux will not set these environment variables

Therefore, when setting an environment variable for the first time, it should be directly assigned to the required directory, and then the recursive assignment should be used in the subsequent settings; Or all directories can be separated by : at one time and assigned values together; Or simply do not use these environment variables, but use the - I parameter to add the included directory at compile time

Others:

To view the search directory of GCC preprocessing C:

echo | gcc -x c -v -E -

To view the search directory of GCC preprocessing C + +

echo | gcc -x c++ -v -E -

To view the search directory of C + + preprocessed by clang:

echo | clang -x c++ -v -E -

cwd : current working directory