- What is the environment variable for GCC/G++ to look for .h files during compilation: LIBRARY_PATH, C_PATH, C_INCLUDE_PATH or CPLUS_PATH?
- 3 Answers 3
- How to include header files in GCC search path?
- 3 Answers 3
- Example usage in MacOS / Linux
- Example usage in Windows
- Not the answer you’re looking for? Browse other questions tagged c++ gcc header or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- How to specify libraries paths in gcc?
- 3 Answers 3
- Linking
- Running
- How to add a library include path for NetBeans and gcc on Windows?
- 1 Answer 1
- What are the GCC default include directories?
- 4 Answers 4
What is the environment variable for GCC/G++ to look for .h files during compilation: LIBRARY_PATH, C_PATH, C_INCLUDE_PATH or CPLUS_PATH?
Is there an environment variable for GCC/G++ to look for .h files during compilation?
I google my question, there are people say LIBRARY_PATH, C_PATH, C_INCLUDE_PATH, CPLUS_PATH, so which one is it?
3 Answers 3
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 ‘.
I think that most setups avoid using the environment variables and instead pass the include directories in the command line using the -I option. there will usually be a makefile variable or an IDE setting to control what gets passed to -I .
How to include header files in GCC search path?
I have the following code in a sample file:
However, this code is located in various folders within /home/me/development/skia (which includes core/ animator/ images/ ports/ svg/ and a lot more.)
How can I make GCC recognize this path?
3 Answers 3
Try gcc -c -I/home/me/development/skia sample.c .
The -I directive does the job:
Using environment variable is sometimes more convenient when you do not control the build scripts / process.
For C includes use C_INCLUDE_PATH .
For C++ includes use CPLUS_INCLUDE_PATH .
See this link for other gcc environment variables.
Example usage in MacOS / Linux
Example usage in Windows
Not the answer you’re looking for? Browse other questions tagged c++ gcc header or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to specify libraries paths in gcc?
I am writing a program using OpenCV calls and I want to compile it and make the executable. I already read the gcc tutorial but I still would like to know exactly how to link the OpenCV libraries I am using. My command and the related output is below:
How should I correctly specify the libraries?
I ran the command below. It compiles and links but does not run correctly:
What else am I missing?
3 Answers 3
Move the -L and -l to the end of the command line:
/programmi$ gcc imagefilter.c -o imagefilter -I/home/savio/opencv-3.0.0/include/opencv -L/home/savio/opencv-3.0.0/cmake_binary_dir/lib -lopencv_core -lopencv_highgui -lopencv_imgproc /tmp/ccyNTX5t.o: nella funzione «main»: imagefilter.c:(.text+0x1396): riferimento non definito a «cvLoadImage» imagefilter.c:(.text+0x1403): riferimento non definito a «cvSaveImage» collect2: error: ld returned 1 exit status – Saverio Jul 16 ’15 at 13:52
The way my IDE handles the process is to put the -L tag up front and the -l tag at the end. All of the -l tags need to come after your target so that the compiler knows which symbols need to be resolved before searching.
Linking
For linking, make sure you specify object files (or source files) before libraries ( -lxxx options). And make sure the -L option for a given library appears before the -l option that uses it. The order of libraries can matter; list libraries in an order such that libraries listed earlier reference code in those listed later. (When designing sets of libraries, avoid circular references between libraries; they’re a plague.)
Running
Both the compiler/linker and the runtime system need to be able to find the shared objects. You use the -L option to tell the linker where to find the libraries. You have a variety of ways of telling the runtime (dynamic loader) where to find the libraries:
On some systems, you can add a -R option to the command line to specify where libraries may be found at runtime:
Not all systems support this option. If yours doesn’t, you have to move on.
The disadvantage of this option is that the location you specify is embedded in the binary. If the libraries on the customers’ machines is not in the same place, the library won’t be found. Consequently, a path under someone’s home directory is only appropriate for that user on their machines — not for the general population. OTOH, if the software is installed by default in, say, /opt/packagename/lib , then specifying that with -R is probably appropriate.
Add the directory to LD_LIBRARY_PATH environment variable (or its equivalent; for example, DYLD_LIBRARY_PATH on Mac OS X, or SHLIB_PATH on HP-UX IIRC).
The first notation sets the environment variable just for as long as the program is running. It can be useful if you need to compare the behaviour of two versions of a library, for example. The second notation sets the environment variable for the session. You might include that in your .profile or equivalent so it applies to every session.
Some systems have an LD_RUN_PATH environment variable too. And some have 32-bit and 64-bit variants of the environment variables.
This is fiddly for users and installers alike; how do you ensure the environment variable is set for everyone that uses your code? An environment-setting shell script that then runs the real program can help here.
Add the directory to the configuration file that specifies the list of known directories for the dynamic loader to search. Needless to say, that’s platform specific — file name, format, location (usually under /etc somewhere) and mechanism used to edit it. The file might be /etc/ld.so.conf . There might well be a program to edit the config file correctly.
Install the libraries in a location that will be searched anyway (without reconfiguring the dynamic loader). This might be /usr/lib , or maybe /usr/local/lib or some other related directory.
How to add a library include path for NetBeans and gcc on Windows?
How to add a library include path for NetBeans and gcc on Windows?
- NetBeans 7.1.2
- MinGW ( mingw-get-inst-20120426.exe )
- gcc 4.7.0
1 Answer 1
For example, you want to add the directories in C:\Program Files (x86)\Example\1.0\include\ as the include paths.
First, set up code assistance:
- NetBeans > Tools > Options > C/C++ > Code Assistance
- C Compiler > Include Directories:
- C:\Program Files (x86)\Example\1.0\include\shared
- C:\Program Files (x86)\Example\1.0\include\other
- C:\Program Files (x86)\Example\1.0\include
- C:\MinGW\lib\gcc\mingw32\4.7.0\include
- C:\MinGW\include
- C:\MinGW\lib\gcc\mingw32\4.7.0\include-fixed
- .
- C++ Compiler > Include Directories:
- C:\Program Files (x86)\Example\1.0\include\shared
- C:\Program Files (x86)\Example\1.0\include\other
- C:\Program Files (x86)\Example\1.0\include
- C:\MinGW\lib\gcc\mingw32\4.7.0\include\c++
- C:\MinGW\lib\gcc\mingw32\4.7.0\include\c++\mingw32
- C:\MinGW\lib\gcc\mingw32\4.7.0\include\c++\backward
- C:\MinGW\lib\gcc\mingw32\4.7.0\include
- C:\MinGW\include
- C:\MinGW\lib\gcc\mingw32\4.7.0\include-fixed
- .
- OK.
- C Compiler > Include Directories:
The C:\MinGW\. directories are examples only. Do not actually add them. NetBeans should have detected and added the MinGW directories automatically. If not, try resetting the settings:
- NetBeans > Tools > Options > C/C++
- Code Assistance
- C Compiler > Reset Settings
- C++ Compiler > Reset Settings
- Build Tools
- Restore Defaults
- Code Assistance
For instructions on automatic code assistance for existing sources, see:
C/C++ Projects Quick Start Tutorial:
How to Configure Code Assistance When Creating a Project from Existing Code:
Now, configure the project options:
- Right click on project > Properties
- Configuration:
- Build
- C Compiler
- General
- Include Directories:
- C:\Program Files (x86)\Example\1.0\include\shared
- C:\Program Files (x86)\Example\1.0\include\other
- C:\Program Files (x86)\Example\1.0\include
- Include Directories:
- Compilation Line
- Additional Options:
- -std=c11 -g3 -pedantic -Wall -Wextra -O0
- Additional Options:
- General
- C++ Compiler
- General
- Include Directories:
- C:\Program Files (x86)\Example\1.0\include\shared
- C:\Program Files (x86)\Example\1.0\include\other
- C:\Program Files (x86)\Example\1.0\include
- Include Directories:
- Compilation Line
- Additional Options:
- -std=c++11 -g3 -pedantic -Wall -Wextra -O0
- Additional Options:
- General
- C Compiler
- OK.
For adding command-line options by default for all projects, see:
Any spaces in the path should be automatically escaped. Any backward slashes should be replaced with forward slashes automatically.
For example, the «All options» textbox in «Additional Options» looks like this:
If this does not work, you may have to fix the path and add the includes manually in the additional options. For example, replace /C/ with C:/ .
If you are using Cygwin make and if you try to clean or rebuild the project with colons in the command, you may get a *** multiple target patterns. Stop. error message. According to the answers from Multiple target patterns? and Very simple application fails with «multiple target patterns» from Eclipse, » make sees the : in the path and thinks it is another target definition, hence the error.»
The workaround is to delete the generated build and dist folders every time before you build your project. However, this can be annoying, so you could try MinGW MSYS make instead (not to be confused with MinGW make , which is unsupported).
For MinGW and MSYS configuration instructions, see:
Configuring the NetBeans IDE for C/C++/Fortran:
For working with MinGW and Unicode, you should install the latest version of MinGW-w64. See:
What are the GCC default include directories?
When I compile a very simple source file with gcc I don’t have to specify the path to standard include files such as stdio or stdlib.
How does GCC know how to find these files?
Does it have the /usr/include path hardwired inside, or it will get the paths from other OS components?
4 Answers 4
In order to figure out the default paths used by gcc / g++ , as well as their priorities, you need to examine the output of the following commands:
The credit goes to Qt Creator team.
There is a command with a shorter output, which allows to automatically cut the include pathes from lines, starting with a single space:
The credit goes to the libc++ front-page.
Though I agree with Ihor Kaharlichenko’s answer for considering C++ and with abyss.7’s answer for the compactness of its output, they are still incomplete for the multi-arch versions of gcc because input processing depends on the command line parameters and macros.
echo | /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-g++ -specs=nano.specs -mcpu=cortex-m4 -march=armv7e-m -mthumb -mfloat-abi=soft -x c++ -E -Wp,-v\ — -fsyntax-only yields
whereas echo | /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-g++ -x c++ -E -Wp,-v — -fsyntax-only yields
The former invocation utilizes newlib (see lines 1 and 3 of the output), the latter goes with the standard includes. The common files at the end of the list are an example for the usage of include_next .
Bottom line: Always consider all macros and compiler options when printing the include directories.