2012-04-18 36 views
8

Được rồi, vì vậy tôi đã tải xuống mã nguồn SpiderMonkey bằng cách sử dụng lệnh wget http://ftp.mozilla.org/pub/mozilla.org/js/js185-1.0.0.tar.gz và trích xuất nó. Sau đó, tôi đã xây dựng thành công bao gồm các tập tin và thư viện tĩnh bằng cách thực hiện các lệnh sau:Lỗi khi biên dịch chương trình SpiderMonkey được nhúng

  1. autoconf2.13
  2. ./configure --prefix=~/js --disable-shared-js
  3. make
  4. make install

Bây giờ tôi cố gắng biên soạn đoạn mã sau sử dụng command g++ -I/home/aaditmshah/js/include/js -L/home/aaditmshah/js/lib -lmozjs185-1.0 -ldl -lm -ldl helloworld.cpp -o helloworld:

/* 
* This define is for Windows only, it is a work-around for bug 661663. 
*/ 
#ifdef _MSC_VER 
# define XP_WIN 
#endif 

/* Include the JSAPI header file to get access to SpiderMonkey. */ 
#include "jsapi.h" 

/* The class of the global object. */ 
static JSClass global_class = { 
    "global", JSCLASS_GLOBAL_FLAGS, 
    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, 
    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, 
    JSCLASS_NO_OPTIONAL_MEMBERS 
}; 

/* The error reporter callback. */ 
void reportError(JSContext *cx, const char *message, JSErrorReport *report) 
{ 
    fprintf(stderr, "%s:%u:%s\n", 
      report->filename ? report->filename : "<no filename=\"filename\">", 
      (unsigned int) report->lineno, 
      message); 
} 

int main(int argc, const char *argv[]) 
{ 
    /* JSAPI variables. */ 
    JSRuntime *rt; 
    JSContext *cx; 
    JSObject *global; 

    /* Create a JS runtime. You always need at least one runtime per process. */ 
    rt = JS_NewRuntime(8 * 1024 * 1024); 
    if (rt == NULL) 
     return 1; 

    /* 
    * Create a context. You always need a context per thread. 
    * Note that this program is not multi-threaded. 
    */ 
    cx = JS_NewContext(rt, 8192); 
    if (cx == NULL) 
     return 1; 
    JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT); 
    JS_SetVersion(cx, JSVERSION_LATEST); 
    JS_SetErrorReporter(cx, reportError); 

    /* 
    * Create the global object in a new compartment. 
    * You always need a global object per context. 
    */ 
    global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); 
    if (global == NULL) 
     return 1; 

    /* 
    * Populate the global object with the standard JavaScript 
    * function and object classes, such as Object, Array, Date. 
    */ 
    if (!JS_InitStandardClasses(cx, global)) 
     return 1; 

    /* Your application code here. This may include JSAPI calls 
    * to create your own custom JavaScript objects and to run scripts. 
    * 
    * The following example code creates a literal JavaScript script, 
    * evaluates it, and prints the result to stdout. 
    * 
    * Errors are conventionally saved in a JSBool variable named ok. 
    */ 
    char *script = "'Hello ' + 'World!'"; 
    jsval rval; 
    JSString *str; 
    JSBool ok; 
    const char *filename = "noname"; 
    uintN lineno = 0; 

    ok = JS_EvaluateScript(cx, global, script, strlen(script), 
          filename, lineno, &rval); 
    if (rval == NULL | rval == JS_FALSE) 
     return 1; 

    str = JS_ValueToString(cx, rval); 
    printf("%s\n", JS_EncodeString(cx, str)); 

    /* End of your application code */ 

    /* Clean things up and shut down SpiderMonkey. */ 
    JS_DestroyContext(cx); 
    JS_DestroyRuntime(rt); 
    JS_ShutDown(); 
    return 0; 
} 

Tôi biết rằng các tùy chọn includelinker trỏ tới các thư mục chính xác. Các tệp bao gồm nằm trong /home/aaditmshah/js/include/js và thư viện tĩnh có tên libmozjs185-1.0 nằm trong số /home/aaditmshah/js/lib. Tuy nhiên, nó vẫn tạo ra lỗi sau:

