1

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;
}
4
  • You can add a directory to the include path with the -I option. Commented Jan 18, 2013 at 5:17
  • Can u write the full command? Commented Jan 18, 2013 at 5:18
  • 1
    It's just gcc -I"/path/to/folder/that/contains/the/headers" program.c Commented Jan 18, 2013 at 5:19
  • 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. Commented Jan 18, 2013 at 5:27

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 

4 Comments

shows "Undefined reference to ........". Is the location will be under double quote ( "") ?
@Naseef Ur Rahman "Undefined reference to..." is the linker error. It means that compiler found headers, but linker can't find corresponding object files (or library). Try to add something like -lpulse to command line
@Naseef Ur Rahman As far as I understand you've installed pulse library, and now you need to link with it. So your command line would be "gcc program.c -lpulse" and you need to include header files as "#include <pulse/simple.h>" (see the last few lines of my answer above)
And it would be useful for you to read the article Tushar gave you: network-theory.co.uk/docs/gccintro/gccintro_22.html It contains most of the answers for your questions
0

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

3 Comments

How I can execute? See my code there is two header files. Should I define both header files in the path? If you edit the command according to my path which has been given in the code, it will be helpful. Thanks
You just need to include the directory contains the headers. Compiler will automatically pick it for you
I didn't get u Shantanu. I am new in this platform. Please provide me examples.

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.