Finding out what the GCC include path is [duplicate] - c

This question already has answers here:
What are the GCC default include directories?
(5 answers)
Closed 8 years ago.
I'm trying to programmatically find the #include path on Linux, which as I understand it, in practice means finding what GCC considers it to be. (Is that quite true? How does Clang do it?)
According to http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html some of the components involve the CPU architecture and the GCC version; the latter in particular seems tricky; I suppose it could be obtained by running gcc --version and parsing the output (or gcc -v), but this seems inelegant at best and fragile at worst. Doing it from within one's code assuming one's program is being compiled with GCC might be another option, but it would require depending on that assumption.
What's the recommended way to do it?

The command
echo | gcc -E -Wp,-v -
will show the include path in use.

I'm not sure what you mean by the recommended way to find the include path. The standard way is as given below (for c and c++):
$ `gcc -print-prog-name=cc1` -v
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include
End of search list.
^C
$ `gcc -print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include
End of search list.
^C

Related

fatal error: stdio.h: No such file or directory

I have a problem with #include <stdio.h> on mac Mojave 10.14.1
I have a default gcc compiler together with other gcc compilers.
DorothyeMacBook:Desktop dorothy$ which gcc
/usr/local/bin/gcc
The version is
DorothyeMacBook:Desktop dorothy$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin15.6.0/7.1.0/lto-wrapper
Target: x86_64-apple-darwin15.6.0
Configured with: ../gcc-7.1.0/configure --enable-languages=c++,fortran
Thread model: posix
gcc version 7.1.0 (GCC)
Now I am in the path :
/Users/dorothy/Desktop
I have stdio.h in the lib
DorothyeMacBook:Desktop dorothy$ find /usr -name "stdio.h"
find: /usr/sbin/authserver: Permission denied
/usr/local/include/c++/7.1.0/tr1/stdio.h
/usr/local/include/c++/4.9.2/tr1/stdio.h
/usr/local/lib/gcc/x86_64-apple-darwin15.6.0/7.1.0/include/ssp/stdio.h
/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include/ssp/stdio.h
/usr/local/Cellar/gcc/8.2.0/include/c++/8.2.0/tr1/stdio.h
/usr/local/Cellar/gcc/8.2.0/lib/gcc/8/gcc/x86_64-apple-darwin18.2.0/8.2.0/include/ssp/stdio.h
/usr/local/Cellar/gcc/8.2.0/lib/gcc/8/gcc/x86_64-apple-darwin18.2.0/8.2.0/include-fixed/stdio.h
However when I compile the file under the current path /Users/dorothy/Desktop Terminal will give me an error:
DorothyeMacBook:Desktop dorothy$ gcc inverse.c -o inv
inverse.c:1:10: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^~~~~~~~~
compilation terminated.
I still have a problem linking gcc with stdio.h on my mac. However, I have found an alternative way to compile the program with gcc. I typed brew install gcc in my terminal, that means a new gcc8.0.2 will be installed.(which is my case, I don't know what gcc you will get) Then I use gcc-8 instead of gcc to compile my program. In this way, the new gcc8.0.2 is invoked.
You can check the command name for gcc variants by going to /usr/local/bin All the names for gcc commands are listed there. As for me, those are
g++
g++-8
gcc
gcc-8
If you have further question, you can contact me and I will give more details.
#Jonathan Leffler is right. Apple made another big cake for us.
Since /usr/include has been moved (now it in /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include after you install commandline tools), old versions of gcc build cannot find the most bottom level include files. And this is a problem. (What are there in their head) So only you are developer you use /usr/include?
This is NOT Unix-like.
Here is what happened. For gcc-4.9(gcc-4.9 and gcc-9 below are all brew gcc.), the include search paths are:
enter ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
.
/usr/local/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
/usr/local/Cellar/gcc#4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include
/usr/local/Cellar/gcc#4.9/4.9.4_1/include
/usr/local/Cellar/gcc#4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include-fixed
/System/Library/Frameworks
/Library/Frameworks
End of search list. here
There is system level file stdio.h, this is why stdio.h cannot be found.
The problem in:
inverse.c:1:10: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^~~~~~~~~
compilation terminated.
is:
Refer to /usr/local/Cellar/gcc#4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include/ssp/stdio.h
#ifndef _SSP_STDIO_H
#define _SSP_STDIO_H 1
#include <ssp.h>
#include_next <stdio.h>
#include_next means include next one name stdio.h, this one is just a guider.
Give a deprecated shot. cp all files in /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include to /usr/local/Cellar/gcc#4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include-fixed, gcc-4.9 will work again.
But for gcc-9 the path problem is fine. See:
#include "..." search starts here:
#include <...> search starts here:
.
/usr/local/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include # key path
/usr/local/Cellar/gcc/9.1.0/lib/gcc/9/gcc/x86_64-apple-darwin18/9.1.0/include
/usr/local/Cellar/gcc/9.1.0/lib/gcc/9/gcc/x86_64-apple-darwin18/9.1.0/include-fixed
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks
End of search list.
Actually this bug can be simply fixed by reinstall xcode commandline tools, after that gcc-4.9 will work again.

Determining where header file is found

When I compile with gcc -v hello.c*, the output shows a search path for #include:
$ gcc -v hello.c
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
...
clang -cc1 version 9.1.0 (clang-902.0.39.2) default target x86_64-apple-darwin17.7.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Library/Developer/CommandLineTools/usr/lib/clang/9.1.0/include
/Library/Developer/CommandLineTools/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
Is there a way to determine definitively which of these locations is used? For example, say that hello.c contained #include <stdio.h>. I can see manually that there are versions of stdio.h in multiple locations, presumably which can use different function construction:
$ find /usr/local/include -name "stdio.h"
/usr/local/include/c++/5.5.0/tr1/stdio.h
$ find /Library/Developer/CommandLineTools/usr/include -name "stdio.h"
/Library/Developer/CommandLineTools/usr/include/c++/v1/stdio.h
$ find /usr/include -name "stdio.h"
/usr/include/c++/4.2.1/tr1/stdio.h
/usr/include/sys/stdio.h
/usr/include/stdio.h
In Python, this would look something like:
>>> import math
>>> math.__file__
'/Users/brad/miniconda3/lib/python3.6/lib-dynload/math.cpython-36m-darwin.so'
*I'm on a Macbook, so gcc actually seems to route to clang, although it appears to be a bona fide executable rather than symlink.
From gcc man page:
-H
Print the name of each header file used, in addition to other normal activities. Each name is indented to show how deep in the #include stack it is. Precompiled header files are also printed, even if they are found to be invalid; an invalid precompiled header file is printed with ...x and a valid one with ...! .

Still get 'curl/curl.h: No such file or directory' in spite of all looks proper

I installed on windows curl 7.28.0 from curl-7.28.1-devel-mingw32.zip through minGW console to default directory like:
./config && make && make install
All needed headers (aka curl.h, types.h ...) I see in C:\MinGW\msys\1.0\local\include\curl
libcurl.pc placed in C:\MinGW\msys\1.0\local\lib\pkgconfig\
libcurl.a, libcurl.dll.a and libcurl.la placed in C:\MinGW\msys\1.0\local\lib.
My download_file.c file includes are:
...
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
...
I try to compile the C code with followed command through gcc:
$ gcc -IC:/MinGW/msys/1.0/include/
-IC:/MinGW/msys/1.0/local/include/curl
-IC:/MinGW/msys/1.0/local/lib/pkgconfig
-o download_file download_file.c -llibcurl -lcurl
with absolute path get the same error:
gcc -I/include/
-I/local/include/curl
-I/local/lib/pkgconfig
-o download_file download_file.c -llibcurl -lcurl
But I still get an error:
download_file.c:21:23: fatal error: curl/curl.h: No such file or directory compilation terminated.
row 21 is #include <curl/curl.h>
What I did wrong? Please help.
You have the curl/ directory in the source code, but also in the option.
It seems the option should point out the higher-level directory in which curl/ is, so it should be something like:
-I/local/include/
I think the problem is likely that you give your include paths on the command line in the Win32 path format. This is not the same as the one used by msys (or ultimately Cygwin).
Try these:
$ gcc -I/include/
-I/local/include/curl
-I/local/lib/pkgconfig
...
Hope I got the absolut paths right, but you can check in your msys shell.
What ticked me off was that you use ./config, which wouldn't work from the Command Prompt, but works from the msys shell. So you need to give paths that all the programs in MinGW understand.
Basically, most programs in MinGW only have the concept of a single file system root, like on any unixoid system, while Win32 has multiple (the drive letters). Since the MinGW programs are linked accordingly, you need to give paths that they understand.
Thank you very much to #0xC0000022L and #unwind. By your help I fixed my problem.
0xC0000022L you are right about absolute path
unwind you are right about -I/local/include/ instead -I/local/include/curl
I found other problem: -L/local/lib instead -I/local/lib.
So this is a working command:
gcc -I/include/
-I/local/include
-L/local/lib
-o download_file download_file.c -llibcurl -lcurl

Cannot compile C code with #include <sys/times.h> in Cygwin

I was trying to install/compile libraries such as igraph and SNAP in Windows 7 using Cygwin (and also tried MinGW-MSYS) and I ran into some problems.
I think I have narrowed down the problem to this error given by ./configure:
checking sys/times.h usability... no
checking sys/times.h presence... no
checking for sys/times.h... no
In Cygwin, /usr/include/sys/times.h actually do exists. I googled about this for MinGW and it seems that sys/times.h is not available for MinGW because "the POSIX/BSD "times" function is not part of the ANSI standard and does not exist under Mingw32 runtime".
As an experiment, I tried compiling this C code in Cygwin using gcc:
#include <stdio.h>
#include <sys/times.h>
int main (void)
{
return 0;
}
This does not compile, with the error sys/times.h no such file or directory. This happens even when I change the include to </usr/include/sys/times.h> or <usr/include/sys/times.h>. In the Cygwin command promot /usr/include/sys/times.h work correctly.
Question
How do I get sys/times.h usability and presence? Is there a package or library I can install?
Your code compiles with no problem on my Cygwin. Actually /usr/include is one of the default include search paths for gcc, so normally gcc should be able to find sys/times.h.
Perhaps you are using MinGW version of gcc instead of Cygwin gcc? Try which gcc to make sure it's /usr/bin/gcc, and also gcc --version to make sure it does not display like mingw32-gcc.exe (GCC) x.x.x.
You can also try to compile your C file with verbose output:
gcc -v test.c
It shows how gcc searches include files. /usr/include should be one of the search path list if you use Cygwin's gcc.
As I found out, there seems to be some intrinsic problems with this issue, and sys/times.h is not supposed to be used under windows (not supported for some reason).
As I mentioned also here, it's use should actually be removed from the code to make it compile.
I had this same problem. Delete the folder C:\cygwin64 . Reinstall cygwin. Choose all defaults but when you get to 'Select Packaages', make sure to search and select the following one by one: binutils , make , gcc-g++ . For each one, select the dropdown and change form 'skip' to the newest version. Continue until you complete the installation. Everything worked for me from there.

what is default path for header file included in c program?

see if i write in any c file like
#include "header.h"
then it will search this file in current directory
but when i write
#include <header.h>
then where it will go to find this file ?
what is defualt path for header file included in c program?
see i have installed gstreamer in /usr/local but when i am including
#include <gst/gst.h>
i am geeting fatal error: gst/gst.h: No such file or directory
How can i remove this error?
Try running gcc -v -E -. When I do, part of the output is as follows:
#include <...> search starts here:
/usr/lib/gcc/i686-linux-gnu/4.6.1/include
/usr/local/include
/usr/lib/gcc/i686-linux-gnu/4.6.1/include-fixed
/usr/include/i386-linux-gnu
/usr/include
It's not an answer to the gstreamer question, but I hope this still helps!
Pulled from here
The default paths are
/usr/local/include
/usr/include
If you use another path, you can add in your compile command with -I flag. In your case, assuming you have a /usr/local/gst/include directory, you may add -I/usr/local/gst/include and use #include <whatever_you_need.h>
The path searched depends on the implementation (and current configuration). The correct way to find the include path is to use pkg-config
pkg-config --cflags gstreamer
The default path for <> stuff is /usr/include, at least on Unix.
You can add as many default paths as you want with -I /my/new/path compiler option.
`gcc -print-prog-name=cc1` --verbose
and then CTRL+C
you can find those files in:
/usr/include

Resources