I know this one way of adding include paths to clang:-
clang++ -I <dir> a.cpp
but with this, that path is only added for that particular file, and u have to write that every time linting, so how can I add some include paths globally to clint.
There are also some environment variables which Clang looks at for include paths. For c++, they would be CPATH (both C and C++) and CPLUS_INCLUDE_PATH (C++ only) (And LIBRARY_PATH for the linker). So you can add something like this to your shell startup file if you are using bash or similar:
export CPLUS_INCLUDE_PATH="${CPLUS_INCLUDE_PATH:+${CPLUS_INCLUDE_PATH}:}<dir>"
And you could also just alias clang++ with clang++ -I<dir>.
Related
I have a project (a library) that is subdivided into a few directories with code in them. I'd like to to have g++ search for header files in the project's root directory, so I can avoid different include paths for same header files across multiple source files.
Mainly, the root/ directory has sub-directories A/, B/ and C/, all of which have .hpp and .cpp files inside. If some source file in A wanted to include file.hpp, which was in B, it would have to do it like this: #include "../B/file.hpp". Same for another source file that was in C. But, if A itself had sub-directories with files that needed file.hpp, then, it would be inconsistent and would cause errors if I decided to move files (because the include path would be "../../B/file.hpp").
Also, this would need to work from other projects as well, which reside outside of root/. I already know that there is an option to manually copy all my header files into a default-search directory, but I'd like to do this the way I described.
Edit: all programs using the library must compile only with g++ prog.cpp lib.a -o prog. That means permanently setting the include path for g++!
A/code.cpp
#include <B/file.hpp>
A/a/code2.cpp
#include <B/file.hpp>
Compile using:
g++ -I /your/source/root /your/source/root/A/code.cpp
g++ -I /your/source/root /your/source/root/A/a/code2.cpp
Edit:
You can use environment variables to change the path g++ looks for header files. From man page:
Some additional environments variables affect the behavior of the
preprocessor.
CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH
Each variable's value is a list of directories separated by a special character, much like PATH, in which to look for header
files. The special character, "PATH_SEPARATOR", is target-dependent and determined at GCC build time. For Microsoft Windows-based targets it
is a semicolon, and for almost all other targets it is a colon.
CPATH specifies a list of directories to be searched as if specified with -I, but after any paths given with -I options on the
command line. This
environment variable is used regardless of which language is being preprocessed.
The remaining environment variables apply only when preprocessing the particular language indicated. Each specifies a
list of directories to be
searched as if specified with -isystem, but after any paths given with -isystem options on the command line.
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.
There are many ways you can change an environment variable. On bash prompt you can do this:
$ export CPATH=/your/source/root
$ g++ /your/source/root/A/code.cpp
$ g++ /your/source/root/A/a/code2.cpp
You can of course add this in your Makefile etc.
gcc -I/path -L/path
-I /path path to include, gcc will find .h files in this path
-L /path contains library files, .a, .so
it's simple, use the "-B" option to add .h files' dir to search path.
E.g. g++ -B /header_file.h your.cpp -o bin/your_command
Headers included with #include <> will be searched in all default directories , but you can also add your own location in the search path with -I command line arg.
I saw your edit you could install your headers in default locations usually
/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include
Confirm with compiler docs though.
i have two folders named Source and powerserver in my home directory. In source direectory i have some code related to project and it uses a .h header file from power server directory. when am compiling using make -f somename.lnx32 I am getting the below error.
error: cst.h no such file or directory.
I am new to this i dont know how to access that .h file ?
GCC looks in several different places for headers. On a normal Unix system, if you do not instruct it otherwise, it will search in default directories.
You can add directories to this list with the -I command-line option. All the directories named by -I are searched, in left-to-right order, before the default directories.
Try gcc -c -I/path_to_powerserver_directory to include header files in GCC search path.
Currently this particular header file is not found to gcc in it's search paths.
It's tagged Linux so i guess you use GCC.
#include <someheader.h> means to search someheader.h in system directories (ie. /usr/include), which you can override using the -Ipath option with GCC.
But in your case, you should be using #include "someheader.h" with the correct user path in your Makefile with the -iquote option.
Let us assume that in first.h we have #include "aaa/second.h" and in the aaa/second.h we have #include "bbb/third.h". I think that in the "default settings" the compiler will complain if "third.h" is not located in "aaa/bbb".
Is it possible to change this behavior in such a way that the directory, in which the first.cpp is located is used to construct the full names in all includes?
For example, if "first.h" is located in '/home/bucky/' then #include "bbb/third.h" (from "aaa/second.h") should be interpreted as /home/bucky/bbb/third.h and not as /home/bucky/aaa/bbb/third.h.
EDITS
I cannot change the whole source code. In the code quotation marks are used instead of angle brackets.
I compile using g++ -std=c++0x name.cpp -o name in the command line. I do it in two different terminals. It looks like in the first terminal the working directory is used to construct the full names and in the second terminal it is not the case. I am almost sure that it happens because of the environment variables but I do not know which ones. So, my question is, to larger extent, what environment variables can force the compiler to construct full names using the working directory.
EDIT 2
In my test.cpp file I include "first.h". This inclusion does not cause any problem (complier sees "first.h"). The "first.h" file includes "ppp/second.h". It also causes no problems. But "ppp/second.h" includes "ppp/third.h" and this is the place where the problem appears. I think that the reason of the problem is that "second.h" tries to find "third.h" in the "ppp" subdirectory of the directory where second.h is located. In other words, second.h tries to find the third.h in the "ppp/ppp" subdirectory (because second.h is located in the ppp subdirectory).
In another terminal, the same compilation command, in the same directory does not cause any problem. The reason, is obviously in the values of the environment varibales.
Yes. The exact mechanics depend on the compiler but the short and the long of it is that you need to configure your compiler to include the project path in the search path. For GCC and clang that’s done via the -I command line flag (-I path/of/first.cpp). This configuration would usually be done in the project settings (if you’re working with an IDE), a Makefile or similar.
Since you’re talking about environment variables: the flags that are passed to the g++ and c++ compiler are controlled by the CXXFLAGS and CFLAGS variables.
You should set up include paths for your project globally. In your example, you would pass some option like -I /home/bucky to your compiler (if it is GCC or Clang). MSVC has analogous options.
(All #includes are searched relative to the include paths. The difference between <...> and "..." is that the latter also searches the current directory.)
I am trying to use OpenBabel and am experiencing great difficulty with setting up a global search path for include files. I have successfully linked to the libraries with $LD_LIBRARY_PATH, but when compiling with the GNU C++ compiler, it cannot find the include files. Is there a global include environment variable on Linux, and if so, what is it?
You could give the include path to GCC using the option -I:
g++ -I/path/to/the/include/dir blabla
Please note that also the library dir may be bassed via -L option -L/path/to/lib/dir. LD_LIBRARY_PATH is usually considered a dirty hack.
You can have multiple -I (and -L) options:
g++ -I/dir/include1 -I/dir/include2
If you check the manpage for cpp (the C Preprocessor), it state that it will treat the following environment variables like the -I option mentioned above:
CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
Now, I believe that current g++ and gcc use an inbuilt cpp, but I would expect that it would function like the stand alone cpp, and respect these environment variables.
I have a linux system at my workplace with pretty old packages and no root access. I'm compiling packages that I need from source with --prefix=[somewhere in homedir]. My problem is that I just can't find out how to convince configure to look for header files in a specific directory. The source is cpp. I tried with environment variables related to g++ and looking up flags and googling but I had no success. Can someone help me solve this?
Usually you can pass additional compiler flags inside CXXFLAGS. For gcc you can specify more include directories with -I/some/dir, e.g.
$ ./configure CXXFLAGS="-I/some/dir/"
where /some/dir/ contains your headers.
The normal way to do this is --with-<feature>=<header directory>.
CPPFLAGS = C Preprocessor Flags, these flags will be used for C and C++ compilation.
CFLAGS = C Flags, these flags will be used when compiling C.
CXXFLAGS = C++ Flags, these flags will be used when compiling C++.
The -I flag specifies an additional include directory to be used during compilation.
Generally it's a good idea to use CPPFLAGS when specifying include directories, that way you know it will be used even if the project has some source that is compiled as C.
Of course, there might also be circumstances where you only want the include directory to be used by C or C++, but not both. In which case you would obviously be better served by using CFLAGS or CXXFLAGS instead.
It's better to use CPPFLAGS to specify include directories.
./configure CPPFLAGS="-I/your/whatever/includedir"