Linux gcc include files

How can I include a needed C library using GCC?

I am trying to compile the simple C example from this tutorial on Ubuntu using GCC. What do I have to use as arguments for GCC to include the needed libraries for #include ?

7 Answers 7

indeed @debuti . furthermore, seems that one can omit the space between the I or L and the searchpath , and put it all together like: -I

That doesn’t answer the question, only regurgitating the (incomplete) documentation. A proper answer would at least address possible naming conventions used to derived the actual argument. What should the actual argument be? -llibappindicator ? -lappindicator ? Something else?

Use the -l command line option. You can specify the library search path with the -L option. E.g:

gcc -o myprogram -lfoo -L/home/me/foo/lib myprogram.c 

This will link myprogram with the static library libfoo.a in the folder /home/me/foo/lib .

That doesn’t answer the question, only regurgitating the (incomplete) documentation. A proper answer would at least address possible naming conventions used to derived the actual argument. What should the actual argument be? -llibappindicator ? -lappindicator ? Something else?

If you used apt-get , Synaptic Package Manager , etc. to get the appindicator library (vs. building it from source), did you only install the libappindicator1 package or did you also install libappindicator-dev to get the libappindicator header files? Linux packages very often have split the runtime libraries from the compile-time headers. That way people who only need the libraries to satisfy a dynamic link don’t have to install unneeded headers. But since you’re doing development you need those headers and therefore need the libappindicator-dev package as well.

According to packages.ubuntu.com/hu/natty/i386/libappindicator-dev/filelist you need to use -I/usr/include/libappindicator-0.1/libappindicator

Источник

Where are include files stored — Ubuntu Linux, GCC

the compiler, GCC in my case, knows where that stdio.h (and even the object file) are located on my hard drive. It just utilizes the files with no interaction from me. I think that on my Ubuntu Linux machine the files are stored at /usr/include/ . How does the compiler know where to look for these files? Is this configurable or is this just the expected default? Where would I look for this configuration? Since I’m asking a question on these include files, what are the source of the files? I know this might be fuzzy in the Linux community but who manages these? Who would provide and manage the same files for a Windows compiler. I was always under the impression that they come with the compiler but that was an assumption.

Читайте также:  Сервер имен dns linux

4 Answers 4

When the include file is in brackets the preprocessor first searches in paths specified via the -I flag. Then it searches the standard include paths (see the above link, and use the -v flag to test on your system).

When the include file is in quotes the preprocessor first searches in the current directory, then paths specified by -iquote, then -I paths, then the standard paths.

-nostdinc can be used to prevent the preprocessor from searching the standard paths at all.

Environment variables can also be used to add search paths.

When compiling if you use the -v flag you can see the search paths used.

gcc is a rich and complex «orchestrating» program that calls many other programs to perform its duties. For the specific purpose of seeing where #include «goo» and #include will search on your system, I recommend:

$ touch a.c $ gcc -v -E a.c . #include ". " search starts here: #include search starts here: /usr/local/include /usr/lib/gcc/i686-apple-darwin9/4.0.1/include /usr/include /System/Library/Frameworks (framework directory) /Library/Frameworks (framework directory) End of search list. # 1 "a.c" 

This is one way to see the search lists for included files, including (if any) directories into which #include «. » will look but #include <. >won’t. This specific list I’m showing is actually on Mac OS X (aka Darwin) but the commands I recommend will show you the search lists (as well as interesting configuration details that I’ve replaced with . here;-) on any system on which gcc runs properly.

Источник

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?

5 Answers 5

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.

Here’s a breakdown of the flags:

  • -x selects the language, C or C++ respectively
  • -E makes gcc to run the preprocessor only, so no compilation takes place
  • -v prints all the commands run, which is the key to dumping the standard paths
  • — is the «input file» to preprocess, as a convention — stands for stdin (or stdout, depending on the context); echo | feeds an empty string to gcc so effectively we preprocess an empty file generated on the fly

@Ihor — what does the — at the end of the command line do? I’ve seen questions about these dashes elesewhere on Stack Overflow, but their meaning varies by command. As far as I can tell when experimenting with Cygwin, it means gcc will do nothing and ignore all input except Ctrl-C. But gcc in an actual Bash shell might behave very differently.

Читайте также:  Astra linux install python

@palapapa, I suppose it depends on the version of GCC. I tried omitting some of the flags from the above set, and even though there was some output each time, none of them contained the include . search starts here piece which is the one we are looking for. Running on Ubuntu 22.04, GCC 11.3.0

