0

I know that gcc -xc -E -v - Prints default include paths at the end.

[patryk@patryk-asus-manjaro ~]$ gcc -xc -E -v -  
Using built-in specs.
COLLECT_GCC=gcc
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp --enable-cet=auto
Thread model: posix
gcc version 8.2.1 20181127 (GCC) 
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/cc1 -E -quiet -v - -mtune=generic -march=x86-64
ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/include-fixed
 /usr/include
End of search list.
^C
[patryk@patryk-asus-manjaro ~]$

I created regex

#include <\.\.\.> search starts here:$
(^.+$)*
^End of search list\.$

But It does not works properly. Other problem is that gcc for that parameters does not terminates. I must call SIGINT Ctrl+C on Linux. I'm using Python 3 and subprocess.run.

3
  • gcc does not terminate because you told it to read from stdin, so it's sitting there waiting for input. If you don't want that, don't tell it to parse stdin. Commented Dec 26, 2018 at 21:45
  • @melpomene so I must pass /dev/zero to gcc? Commented Dec 26, 2018 at 21:51
  • Only if you want to parse an infinite stream of NUL bytes. gcc.gnu.org/onlinedocs/cpp/Search-Path.html#Search-Path Commented Dec 26, 2018 at 21:52

2 Answers 2

1

A solution using only the shell:

gcc -x c -E -v /dev/null 2>&1 | sed -n '/include.*search starts here/,/End of search list/{s/^ //p}'

If relevant, be sure to handle the following cases:

  • the directory involves following symlinks
  • there are duplicate directories
  • the directory does not exist
  • there might be more than one gcc binary installed (different version, different arch, etc.), each with different paths
  • even for a single gcc binary, compiler flags (such as -m32) can affect the include path
Sign up to request clarification or add additional context in comments.

Comments

0

I've written some spagheti

def find_re_it_in_list(pattern, input, start=0, stop=-1, flags=0):
    length = len(input)

    if length == 0:
        return None

    end_it = max(0, length - 1)

    if start >= end_it:
        return None

    if stop<0:
        stop = length

    if stop <= start:
        return None

    for it in range(max(0, start), min(stop, length)):
        elem = input[it]
        match = re.match(pattern, elem, flags)
        if match:
            return it

def get_includes(self):
    args = [self.conf['gcc_path'], '-xc', '-E', '-v', os.devnull]
    args.extend(self.env.get('flags', []))

    incl_start_regex = r' *#include <\.\.\.> search starts here: *'
    incl_end_regex = r' *End of search list\. *'

    proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT , text=True)
    lines = proc.stdout.splitlines()

    start_it = find_re_it_in_list(incl_start_regex, lines)
    if start_it == None:
        return []

    end_it = find_re_it_in_list(incl_end_regex, lines, start_it)
    if end_it == None:
        return []

    # theres no paths between them
    if (end_it - start_it) == 1:
        return []

    return lines[start_it+1 : end_it]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.