helloworld.cpp: In function ‘int main(int, const char**)’: 
helloworld.cpp:74:20: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] 
helloworld.cpp:83:17: warning: NULL used in arithmetic [-Wpointer-arith] 
/tmp/ccUU9may.o: In function `main': 
helloworld.cpp:(.text+0x6e): undefined reference to `JS_Init' 
helloworld.cpp:(.text+0x94): undefined reference to `JS_NewContext' 
helloworld.cpp:(.text+0xba): undefined reference to `JS_SetOptions' 
helloworld.cpp:(.text+0xcb): undefined reference to `JS_SetVersion' 
helloworld.cpp:(.text+0xdc): undefined reference to `JS_SetErrorReporter' 
helloworld.cpp:(.text+0xf2): undefined reference to `JS_NewCompartmentAndGlobalObject' 
helloworld.cpp:(.text+0x11a): undefined reference to `JS_InitStandardClasses' 
helloworld.cpp:(.text+0x191): undefined reference to `JS_EvaluateScript' 
helloworld.cpp:(.text+0x1c8): undefined reference to `JS_ValueToString' 
helloworld.cpp:(.text+0x1df): undefined reference to `JS_EncodeString' 
helloworld.cpp:(.text+0x1f3): undefined reference to `JS_DestroyContext' 
helloworld.cpp:(.text+0x1ff): undefined reference to `JS_Finish' 
helloworld.cpp:(.text+0x204): undefined reference to `JS_ShutDown' 
/tmp/ccUU9may.o:(.data+0x10): undefined reference to `JS_PropertyStub' 
/tmp/ccUU9may.o:(.data+0x18): undefined reference to `JS_PropertyStub' 
/tmp/ccUU9may.o:(.data+0x20): undefined reference to `JS_PropertyStub' 
/tmp/ccUU9may.o:(.data+0x28): undefined reference to `JS_StrictPropertyStub' 
/tmp/ccUU9may.o:(.data+0x30): undefined reference to `JS_EnumerateStub' 
/tmp/ccUU9may.o:(.data+0x38): undefined reference to `JS_ResolveStub' 
/tmp/ccUU9may.o:(.data+0x40): undefined reference to `JS_ConvertStub' 
/tmp/ccUU9may.o:(.data+0x48): undefined reference to `JS_FinalizeStub' 
collect2: ld returned 1 exit status 

Tôi tự hỏi mình đang thiếu những tệp nào. Tôi có nên xây dựng lại SpiderMonkey và cài đặt nó trong /usr/local? Chúng tôi rất trân trọng bất kỳ sự giúp đỡ nào.

Tôi khá chắc chắn rằng vấn đề không phải là tôi đang sử dụng thư viện tĩnh. Tôi xây dựng lại SpiderMonkey như một thư viện đối tượng được chia sẻ và nó vẫn cho tôi lỗi tương tự.

+1

Tôi thực sự xin lỗi khi nói điều này, nhưng điều đó làm việc tốt cho tôi. Tôi nhận được cả hai cảnh báo C + +, nhưng không có lỗi liên kết. Kết quả thực thi chạy tốt. Tôi đang ở trên máy Mac. Bạn có thể muốn xem kết quả của 'nm libmozjs185-1.0.a | grep JS_NewContext'. Tôi nhận được một dòng có nội dung '00000000000016d0 T _JS_NewContext'. –

+0

Tôi nhận được một dòng có nội dung '0000000000003500 T JS_NewContext', mà tôi giả sử có nghĩa là' JS_NewContext' có giá trị là '13568'. Tôi không chắc chắn những gì để làm cho đầu ra này. –

Trả lời

1

Bạn có thể thử này:

g++ -I/home/aaditmshah/js/include/js /home/aaditmshah/js/lib/mozjs185-1.0.a -lm -ldl helloworld.cpp -o helloworld 

tức. thêm tệp lưu trữ trực tiếp vào dòng lệnh g ++. Bạn có thể cần phải xác định một số kết hợp của những điều này nếu nó không hoạt động. (Với gcc 4.2, điều này có hiệu quả đối với tôi):

-static -lmozjs185 

Chỉ định đường dẫn chính xác cho mozjs185-1.0.a.

+0

Không, nó vẫn không hoạt động. Các tùy chọn '-lm' và' -ldl' có nghĩa là gì? Tôi đang sử dụng phiên bản g ++ 'g ++ - 4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1'. –

0

Trên Ubuntu 12.10, phiên bản g ++ phiên bản gcc 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) và SipderMonkey v1.8.5: Lệnh là sudo g++ -o helloworld -I /usr/local/include/js helloworld.cpp -lmozjs185-1.0 -L /usr/local/lib -lpthread Dành nhiều thời gian hơn dự kiến ​​để làm điều gì đó thật đơn giản như vậy! Không, cảm ơn FireFox/Mozilla.

Các vấn đề liên quan