2013-02-08 16 views
7

के अनिर्धारित संदर्भ में मुझे GNU Readline लाइब्रेरी नमूना कोड available in wikipedia चलाने की कोशिश करने में समस्या आ रही है। यहाँ यह जाता है:'रीडलाइन'

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <readline/readline.h> 
#include <readline/history.h> 

int main() 
{ 
    char* input, shell_prompt[100]; 

    // Configure readline to auto-complete paths when the tab key is hit. 
    rl_bind_key('\t', rl_complete); 

    for(;;) { 
     // Create prompt string from user name and current working directory. 
     snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024)); 

     // Display prompt and read input (n.b. input must be freed after use)... 
     input = readline(shell_prompt); 

     // Check for EOF. 
     if (!input) 
      break; 

     // Add input to history. 
     add_history(input); 

     // Do stuff... 

     // Free input. 
     free(input); 
    } 
} 

मैं प्रतिबंधित वातावरण पर काम कर रहा हूँ थे ReadLine उपलब्ध नहीं था, इसलिए मैं सूत्रों डाउनलोड यह संकलन और मेरे घर निर्देशिका करने के लिए इसे स्थापित करने के लिए किया था।

ये मेरा घर निर्देशिका के अंदर संरचना होती है:

.local 
├── include 
│   └── readline 
│      ├── chardefs.h 
│      ├── history.h 
│      ├── keymaps.h 
│      ├── readline.h 
│      ├── rlconf.h 
│      ├── rlstdc.h 
│      ├── rltypedefs.h 
│      └── tilde.h 
└── lib 
   ├── libhistory.a 
   ├── libhistory.so -> libhistory.so.6 
   ├── libhistory.so.6 -> libhistory.so.6.2 
   ├── libhistory.so.6.2 
      ├── libreadline.a 
   ├── libreadline.so -> libreadline.so.6 
   ├── libreadline.so.6 -> libreadline.so.6.2 
   └── libreadline.so.6.2 

समस्या यह है, जब मैं जीसीसी फोन यह मुझे एक त्रुटि फेंकता है:

$ gcc hello.c -o hello_c.o -I /home/my-home-dir/.local/include 
/tmp/cckC236E.o: In function `main': 
hello.c:(.text+0xa): undefined reference to `rl_complete' 
hello.c:(.text+0x14): undefined reference to `rl_bind_key' 
hello.c:(.text+0x60): undefined reference to `readline' 
hello.c:(.text+0x77): undefined reference to `add_history' 
collect2: error: ld returned 1 exit status 

इस here के बारे में एक जवाब नहीं है, लेकिन मैं Netbeans का उपयोग नहीं कर रहा हूं और मुझे यकीन नहीं है कि कमांड लाइन पर लाइब्रेरी के पथ को कैसे निर्दिष्ट किया जाए।

मैं लिंकर जहां पुस्तकालय हैं बताने के लिए कोशिश की, लेकिन परिणाम अभी भी वही है:

$ gcc hello.c -o hello_c.o -I /home/my-home-dir/.local/include -Xlinker "-L /home/my-home-dir/.local/lib" 
/tmp/cceiePMr.o: In function `main': 
hello.c:(.text+0xa): undefined reference to `rl_complete' 
hello.c:(.text+0x14): undefined reference to `rl_bind_key' 
hello.c:(.text+0x60): undefined reference to `readline' 
hello.c:(.text+0x77): undefined reference to `add_history' 
collect2: error: ld returned 1 exit status 

कोई भी विचार क्या मैं यहाँ याद आ रही हो सकता है?

+1

आप पुस्तकालय लिंक करने की आवश्यकता में वास्तविक पुस्तकालय -lreadline का उपयोग कर againts लिंक करना होगा। स्थानीय/lib' पहले)। और '* .c' का उपयोग' * .c' प्राप्त करने के लिए 'gcc -Wall -c -g hello.c -o hello.o' –

+1

रीडलाइन और add_history के लिए उपयोग-संपादन – yet

उत्तर

12

आप तो अपने `gcc` आदेश (और शायद` एल के अंत के पास -lreadline` डाल `, जीसीसी तर्क

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