Naseef Chowdhury
Naseef Chowdhury

Reputation: 2464

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 <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#include </usr/include/pulse/simple.h>
#include </usr/include/pulse/error.h>

#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;
}

Upvotes: 1

Views: 6081

Answers (3)

vonbrand
vonbrand

Reputation: 11791

The C compiler includes header files (via #include) either through:

  1. #include "somename.h": It starts seaching in the directory where the source is for somefile.h, if it isn't found there it starts looking like (2)
  2. #include <somename.h>: It searches a sequence of (system dependent) directories. In Unix-like systems (Linux, MacOS) this is basically /usr/include, but might add other directories.

You can control this in most compilers by -I/some/path flags, which add /some/path at the beginning of the sequence (2). Note also that the somename.h above can include /, so that if you write

#include "this/file.h"

then it looks for file.h in this directory in the current directory.

Upvotes: 1

cody
cody

Reputation: 3277

Looks like you just need to replace this:

#include </usr/include/pulse/simple.h>
#include </usr/include/pulse/error.h>

with this:

#include "simple.h"
#include "error.h"

and your command line must be something like this:

gcc -I"/usr/include/pulse" program.c -lpulse

where you need to replace 'program.c' with the name of your source file.

Or even just replace those two lines with the next two:

#include <pulse/simple.h>
#include <pulse/error.h>

(directory /usr/include seems like standard include path) and in this case your command line will be just:

gcc program.c -lpulse 

Upvotes: 1

Tushar Mishra
Tushar Mishra

Reputation: 177

Try gcc -c -I/path/to/source/files fileName.c .See http://www.network-theory.co.uk/docs/gccintro/gccintro_22.html

Upvotes: 0

Related Questions