如何在linux中使用gcc命令编译C代码时包含lib

时间:2013-01-18 05:15:42

标签: c++ c linux gcc centos

我想运行位于桌面的C代码,其中头文件位于其他位置。什么应该是适当的GCC命令用于编译和执行?我已附上以下代码。我在这方面要求善意的考虑和帮助。

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

3 个答案:

答案 0 :(得分:1)

看起来你只需要替换它:

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

用这个:

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

并且您的命令行必须是这样的:

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

您需要将'program.c'替换为源文件的名称。

或者甚至只用接下来的两行替换这两行:

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

(目录/ usr / include看起来像标准包含路径) 在这种情况下,您的命令行将只是:

gcc program.c -lpulse 

答案 1 :(得分:1)

C编译器通过以下方式包含头文件(通过#include):

  1. #include "somename.h":它开始在somefile.h的源目录中进行搜索,如果在那里找不到,它开始看起来像(2)
  2. #include <somename.h>:它搜索一系列(系统相关的)目录。在类Unix系统(Linux,MacOS)中,这基本上是/usr/include,但可能会添加其他目录。
  3. 您可以通过-I/some/path flags在大多数编译器中对此进行控制,在序列的开头添加/some/path(2)。另请注意,上面的somename.h可以包含/,因此如果您编写

    #include "this/file.h"
    

    然后它在当前目录的file.h目录中查找this

答案 2 :(得分:0)

尝试gcc -c -I/path/to/source/files fileName.c。查看http://www.network-theory.co.uk/docs/gccintro/gccintro_22.html