Search code examples
c++cmakecompilationundefined-reference

How to build and run OpenSubdiv Tutorials/examples


I'm trying to experiment with the OpenSubdiv C++ library. I'm not an experienced C++ programmer.

The OpenSubdiv library has some tutorials that I'd like to get working, but I can't figure out how.

So far, I have installed OpenSubD and dependencies (GLFW) to the best of my extremely limited abilities, by doing the following (I'm on Ubuntu, for reference).

sudo apt-get install doxygen # (GLFW Dependency?)
sudo apt-get install xorg-dev # (GLFW Dependency?)
sudo apt-get install libglfw3 # (GLFW Dependency?)
sudo apt-get install libglfw3-dev # (GLFW Dependency?)

git clone [email protected]:PixarAnimationStudios/OpenSubdiv.git
cd OpenSubdiv
mkdir build
cd build 
cmake -D NO_PTEX=1 -D NO_DOC=1 -D NO_OMP=1 -D NO_TBB=1 -D NO_CUDA=1 -D NO_OPENCL=1 -D NO_CLEW=1 -D GLFW_LOCATION="/usr/" ..
cmake --build . --config Release --target install

Note this roughly follows the instructions given in the OpenSubdiv repo README.

The code I am trying to compile is

#include <GLFW/glfw3.h>
#include <opensubdiv/far/topologyDescriptor.h>
#include <opensubdiv/far/primvarRefiner.h>

#include <cstdio>

using namespace OpenSubdiv;

int main(int, char **){
    typedef Far::TopologyDescriptor Descriptor; 
    
    Sdc::SchemeType type = OpenSubdiv::Sdc::SCHEME_CATMARK;

    Sdc::Options options; 

    options.SetVtxBoundaryInterpolation(Sdc::Options::VTX_BOUNDARY_EDGE_ONLY);
    // Compiles fine up to this point. 
    

    Descriptor desc; //Fails here, trying to instantiate a "Descriptor" object, which lives in OpenSubdiv
    desc.numVertices = g_nverts;
    desc.numFaces = g_nfaces;
    desc.numVertsPerFace = g_vertsperface;
    desc.vertIndicesPerFace = g_vertIndices; 
    
    return 0;
}

The error I get is

g++ opensubd_minimal_example.cpp -o opensubd_minimal_example
/usr/bin/ld: /tmp/cc4WQ9jc.o: in function `main':
opensubd_minimal_example.cpp:(.text+0x57): undefined reference to `OpenSubdiv::v3_4_4::Far::TopologyDescriptor::TopologyDescriptor()'
collect2: error: ld returned 1 exit status

I'm guessing that there is some linking that needs to happen that I'm missing, but I don't know what, and I can't figure it out from looking at the cmake files used by OpenSubd, because I don't know how to use cmake.

I could use some guidance. Thanks.


Solution

  • Please refer to the last section of OpenSubDiv: building with cmake.

    The linker needs to know where to find the library to link. Set the OPENSUBDIV variable to the directory of OpenSubDiv, then compile and link your app.

    g++ -I$OPENSUBDIV/include -c myapp.cpp
    g++ myapp.o -L$OPENSUBDIV/lib -losdGPU -losdCPU -o myapp