2013-12-20 32 views
0

我正在嘗試向c文件添加一個新函數「pa_context_set_sink_input_paused」。這個c文件是pulseaudio的一部分。我也能夠成功編譯pulseaudio。未定義的引用 - 本地符號

我添加到c文件中的代碼(其中定義了許多其他函數,並且聲明爲全局函數)。

pa_operation* pa_context_set_sink_input_paused(pa_context *c, uint32_t idx, int pause, pa_context_success_cb_t cb, void *userdata) 
{ 
    pa_operation *o; 
    pa_tagstruct *t; 
    uint32_t tag; 

    pa_assert(c); 
    pa_assert(PA_REFCNT_VALUE(c) >= 1); 

    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED); 
    PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE); 
    PA_CHECK_VALIDITY_RETURN_NULL(c, idx != PA_INVALID_INDEX, PA_ERR_INVALID); 
    //PA_CHECK_VALIDITY_RETURN_NULL(c, pa_cvolume_valid(volume), PA_ERR_INVALID); 
PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 11, PA_ERR_NOTSUPPORTED); 

    o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata); 

    t = pa_tagstruct_command(c, PA_COMMAND_SET_SINK_INPUT_PAUSE, &tag); // ADD A NEW COMMAND 
    pa_tagstruct_putu32(t, idx); 
    pa_tagstruct_puts(t, NULL); 
    pa_tagstruct_put_boolean(t, pause); 
    pa_pstream_send_tagstruct(c->pstream, t); 
    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref); 

    return o; 
} 

在頭文件中,函數聲明這樣

/** Set the pause of a sink input stream specified by its index */ 

pa_operation* pa_context_set_sink_input_paused(pa_context *c, uint32_t idx, int pause, pa_context_success_cb_t cb, void *userdata); //ADDED BY SATHISH 

編譯如下所示:

$ gcc pa_listclients_pause.c -o pa_listclients_pause -L /home/sathish/Desktop/pulseaudio-4.0/build/src/.libs/ -lpulse 
/tmp/ccueP47b.o: In function `pa_get_devicelist': 
pa_listclients_pause.c.text+0x6cb): undefined reference to `pa_context_set_sink_input_paused' 
collect2: error: ld returned 1 exit status 

但是當我嘗試運行使用這個新功能的程序,它返回我「未定義的參考」。

我試圖運行nm命令並檢查新的功能是否出現在libpulse.so

該函數返回我已經找到了..

$ nm -A *.so | grep "pa_context_set_sink_input_paused" 
libpulse.so:0001d4a0 **t** pa_context_set_sink_input_paused 

它被識別爲局部符號與字母「t」。

我無法理解發生了什麼。你能幫助我嗎?

感謝, Sathish所在

更新: -

謝謝你們,

Your pointers helped me to narrow down close to a solution.. I edited my Makefile process to expose my new function as a "GLOBAL SYMBOL". 

In explanation, there was a file named "map-file" which holding which functions become as global and local. I added my function into the global part of the file.. These seems to have temporarily solved the issue.. 

I have added the new error I am facing as a seperate question. 

http://stackoverflow.com/questions/20702888/relocation-error-while-running-binary 

Any pointers on the new issue ?? 

感謝, Sathish所在

+0

可以顯示的代碼? – tristan

+0

該代碼有大約一千行..如果我在這裏粘貼代碼,我不會看起來不錯。我添加了我添加的特定功能。 c文件和頭文件是pulseaudio更大的構建系統的一部分。 – Sathish

+0

您是否將'pa_context_set_sink_input_paused()'添加到'pa_listclients_pause.c'中? – alk

回答

1

這是例子演示了類似的問題:

在我的shar ED庫:

static void test_func_1() 
{ 
} 

void test_func_2() 
{ 
} 

納米顯示:

int main() 
{ 
    test_func_1(); 
    test_func_2(); 

    return 0; 
} 



>g++ -g -m64 main.cpp -o main -L. -lmylib 
/tmp/cciphwuO.o: In function `main': 
/main.cpp:32: undefined reference to `test_func_1()' 
collect2: ld returned 1 exit status 

或者:

void test_func_1() 
{ 

} 
void __attribute__ ((visibility ("default"))) test_func_2() 
{ 

} 

和建築

>nm -C ./libmylib.so | grep test 
000000000000062a T test_func_2() 
0000000000000624 t test_func_1() 

當與該庫鏈接主程序-fvisibility:

g++ -fvisibility=hidden -m64 -shared -g -fpic mylib.cpp -o libmylib.so 

給出了同樣的問題:

>nm -C ./libmylib.so | grep test 
00000000000005c4 t test_func_1() 
00000000000005ca T test_func_2() 
+0

感謝您的回答。但我有疑問。我沒有明確地將其設置爲靜態。有沒有其他方式可以將其定義爲本地符號? – Sathish

+0

當你構建你的程序時,你使用'-fvisibility = hidden'嗎?你說你不使用靜態,所以我想你以某種方式隱藏你的功能。我知道gcc中有選項-fvisibility,它會影響符號的可見性。你可以看到它的描述:http://gcc.gnu.org/wiki/Visibility –

+0

謝謝,這幫助我.. – Sathish