Search code examples
c++compilationspidermonkey

Linking a C++ program with SpiderMonkey?


I successfully compiled spidermonkey (on windows), how can I link against it now (to embed it)?

js-config is not properly installed, and I don't understand this workaround.

Linking to the static library should be easier, but I don't even know which file it is. I have mozglue.lib, mozjs-43a1.lib, nspr4.lib, plc4.lib, plds4.lib in dist/sdk/lib and nspr4.lib, plc4.lib, plds4.lib in dist/lib.

Update

js-config was not working because I had this problem (-bash: '\r': command not found because of Windows/Unix newline characters problem, I ran dos2unix js-config and I could run it).

However, the output does not help (on windows):

$ ./js-config --cflags -std=gnu++0x -include /usr/local/include/mozjs-43a1/js/RequiredDefines.h -I/usr/local/include/mozjs-43a1 -Ic:/Users/Yvain/Documents/mozilla-central/js/src/build_OPT.OBJ/dist/include/nspr

$ ./js-config --libs ${libdir}/${JS_LIBRARY_NAME}.lib c:/Users/Yvain/Documents/mozilla-central/js/src/build_OPT.OBJ/dist/lib/nspr4.lib c:/Users/Yvain/Documents/mozilla-central/js/src/build_OPT.OBJ/dist/lib/plc4.lib c:/Users/Yvain/Documents/mozilla-central/js/src/build_OPT.OBJ/dist/lib/plds4.lib kernel32.lib user32.lib gdi32.lib winmm.lib wsock32.lib advapi32.lib psapi.lib

Notes

I used the following command to compile:

g++ -std=c++11 -I<objdir>/dist/include -L<objdir>/dist/lib helloworld.cpp -o helloworld  -lmozjs-31 -lz -lpthread -ldl 

I know it is not the correct way to compile it since those libraries are not in <objdir>/dist/lib. It returns the following errors:

[...]/jscpucfg.h:121:3: erreur:
#error "Cannot determine endianness of your platform. Please add support to jscpucfg.h."
[...]
erreur: ‘JS_EvaluateScript’ was not declared in this scope

This question seems to draw some attention. Note that I asked the same question for V8.


Solution

  • The easy way to use spidermoney in Cygwin projects (some of your output suggests you are using Cygwin) is to use the libmozjs185 package that is available for Cygwin. My project is a bit complicated but a makefile would look something like this:

    CFLAGS += -g -I/usr/include/js -DXP_UNIX
    CXXFLAGS += -g -I/usr/include/js -DXP_UNIX
    JSLINK=-lmozjs185
    
    objs = <your .o files>
    
    <your app>: $(objs)
        g++ -g -o <your app> $(objs) \
        $(JSLINK) \
        $(NULL)
    

    If your app is only 'C' code change the g++ above to gcc. I have more libraries besides mozjs185 and I put them ahead of the $(NULL).