amoeba / netcdf-wasm

1 stars 1 forks source link

Figure out build issue #2

Open amoeba opened 3 years ago

amoeba commented 3 years ago

When building a simple test program:

#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
#include "emscripten.h"

EMSCRIPTEN_KEEPALIVE
int main() {
  int ncid;
  int retval;

  // Just check symbols from netcdf.h work...
  NC_INT;

  // Try nc_create for now for testing/debugging
  retval = nc_create("simple_xy.nc", NC_CLOBBER, &ncid);

  return retval;
}

I get:

; emcc -O3 -s WASM=1 -I /usr/local/Cellar/curl/*/include -I /usr/local/Cellar/hdf5/*/include -I /usr/local/Cellar/netcdf/*/include src/main.c
error: undefined symbol: nc_create (referenced by top-level compiled C/C++ code)
warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
warning: _nc_create may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
emcc: error: '/usr/local/bin/node /usr/local/Cellar/emscripten/2.0.31/libexec/src/compiler.js /var/folders/jy/zkmn3bgj13710pnytbgh27800000gn/T/tmphlo1gyfc.txt' failed (returned 1)

If I follow the advice to link with -s LLD_REPORT_UNDEFINED, I get:

; emcc -O3 -s WASM=1 -s LLD_REPORT_UNDEFINED -I /usr/local/Cellar/curl/*/include -I /usr/local/Cellar/hdf5/*/include -I /usr/local/Cellar/netcdf/*/include src/main.c
wasm-ld: error: /var/folders/jy/zkmn3bgj13710pnytbgh27800000gn/T/emscripten_temp_k08ig8nq/main_0.o: undefined symbol: nc_create
emcc: error: '/usr/local/opt/emscripten/libexec/llvm/bin/wasm-ld -o a.out.wasm /var/folders/jy/zkmn3bgj13710pnytbgh27800000gn/T/emscripten_temp_k08ig8nq/main_0.o -L/usr/local/Cellar/emscripten/2.0.31/libexec/cache/sysroot/lib/wasm32-emscripten -lGL -lal -lhtml5 -lc -lcompiler_rt -lc++-noexcept -lc++abi-noexcept -ldlmalloc -lc_rt_wasm -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --allow-undefined-file=/var/folders/jy/zkmn3bgj13710pnytbgh27800000gn/T/tmpgqll_j4s.undefined --strip-debug --export-if-defined=main --export-if-defined=stackSave --export-if-defined=stackRestore --export-if-defined=stackAlloc --export-if-defined=__wasm_call_ctors --export-if-defined=__errno_location --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-table -z stack-size=5242880 --initial-memory=16777216 --entry=main --max-memory=16777216 --global-base=1024' failed (returned 1)

The first thing that pops out at me is that nc_open is extern'd:

// ...snip...
#define MSC_EXTRA  /**< Needed for DLL build. */
#endif  /* defined(DLL_NETCDF) */
#define EXTERNL MSC_EXTRA extern /**< Needed for DLL build. */
// ...snip...
EXTERNL int
nc_create(const char *path, int cmode, int *ncidp);
amoeba commented 3 years ago

This probably isn't an emscripten thing. Compilation fails with this method with just a regular old compiler:

; cat src/main_nowasm.c
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>

int main()
{
  int ncid;
  int retval;

  // Just check symbols from netcdf.h work...
  NC_INT;

  // Try nc_create for now for testing/debugging
  retval = nc_create("simple_xy.nc", NC_CLOBBER, &ncid);

  return retval;
}
~/src/netcdf-wasm main
; cc -v -O3  -I /usr/local/Cellar/curl/*/include -I /usr/local/Cellar/hdf5/*/include -I /usr/local/Cellar/netcdf/*/include src/main_nowasm.c
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx11.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main_nowasm.c -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-strict-return -fno-rounding-math -munwind-tables -target-sdk-version=11.3 -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -debugger-tuning=lldb -target-linker-version 711 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I /usr/local/Cellar/curl/7.79.1/include -I /usr/local/Cellar/hdf5/1.12.1/include -I /usr/local/Cellar/netcdf/4.8.0_2/include -I/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O3 -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -fdebug-compilation-dir /Users/bryce/src/netcdf-wasm -ferror-limit 19 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fmax-type-align=16 -fcommon -fcolor-diagnostics -vectorize-loops -vectorize-slp -clang-vendor-feature=+nullptrToBoolConversion -clang-vendor-feature=+messageToSelfInClassMethodIdReturnType -clang-vendor-feature=+disableInferNewAvailabilityFromInit -clang-vendor-feature=+disableNeonImmediateRangeCheck -clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+revert09abecef7bbf -mllvm -disable-aligned-alloc-awareness=1 -mllvm -enable-dse-memoryssa=0 -o /var/folders/jy/zkmn3bgj13710pnytbgh27800000gn/T/main_nowasm-ee8289.o -x c src/main_nowasm.c
clang -cc1 version 13.0.0 (clang-1300.0.29.3) default target x86_64-apple-darwin20.6.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/Cellar/curl/7.79.1/include
 /usr/local/Cellar/hdf5/1.12.1/include
 /usr/local/Cellar/netcdf/4.8.0_2/include
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -platform_version macos 11.0.0 11.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o a.out -L/usr/local/lib /var/folders/jy/zkmn3bgj13710pnytbgh27800000gn/T/main_nowasm-ee8289.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "_nc_create", referenced from:
      _main in main_nowasm-ee8289.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)