2012-03-29 14 views
24

मेरे पास बस एक सिस्टम क्रैश है और उबंटू 11.10 को पुनर्स्थापित करें, और मेरा कोड इस अजीब त्रुटि उत्पन्न करता है।अपरिभाषित संदर्भ 'shm_open', पहले से ही add -lrt ध्वज

मैं परीक्षण करने के लिए एक सरल कोड नमूना लिखा था जहां समस्या है:

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <sys/mman.h> 
#include <sys/stat.h> 

int main (void) { 

    int i; 

    i = shm_open ("/tmp/shared", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); printf ("shm_open rc = %d\n", i); 

    shm_unlink ("/tmp/shared"); 

    return (0); 
} 

और संकलन आदेश

gcc -lrt test.c -o test

त्रुटि है:

/tmp/ccxVIUiP.o: In function `main': 
test.c:(.text+0x21): undefined reference to `shm_open' 
test.c:(.text+0x46): undefined reference to `shm_unlink' 
collect2: ld returned 1 exit status 

मैं पहले से ही जोड़ दिया है- lrt lib, यह अभी भी संकलित क्यों नहीं है? अंत में

+0

मुझे लगता है कि आप -pthread चाहते हो सकता है, लेकिन मैं भूल जाते हैं? कारण। – blueshift

+0

धन्यवाद, मेरे सलाहकार के कंप्यूटर को जोड़ने के लिए आवश्यक है, यह थ्रेड सुरक्षित कार्यों के साथ lpthread का एक बेहतर संस्करण है। और मैं सिर्फ lrt जोड़ सकता था। मुझे लगता है कि दोनों lrt और pthread दोनों POSIX के पुस्तकालय हैं? – bxshi

+0

@bxshi: POSIX लाइब्रेरी नाम निर्दिष्ट नहीं करता है; विभिन्न यूनिक्स स्वादों में अलग-अलग नामांकित पुस्तकालयों में कार्य होते हैं। –

उत्तर

40

पुस्तकालय:

जीसीसी test.c -ओ परीक्षण -lrt

GCC Link Options से:

 
-llibrary 
-l library 
    Search the library named library when linking. 
    (The second alternative with the library as a separate argument 
    is only for POSIX compliance and is not recommended.) 

    It makes a difference where in the command you write this option; 
    the linker searches and processes libraries and object files in the 
    order they are specified. 
    Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but 
    before bar.o. If bar.o refers to functions in `z', those functions 
    may not be loaded. 
+1

मुझे लगता है कि मध्य में कहीं भी पुस्तकालय जोड़ना भी काम करता है। बस उत्सुक क्यों 'जीसीसी' आदेश इतना महत्वपूर्ण मानता है? –

+0

मैं इतना बेवकूफ था ... धन्यवाद। – bxshi

+1

इस बार मुझे इस gcc कमांड के अंत में lib जोड़ने की आवश्यकता है, लेकिन इससे पहले कि मैं अपने लिनक्स को पुनर्स्थापित कर दूं, मैं 'gcc' के बाद lib जोड़ सकता हूं और यह संकलित भी हो सकता है। ऐसा कैसे होता है? क्या इसके बारे में कोई कॉन्फ़िगरेशन फ़ाइल या चर है? – bxshi

3

बदलें

gcc -lrt test.c -o test 
से संकलन लाइन

को
gcc test.c -o test -lrt 
+0

क्या जीसीसी को हमेशा अंत में lib जोड़ने की आवश्यकता है?मैंने अपना कोड और मेकफ़ाइल नहीं बदला, लेकिन इससे पहले कि मैं अपने कंप्यूटर को पुनर्स्थापित करूँ, यह ठीक काम करता है। – bxshi

4

में Expert C programming पृष्ठ 108: <Handy Heuristic> Where to Put Library Options:Always put the -l library options at the rightmost end of your compilation command line. लेकिन यह क्यों बता नहीं है, तो मैं लगता है कि यह कुछ हद तक एक नियम है :)

संबंधित मुद्दे