19

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.

Community
  • 1
  • 1
arthur.sw
  • 11,052
  • 9
  • 47
  • 104
  • Can you provide your source on a git or something else? Does it help to set the endianess manual with `-mbig-endian` or `-mlittle-endian` – Alex44 Sep 03 '15 at 19:49
  • 1
    You should define the Windows architecture and the target processor. Adding -D_WIN64 and -D_M_X64 as compiler options should help. – Vink Sep 04 '15 at 01:29
  • Probably, my problem is that I don't know to which .lib I should link. – arthur.sw Sep 05 '15 at 09:02
  • The jscpucfg.h file has the ability to define certain things depending on the architecture you want to use the library on. But you have to add the options @Vink specified so that jscpucfg.h can determine processor, register size, endianness etc from those. – cardiff space man Sep 08 '15 at 00:59
  • Also, if you got those errors then you did not succeed. Also my guess is that if you have libs that do not begin with either 'lib' or 'cyg', you should be using the Microsoft compiler and linker instead of g++. – cardiff space man Sep 08 '15 at 01:07
  • BTW Cygwin has a package called libmozjs185_1.0-1.0.0-4 which is suitable for embedding JavaScript in a program. – cardiff space man Oct 02 '15 at 19:31
  • @cardiffspaceman I managed to use the libmozjs185 package to link against spidermonkey, I can accept this as an answer if you want (just make another answer). Can you also detail (give some commands?) your first answer (with js-config) now that I edited my question with more info? – arthur.sw Oct 05 '15 at 10:25

2 Answers2

3

The idea behind the work around is to run js-config --libs and put the result in JSAPI_LD_FLAGS, possibly filter out things on Darwin, and then append JSAPI_LD_FLAGS to your LDFLAGS so you can link the right libraries.

So for your library question, the answer is to get js-config built and then run it with --libs

Likewise, you would create your CFLAGS using a combination of the CFLAGS you need already and the output of js-config --cflags . This is something you may have already found yourself doing with the nifty pkg-config utility for other libraries.

This doesn't solve the endianness problem. Why don't you just run the configure script?

cardiff space man
  • 1,442
  • 14
  • 31
  • Do you have any alternatives? – Ryan Fung Oct 02 '15 at 01:26
  • My experience hasn't taught me any alternative to running configure where spidermonkey is concerned. On Cygwin I get a preconfigured package and that suits me. A long time ago I used a source tarball with MSDEV but I'm pretty sure I followed the directions explicitly in that case. – cardiff space man Oct 02 '15 at 19:19
  • Ok, I edited my question to give more info about my js-config problem. Does it help to detail this answer? – arthur.sw Oct 05 '15 at 10:26
3

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).

cardiff space man
  • 1,442
  • 14
  • 31