There is a command with a shorter output, which allows to automatically cut the include pathes from lines, starting with a single space:

$ echo | gcc -Wp,-v -x c++ - -fsyntax-only ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../x86_64-redhat-linux/include" #include ". " search starts here: #include search starts here: /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/x86_64-redhat-linux /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/backward /usr/lib/gcc/x86_64-redhat-linux/4.8.2/include /usr/local/include /usr/include End of search list. 

The credit goes to the libc++ front-page.

To summarise the other answers:

c++ -xc++ /dev/null -E -Wp,-v 2>&1 | sed -n ‘s,^ ,,p’

cc -xc /dev/null -E -Wp,-v 2>&1 | sed -n ‘s,^ ,,p’

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

⋮ /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../arm-none-eabi/include/newlib-nano /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1 /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1/arm-none-eabi/thumb/v7e-m/nofp /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1/backward /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/include /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include ⋮ 

whereas echo | /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-g++ -x c++ -E -Wp,-v — -fsyntax-only yields

⋮ /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1 /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1/arm-none-eabi /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1/backward /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/include /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include ⋮ 

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.

Источник

how to include lib when compiling C code with gcc command in linux

I want to run my C code located in desktop with the header files located in other location. What should be the appropriate GCC command for compilation and execution? I have attached the code below. I am asking kind considerations and help in this regards.

#include #endif #include #include #include #include #include #include #include #define BUFSIZE 32 int main(int argc, char*argv[]) < /* The Sample format to use */ static const pa_sample_spec ss = < .format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 2 >; pa_simple *s_in, *s_out = NULL; int ret = 1; int error; /* Create a new playback stream */ if (!(s_out = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) < fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); goto finish; >if (!(s_in = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) < fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); goto finish; >for (;;) < uint8_t buf[BUFSIZE]; ssize_t r; #if 1 pa_usec_t latency; if ((latency = pa_simple_get_latency(s_in, &error)) == (pa_usec_t) -1) < fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error)); goto finish; >fprintf(stderr, "In: %0.0f usec \r\n", (float)latency); if ((latency = pa_simple_get_latency(s_out, &error)) == (pa_usec_t) -1) < fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error)); goto finish; >fprintf(stderr, "Out: %0.0f usec \r\n", (float)latency); #endif if (pa_simple_read(s_in, buf, sizeof(buf), &error) < 0) < fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno)); goto finish; >/* . and play it */ if (pa_simple_write(s_out, buf, sizeof(buf), &error) < 0) < fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); goto finish; >> /* Make sure that every single sample was played */ if (pa_simple_drain(s_out, &error) < 0) < fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error)); goto finish; >ret = 0; finish: if (s_in) pa_simple_free(s_in); if (s_out) pa_simple_free(s_out); return ret; > 

I am new in this platform. Will you write the whole command according to my code, where the headers location is given in include directive? Please give the command for execution also.

Читайте также:  What is linux integration services

Источник

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 .

Glad to see this answer here. Another point worth mentioning would be that when you have many «.c» source files, it’s necessary to specify each and every one of them in the commandline itself. You can’t just do something like a -I to specify that all source files are in a certain directory.

If the header is in the same directory as the source, do you need a special include? I can’t get my code to compile either way, and I’m not sure what the problem is

According to this answer to a similar question, gcc would not search the subdirectories for the different header files automatically. Instead, pkg-config could produce the proper -I option?

@EdwinPratt Perhaps you meant to say that -L tells GCC where to look for binary libraries to include (which are specified with -l ). And -I tells GCC where to look for header files to include.

The -I directive does the job:

gcc -Icore -Ianimator -Iimages -Ianother_dir -Iyet_another_dir my_file.c 

To anyone needing this: it looks like you can include directories with spaces in them with quotes: -I»some path/with spaces» .

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

# `pip install` will automatically run `gcc` using parameters # specified in the `asyncpg` package (that I do not control) C_INCLUDE_PATH=/home/scott/.pyenv/versions/3.7.9/include/python3.7m pip install asyncpg 

Example usage in Windows

set C_INCLUDE_PATH="C:\Users\Scott\.pyenv\versions\3.7.9\include\python3.7m" pip install asyncpg # clear the environment variable so it doesn't affect other builds set C_INCLUDE_PATH= 

Источник

Оцените статью
Adblock
detector