8

좋아요, 그래서 wget http://ftp.mozilla.org/pub/mozilla.org/js/js185-1.0.0.tar.gz 명령을 사용하여 SpiderMonkey 소스 코드를 다운로드하고 압축을 풉니 다. 지금은을 사용하여 다음 코드를 컴파일 시도포함 된 SpiderMonkey 프로그램을 컴파일하는 동안 오류가 발생했습니다.

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

: 그럼 성공적으로 다음 명령을 실행하여 파일과 정적 라이브러리를 포함 구축 명령 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; 
} 

includelinker 옵션이 올바른 디렉토리를 가리키는 것을 알고 있습니다. 포함 파일은 /home/aaditmshah/js/include/js이고 libmozjs185-1.0이라는 정적 라이브러리는 /home/aaditmshah/js/lib입니다. 그러나 여전히 다음과 같은 오류가 발생합니다.

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 

제가 누락 된 파일이 궁금합니다. SpiderMonkey를 다시 설치하고 /usr/local에 설치해야합니까? 어떤 도움이라도 대단히 감사하겠습니다.

저는 정적 라이브러리를 사용하고있는 것이 아니라고 확신합니다. SpiderMonkey를 공유 객체 라이브러리로 다시 작성했지만 여전히 동일한 오류가 발생했습니다.

+1

정말 대단히 죄송합니다.하지만 저에게는 잘되었습니다. C++ 경고 메시지는 모두 표시되지만 링커 오류는 표시되지 않습니다. 결과 실행 파일은 정상적으로 실행됩니다. 나는 Mac에있다. 'nm libmozjs185-1.0.a |의 출력을 보길 원할 것입니다. grep JS_NewContext'. '00000000000016d0 T _JS_NewContext'라는 줄이 생깁니다. –

+0

'0000000000003500 T JS_NewContext'라는 줄이 있는데, 이는'JS_NewContext'의 값이 '13568'이라는 것을 의미합니다. 이 출력물을 어떻게 만들지 모르겠습니다. –

답변

1

이 작업을 시도 할 수 있습니다 :

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

즉. g ++ 명령 행에 직접 아카이브를 추가하십시오. 작동하지 않는 경우 이들의 일부 조합을 지정해야 할 수도 있습니다. (GCC 4.2이 나를 위해 일) :

-static -lmozjs185 

그냥 mozjs185-1.0.a에 대한 올바른 경로를 지정합니다.

+0

아니요, 여전히 작동하지 않습니다. '-lm'과'-ldl' 옵션의 의미는 무엇입니까? g ++ 버전'g ++ - 4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1'을 사용하고 있습니다. –

0

우분투 12.10에서 g ++ 버전 gcc 버전 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) 및 SipderMonkey v1.8.5 : 명령은 sudo g++ -o helloworld -I /usr/local/include/js helloworld.cpp -lmozjs185-1.0 -L /usr/local/lib -lpthread입니다. FireFox/Mozilla 덕분에.

관련 문제