2017-02-11 68 views
1

我正在研究C++應用程序。我想在應用程序中嵌入SpiderMonkey。嵌入SpiderMonkey JS

我正在與CMake合作,但我無法獲得該構建。所以,爲了減少併發症,我嘗試了這個page上的示例代碼。這不會在命令行中使用cmake或gcc鏈接。

因此,更簡單,只是爲了確保我可以正確鏈接我試圖讓以下工作。 從用gcc命令行:

g++ --std=c++11 
    -I/home/thetasinner/moz/js/src/build_DBG.OBJ/dist/include  
    -L/home/thetasinner/moz/js/src/build_DBG.OBJ/js/src -DDEBUG 
    -Wl,--verbose -lmozjs-54a1 -lm -lz -ldl test.cpp -o test 

在下面的最小代碼例如:

#include <iostream> 
#include <stdexcept> 
#include "jsapi.h" 
#include "js/Initialization.h" 

int main(int argc, char** args) { 
    if (!JS_Init()) { 
    throw std::runtime_error("failed to initialise."); 
    } 

    std::cout << "It's alive!\n"; 

    JS_ShutDown(); 
    return 0; 
} 

即使這沒有鏈接。我得到的錯誤

/tmp/ccqjx5RY.o: In function `main': 
    custom.cpp:(.text+0xf2): undefined reference to `JS_ShutDown()' 
/tmp/ccqjx5RY.o: In function `JS_Init()': 
    custom.cpp:(.text._Z7JS_Initv[_Z7JS_Initv]+0xa): undefined reference to 
     'JS::detail::InitWithFailureDiagnostic(bool)' 
collect2: error: ld returned 1 exit status 

標題是發現和連接器是找到mozjs庫

attempt to open /home/thetasinner/moz/js/src/custom_build_DBG.OBJ/js/src 
    /libmozjs-54a1.so succeeded 
-lmozjs-54a1 (/home/thetasinner/moz/js/src/custom_build_DBG.OBJ/js/src 
    /libmozjs-54a1.so) 

我的工作在Linux(Ubuntu的16.04和Debian 8.7試過),因爲那是構建工具。我甚至不想觸摸Window的。

構建在spidermonkey構建中的'js'可執行文件正常工作,我假設它有我試圖鏈接它的lib。所以我會認爲lib本身是可以構建的。

任何人都可以幫我解決這些鏈接器錯誤嗎?關於SpiderMonkey的舊版本的問題有很多答案,但對於更新的版本沒有什麼。我對版本45(我已經嘗試過非常類似的錯誤)或最新版本52感興趣。 我很熟悉在代碼中挖掘一下如何在構建時如何執行我想要的操作,因此對未正確記錄的最新版本的興趣,我完全被建築步驟所困擾。

回答

1

我懷疑它只是在命令行上一個順序的問題:

g++ --std=c++11 
-I/home/thetasinner/moz/js/src/build_DBG.OBJ/dist/include  
-L/home/thetasinner/moz/js/src/build_DBG.OBJ/js/src -DDEBUG 
test.cpp -o test 
-Wl,--verbose -lmozjs-54a1 -lm -lz -ldl 

彙編第一,其次是依賴的順序連接,與圖書館。 (我的第一個猜測是你忽略了在命令行上提及mozjs,但第二次看到它只是在錯誤的地方。)

+0

這對我有效。不能相信這是一個愚蠢的錯誤!使用版本54a1啓動可執行文件時出現問題,但與版本45完全兼容 –