2

I am converting a large C++ application for run in a webassembly environment. The build is organized into modules and each module sub-project creates a static or a dynamic library.

From what I have read there is no "ABI" yet standardized for "wasm" modules for dynamic-linking into the browser runtime engine, so at present dynamic linking of pre-compiled and linked libraries is not yet supported.

But in the case of static libraries they are just collections of pre-compiled "intermediate" "object" code which are converted to distribution code in the final "link" phase. I have the compiler set to create ".bc" "object code" from the authored languange.

I have not found any reference to creating a simple archive of the compiler output that can be fed to the final "link" that is supported by the emcc compiler/linker.

So a strategy is I can create zip files (kinda like jar files) with a simple manifest and the build system can unpack them all into a temporary area, and merge the exports files. then "link" the whole mass together into the browser "distributable" Module.

Is there a nascent standard definition for this sort of thing? If so I should support it.

peterk
  • 5,136
  • 6
  • 33
  • 47

1 Answers1

3

You should be able to use emar just like a the standard ar tool in order to create libraries from the .bc object files.

You can then feed these into em++ (or emcc) at link time and they will work much like native libraries do with native compilers. You can with reference them directly on the command line or with -L/-l arguments.

sbc100
  • 2,619
  • 14
  • 11
  • Yes seems to be the way. If I have a bunch of ".bc" files generated by the compile tasks ( they compile one at a time because the build has dependency checking and it calls the compiler only for those files that are out of date ) Is there a command line sample of how one calls emcc to take a list of already compiled '.bc' files and then output a final fat '.bc' file containing all the sources ? – peterk Nov 10 '18 at 17:24
  • will check ut the emar tool. – peterk Nov 10 '18 at 17:27
  • emar rcu "${LibDir}/a-library.bc" ${ObjFiles} does the trick – peterk Nov 11 '18 at 07:55
  • it does work for creating a .bc file which when "emar v libname" shows the index of the '.bc' files added to it. BUT emcc -L/path/to/lib -la-library.bc gives an error emcc: cant find library "a-library.bc" . I've tried a bunch of variants ising .a suffix .o suffix, .bc suffix, and using the prefix lib_ etc to no avail. – peterk Nov 11 '18 at 23:26
  • If I add it as an "object file" without the -L/path -lfile it is "found". – peterk Nov 11 '18 at 23:49
  • If you want to use the `-l` flag you need to name you library in form of `lib.a`.. then pass `-l` where is the name of your library. – sbc100 Nov 12 '18 at 17:28
  • Ok - will try it – peterk Nov 14 '18 at 04:43
  • well it does work somewhat but I find I can't export extern "C" { function() {} } items when "linking" the main application module. The C++ code has access to everything in the libraries, and I must place the libraries in order of most dependent to least dependent, or symbols are not pulled out of the libraries and resolved. if I put symbols in the libraries in an exports file used in the final link the exported symbols are NOT found. – peterk Nov 17 '18 at 21:24
  • This is expected behavior for a UNIX-like linker. See https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc. You might want to look at the `--while-archive` and `--start-group` option if you want more control over this behaviour. – sbc100 Nov 19 '18 at 18:18