emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.65k stars 3.29k forks source link

unresolved symbols and vcglib #3272

Closed idini closed 9 years ago

idini commented 9 years ago

HI all. There is a problem when compile with the vcg library (http://vcg.isti.cnr.it/vcglib/): the emcc compiler doesn't report any error but many of the most important methods are ignored. It seems to ignore code imported from library. This is my Github repository where you can find my code: https://github.com/cignoni/meshlabjs/tree/master/test/LoadProcRender1.2 My project needs to include the vcg library above This is the output of the compiler:

warning: unresolved symbol: _ZN3vcg3ply7PlyFile7compileEPNS0_10PlyElementE
warning: unresolved symbol: _ZN3vcg3ply7PlyFile9AddToReadEPKcS3_iijiiiij
warning: unresolved symbol: _ZN3vcg3ply7PlyFileC1Ev
warning: unresolved symbol: _ZN3vcg3ply22interpret_texture_nameEPKcS2_Pc
warning: unresolved symbol: _ZNK3vcg3ply14PropDescriptor11memtypesizeEv
warning: unresolved symbol: _ZN3vcg3ply7PlyFile4ReadEPv
warning: unresolved symbol: _ZNK3vcg3ply7PlyFile10ElemNumberEi
warning: unresolved symbol: _ZN3vcg3ply7PlyFile8ElemNameEi
warning: unresolved symbol: _ZN3vcg3ply7PlyFile4OpenEPKci
warning: unresolved symbol: _ZN3vcg3ply7PlyFileD1Ev

and this is the cpp source:

#include <stdlib.h>
#include <fstream>
#include <emscripten.h>
#include <vcg/complex/complex.h>
#include <wrap/io_trimesh/import_ply.h>

using namespace vcg;
using namespace std;

class MyEdge;
class MyFace;
class MyVertex;
struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<MyVertex>   ::AsVertexType,
                                        vcg::Use<MyEdge>     ::AsEdgeType,
                                        vcg::Use<MyFace>     ::AsFaceType>{};

class MyVertex  : public vcg::Vertex<MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::Color4b, vcg::vertex::Qualityf, vcg::vertex::BitFlags  >{};
class MyFace    : public vcg::Face< MyUsedTypes,  vcg::face::Normal3f, vcg::face::VertexRef, vcg::face::FFAdj,  vcg::face::BitFlags > {};
class MyEdge    : public vcg::Edge<MyUsedTypes>{};
class MyMesh    : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge>  > {};

char *buf=0;
int size;
int vertexNum=0;
int faceNum=0;
MyMesh m;

extern "C" {
  char * allocator(size_t _size)
  {
     size = _size;
     buf = (char *) malloc(size);
     return buf;
  }
  int openMesh(){
     ofstream f("tmp.ply");
     f << buf;
     f.close();
    int ret = tri::io::ImporterPLY<MyMesh>::Open(m,"tmp.ply");
    if(ret != 0)
    {
      printf("Error in opening file\n");
      exit(-1);
    }
    printf("Read mesh %i %i\n",m.FN(),m.VN());
    return ret;
  }
  int getVertexNumber(){ return m.VN(); }
  float * getVertexVector() { 
    float * v = new float[m.VN()*3];
    int k=0;
    for (int i = 0; i < m.VN(); i++){
      for (int j = 0; j < 3; j++){
        v[k] = m.vert[i].cP()[j];
         k++;
      }
    }  
    return v; 
  }
  int getFaceNumber() { return m.FN(); }
  int * getFaceVector() { 
    int * f = new int[m.FN()*3];
    int k=0;
    for (int i = 0; i < m.FN(); i++)
      for (int j = 0; j < 3; j++){
        f[k] = (int)tri::Index(m,m.face[i].cV(j));
        k++;
      }
    return f;
  }
}

I'm sure that the code above is not wrong, because when I use a different part of the library (import_off) works well. For example, LoadProcRender1.1 (https://github.com/cignoni/meshlabjs/tree/master/test/LoadProcRender1.1) works fine.

I don't understand what is the problem: the compiler doesn't report errors but seems to ignore part of the code. Thanks all.

kripken commented 9 years ago

Emscripten by default will warn on unresolved symbols, and not give an error. You can build with -s ERROR_ON_UNDEFINED_SYMBOLS=1 if you prefer errors.

See http://kripken.github.io/emscripten-site/docs/getting_started/FAQ.html#why-do-functions-in-my-c-c-source-code-vanish-when-i-compile-to-javascript-and-or-i-get-no-functions-to-process for some general info on why code might not exist after compilation (see in particular the .a section). More generally, you can use llvm-nm to scan the object files you are linking to see if everything you need is indeed there.

idini commented 9 years ago

Thanks @kripten!