2013-01-18 41 views
1

我想運行位於桌面的C代碼,頭文件位於其他位置。什麼應該是適合編譯和執行的gcc命令?我附上了下面的代碼。我在詢問這方面的問題並提供幫助。如何在Linux中使用gcc命令編譯C代碼時包含lib

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

您可以使用'-I'選項將目錄添加到包含路徑。 –

+0

你能寫出完整的命令嗎? –

+1

這只是'gcc -I「/ path/to/folder/that/contains/the/headers」program.c「 –

回答

1

C編譯器包括頭文件(通過#include)或者通過:

  1. #include "somename.h":它開始在目錄seaching其中源是somefile.h,如果找不到它有它開始看起來像(2)
  2. #include <somename.h>:它搜索的(依賴於系統)目錄的序列。在類Unix系統(Linux,MacOS)中,這基本上是/usr/include,但可能會添加其他目錄。

您可以在大多數編譯器中通過-I/some/path flags來控制它,它在序列(2)的開始處添加/some/path。還需要注意的是上面的somename.h可以包括/,因此,如果你寫

#include "this/file.h" 

然後查找file.hthis目錄在當前目錄。

0

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

+0

我如何執行?看我的代碼有兩個頭文件。我應該在路徑中定義兩個頭文件嗎?如果您根據代碼中給出的路徑編輯命令,這將會很有幫助。謝謝 –

+0

你只需要包含包含標題的目錄。編譯器會自動爲你挑選 –

+0

我沒有得到你的Shantanu。我是這個平臺的新手。請給我舉例。 –

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 
+0

顯示「未定義的引用........」。該位置是否在雙引號(「」)下? –

+0

@Naseef Ur Rahman「Undefined reference to ...」是鏈接器錯誤。這意味着編譯器找到頭文件,但鏈接程序找不到相應的目標文件(或庫)。嘗試添加像-lpulse這樣的命令行 – cody

+0

你能寫嗎? –

相關問題