emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.78k stars 3.3k forks source link

"No thread API" error on C++ compile (macOS) #13318

Open parksj10 opened 3 years ago

parksj10 commented 3 years ago

Trying to create simple .wasm file for use in Javascript (similar to something I've done with C based emcc project) and getting the following error when I attempt to compile.

Any thoughts much appreciated!

Trace

 "/usr/local/opt/emscripten/libexec/llvm/bin/clang++" -DEMSCRIPTEN -fignore-exceptions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -Xclang -isystem/usr/local/Cellar/emscripten/2.0.12/libexec/system/include/SDL -target wasm32-unknown-emscripten -D__EMSCRIPTEN_major__=2 -D__EMSCRIPTEN_minor__=0 -D__EMSCRIPTEN_tiny__=12 -D_LIBCPP_ABI_VERSION=2 -Dunix -D__unix -D__unix__ -Werror=implicit-function-declaration --sysroot=/usr/local/Cellar/emscripten/2.0.12/libexec/system -Xclang -isystem/usr/local/Cellar/emscripten/2.0.12/libexec/cache/include -Xclang -iwithsysroot/include/libcxx -Xclang -iwithsysroot/lib/libcxxabi/include -Xclang -iwithsysroot/include/compat -Xclang -iwithsysroot/include/libc -Xclang -iwithsysroot/lib/libc/musl/arch/emscripten -Xclang -iwithsysroot/local/include -Xclang -iwithsysroot/include/SSE -Xclang -iwithsysroot/include/neon -Xclang -iwithsysroot/lib/compiler-rt/include -Xclang -iwithsysroot/lib/libunwind/include --target=wasm32-unknown-unknown-wasm --optimize=3 -v ./lttb.cpp -c -o /var/folders/xj/yxtdtt1n42l5m_6pxn_1yspw0000gn/T/emscripten_temp_ukq5xqc_/lttb_0.o
clang version 12.0.0 (https://github.com/llvm/llvm-project.git 52e240a0721e4120a7143f6f5bab4760d28d48e8)
Target: wasm32-unknown-unknown-wasm
Thread model: posix
InstalledDir: /usr/local/opt/emscripten/libexec/llvm/bin
 (in-process)
 "/usr/local/Cellar/emscripten/2.0.12/libexec/llvm/bin/clang-12" -cc1 -triple wasm32-unknown-unknown-wasm -emit-obj --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lttb.cpp -mrelocation-model static -mframe-pointer=none -fno-rounding-math -mconstructor-aliases -target-cpu generic -fvisibility hidden -debugger-tuning=gdb -target-linker-version 609.8 -v -resource-dir /usr/local/Cellar/emscripten/2.0.12/libexec/llvm/lib/clang/12.0.0 -D EMSCRIPTEN -D __EMSCRIPTEN_major__=2 -D __EMSCRIPTEN_minor__=0 -D __EMSCRIPTEN_tiny__=12 -D _LIBCPP_ABI_VERSION=2 -D unix -D __unix -D __unix__ -isysroot /usr/local/Cellar/emscripten/2.0.12/libexec/system -internal-isystem /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/c++/v1 -internal-isystem /usr/local/Cellar/emscripten/2.0.12/libexec/llvm/lib/clang/12.0.0/include -internal-isystem /usr/local/Cellar/emscripten/2.0.12/libexec/system/include -O3 -Werror=implicit-function-declaration -fdeprecated-macro -fdebug-compilation-dir /Users/josh/Code/js/idv/app/wasm/cpp -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics -vectorize-loops -vectorize-slp -isystem/usr/local/Cellar/emscripten/2.0.12/libexec/system/include/SDL -isystem/usr/local/Cellar/emscripten/2.0.12/libexec/cache/include -iwithsysroot/include/libcxx -iwithsysroot/lib/libcxxabi/include -iwithsysroot/include/compat -iwithsysroot/include/libc -iwithsysroot/lib/libc/musl/arch/emscripten -iwithsysroot/local/include -iwithsysroot/include/SSE -iwithsysroot/include/neon -iwithsysroot/lib/compiler-rt/include -iwithsysroot/lib/libunwind/include -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/xj/yxtdtt1n42l5m_6pxn_1yspw0000gn/T/emscripten_temp_ukq5xqc_/lttb_0.o -x c++ ./lttb.cpp
clang -cc1 version 12.0.0 based upon LLVM 12.0.0git default target x86_64-apple-darwin19.5.0
ignoring nonexistent directory "/usr/local/Cellar/emscripten/2.0.12/libexec/cache/include"
ignoring nonexistent directory "/usr/local/Cellar/emscripten/2.0.12/libexec/system/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/SDL
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/libcxx
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/lib/libcxxabi/include
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/compat
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/libc
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/lib/libc/musl/arch/emscripten
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/local/include
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/SSE
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/neon
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/lib/compiler-rt/include
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/lib/libunwind/include
 /usr/local/Cellar/emscripten/2.0.12/libexec/llvm/lib/clang/12.0.0/include
 /usr/local/Cellar/emscripten/2.0.12/libexec/system/include
End of search list.
In file included from ./lttb.cpp:3:
In file included from /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/emscripten/bind.h:14:
In file included from /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/libcxx/stddef.h:39:
/usr/local/Cellar/emscripten/2.0.12/libexec/system/include/libcxx/__config:1134:6: error: "No thread API"
#    error "No thread API"
     ^
1 error generated.
emcc: error: '/usr/local/opt/emscripten/libexec/llvm/bin/clang++ -DEMSCRIPTEN -fignore-exceptions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -Xclang -isystem/usr/local/Cellar/emscripten/2.0.12/libexec/system/include/SDL -target wasm32-unknown-emscripten -D__EMSCRIPTEN_major__=2 -D__EMSCRIPTEN_minor__=0 -D__EMSCRIPTEN_tiny__=12 -D_LIBCPP_ABI_VERSION=2 -Dunix -D__unix -D__unix__ -Werror=implicit-function-declaration --sysroot=/usr/local/Cellar/emscripten/2.0.12/libexec/system -Xclang -isystem/usr/local/Cellar/emscripten/2.0.12/libexec/cache/include -Xclang -iwithsysroot/include/libcxx -Xclang -iwithsysroot/lib/libcxxabi/include -Xclang -iwithsysroot/include/compat -Xclang -iwithsysroot/include/libc -Xclang -iwithsysroot/lib/libc/musl/arch/emscripten -Xclang -iwithsysroot/local/include -Xclang -iwithsysroot/include/SSE -Xclang -iwithsysroot/include/neon -Xclang -iwithsysroot/lib/compiler-rt/include -Xclang -iwithsysroot/lib/libunwind/include --target=wasm32-unknown-unknown-wasm --optimize=3 -v ./lttb.cpp -c -o /var/folders/xj/yxtdtt1n42l5m_6pxn_1yspw0000gn/T/emscripten_temp_ukq5xqc_/lttb_0.o' failed (1)

lltb.c

// quick_example.cpp
#undef __APPLE__ // https://github.com/microsoft/vscode-cpptools/issues/1083#issuecomment-338499236
#include <emscripten/bind.h>
#include "lttb.hpp"

using namespace emscripten;

struct DataPoint
{
  float x;
  float y;
};

void readData(DataPoint in[], int data_length, float *x, float *y)
{
  for (int i = 0; i < data_length; i++)
  {
    in[i].x = x[i];
    in[i].y = y[i];
  }
}

void lttb(float *x, float *y, int data_length, int max_points, float *downsampled_x, float *downsampled_y)
{
  using PointLttb = LargestTriangleThreeBuckets<DataPoint, float, &DataPoint::x, &DataPoint::y>;

  DataPoint in[data_length];
  readData(in, data_length, x, y);
  DataPoint out[max_points];
  PointLttb::Downsample(in, data_length, out, max_points);
  *downsampled_x = out[0].x;
  *downsampled_y = out[0].y;
  return;
}

EMSCRIPTEN_BINDINGS(lttb_module)
{
  function("lttb", &lttb, allow_raw_pointers());
}

lttb.h

#ifndef __LTTP_HPP__
#define __LTTP_HPP__

#include <cstddef>
#include <cmath>
#include <vector>
#include <algorithm>

template <typename TPoint, typename TData, TData TPoint::*x, TData TPoint::*y>
struct LargestTriangleThreeBuckets
{
  template <typename InputIt, typename OutputIt>
  static void Downsample(InputIt source, size_t sourceSize, OutputIt destination, size_t destinationSize)
  {
    if (destinationSize == 0)
    {
      return;
    }

    if (destinationSize >= sourceSize)
    {
      std::copy_n(source, sourceSize, destination);
    }

    *destination = *source;
    ++destination;

    TData every = static_cast<TData>(sourceSize - 2) / (destinationSize - 2);
    size_t aIndex = 0;

    for (size_t i = 0; i < destinationSize - 2; ++i)
    {
      size_t avgRangeStart = static_cast<size_t>((i + 1) * every) + 1;
      size_t avgRangeEnd = static_cast<size_t>((i + 2) * every) + 1;
      if (avgRangeEnd > sourceSize)
      {
        avgRangeEnd = sourceSize;
      }

      TData avgRangeLength = avgRangeEnd - avgRangeStart;
      TData avgX = 0;
      TData avgY = 0;
      for (; avgRangeStart < avgRangeEnd; ++avgRangeStart)
      {
        avgX += source[avgRangeStart].*x;
        avgY += source[avgRangeStart].*y;
      }
      avgX /= avgRangeLength;
      avgY /= avgRangeLength;

      size_t rangeOffs = static_cast<size_t>(i * every) + 1;
      size_t rangeTo = static_cast<size_t>((i + 1) * every) + 1;
      TData pointAX = source[aIndex].*x;
      TData pointAY = source[aIndex].*y;

      TData maxArea = -1;
      size_t nextAIndex = 0;
      for (; rangeOffs < rangeTo; ++rangeOffs)
      {
        TData area = std::abs(
                         (pointAX - avgX) * (source[rangeOffs].*y - pointAY) -
                         (pointAX - source[rangeOffs].*x) * (avgY - pointAY)) /
                     2;
        if (area > maxArea)
        {
          maxArea = area;
          nextAIndex = rangeOffs;
        }
      }

      *destination = source[nextAIndex];
      ++destination;
      aIndex = nextAIndex;
    }

    *destination = source[sourceSize - 1];
    ++destination;
  }
};

#endif

Compile:

emcc ./lttb.cpp \
  --target=wasm32-unknown-unknown-wasm \
  --optimize=3 \
  -v \
  -nostdlib \
  -Wl,--export-all \
  -Wl,--no-entry \
  -Wl,--allow-undefined \
  -s ALLOW_MEMORY_GROWTH=1 \
  -o lttb.wasm

Environment macOS: 10.15.5 clang++: Apple clang version 12.0.0 (clang-1200.0.32.21) Target: x86_64-apple-darwin19.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin emcc: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.12 clang version 12.0.0 (https://github.com/llvm/llvm-project.git 52e240a0721e4120a7143f6f5bab4760d28d48e8) Target: wasm32-unknown-emscripten Thread model: posix InstalledDir: /usr/local/opt/emscripten/libexec/llvm/bin

Related Issues: https://github.com/emscripten-core/emscripten/issues/8566 https://github.com/emscripten-core/emscripten/issues/8569

parksj10 commented 3 years ago

Removing --target=wasm32-unknown-unknown-wasm \ from the compile arguments (carryover from a C compile I did separately) gives the following error:

error: undefined symbol: memmove (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: _memmove 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.12/libexec/src/compiler.js /var/folders/xj/yxtdtt1n42l5m_6pxn_1yspw0000gn/T/tmpamj_5evr.txt' failed (1)

And I can suppress this error with -s ERROR_ON_UNDEFINED_SYMBOLS=0, however, not sure if this is suppressing an actual error...

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.