Repository: pytorch
Issue: undefined reference to 'caffe2::Caffe2FlagsRegistry[abi:cxx11]()'
Labels: module: build,triaged

I finished compiling caffe2 from source on pytorch repository following this instructions: [(https://caffe2.ai/docs/getting-started.html?platform=ubuntu&configuration=compile)]

Now I am making some simple code to test caffe2 with C++ and getting this error:

```
g++ -c -std=gnu++0x -msse2 -I. -I/usr/local/cuda-9.0/include -I/home/ficha/Documents/external/pytorch/aten/src -I/usr/local/include -I/usr/include -I/home/ficha/Documents/external/pytorch/build/caffe2/proto -I/home/ficha/Documents/external/pytorch/thrid_party -I/home/ficha/Documents/external/pytorch/caffe2_include demo.cpp -o demo.o
g++ demo.o -I. -I/usr/local/cuda-9.0/include -I/home/ficha/Documents/external/pytorch/aten/src -I/usr/local/include -I/usr/include -I/home/ficha/Documents/external/pytorch/build/caffe2/proto -I/home/ficha/Documents/external/pytorch/thrid_party -I/home/ficha/Documents/external/pytorch/caffe2_include -L/usr/local/lib -L/usr/local/cuda-9.0/lib64 -L/usr/local/cuda-9.0/lib -lcudart -lcublas -lcurand -lglog -lgflags -lprotobuf -lleveldb -lsnappy -llmdb -lboost_system -lm -lopencv_core -lopencv_highgui -lopencv_imgproc -lboost_thread -lstdc++ -lcblas -latlas -L/home/ficha/Documents/external/pytorch/build/lib/ -lcaffe2_gpu -lcaffe2 -lhdf5_hl -lhdf5 -lopencv_imgcodecs -L/home/ficha/Documents/external/pytorch/build/lib -L/usr/lib/x86_64-linux-gnu -L/usr/local/cuda-9.0/lib64 -L/home/ficha/Documents/external/pytorch/thrid_party -o demo
demo.o: In function 'caffe2::(anonymous namespace)::Caffe2FlagParser_init_net::Caffe2FlagParser_init_net(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
demo.cpp:(.text+0xa4): undefined reference to 'bool caffe2::Caffe2FlagParser::Parse<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)'
demo.o: In function 'caffe2::(anonymous namespace)::Caffe2FlagParser_predict_net::Caffe2FlagParser_predict_net(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
demo.cpp:(.text+0xe4): undefined reference to 'bool caffe2::Caffe2FlagParser::Parse<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)'
demo.o: In function 'caffe2::(anonymous namespace)::Caffe2FlagParser_file::Caffe2FlagParser_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
demo.cpp:(.text+0x124): undefined reference to 'bool caffe2::Caffe2FlagParser::Parse<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)'
demo.o: In function 'caffe2::(anonymous namespace)::Caffe2FlagParser_classes::Caffe2FlagParser_classes(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
demo.cpp:(.text+0x164): undefined reference to 'bool caffe2::Caffe2FlagParser::Parse<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)'
demo.o: In function 'caffe2::(anonymous namespace)::Caffe2FlagParser_size::Caffe2FlagParser_size(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
demo.cpp:(.text+0x1a4): undefined reference to 'bool caffe2::Caffe2FlagParser::Parse<int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int*)'
demo.o: In function '__static_initialization_and_destruction_0(int, int)':
demo.cpp:(.text+0x10d2): undefined reference to 'caffe2::Caffe2FlagsRegistry[abi:cxx11]()'
demo.cpp:(.text+0x11e8): undefined reference to 'caffe2::Caffe2FlagsRegistry[abi:cxx11]()'
demo.cpp:(.text+0x12fe): undefined reference to 'caffe2::Caffe2FlagsRegistry[abi:cxx11]()'
demo.cpp:(.text+0x1414): undefined reference to 'caffe2::Caffe2FlagsRegistry[abi:cxx11]()'
demo.cpp:(.text+0x14df): undefined reference to 'caffe2::Caffe2FlagsRegistry[abi:cxx11]()'
collect2: error: ld returned 1 exit status
makefile:52: recipe for target 'demo' failed
make: *** [demo] Error 1
```

The code I want to compile is this: 

```
#include <caffe2/core/init.h>
#include <caffe2/core/net.h>
#include <caffe2/utils/proto_utils.h>

#include <fstream>
using namespace std;

CAFFE2_DEFINE_string(init_net, "res/squeezenet_init_net.pb",
                     "The given path to the init protobuffer.");
CAFFE2_DEFINE_string(predict_net, "res/squeezenet_predict_net.pb",
                     "The given path to the predict protobuffer.");
CAFFE2_DEFINE_string(file, "res/image_file.jpg", "The image file.");
CAFFE2_DEFINE_string(classes, "res/imagenet_classes.txt", "The classes file.");
CAFFE2_DEFINE_int(size, 227, "The image file.");

namespace caffe2 {

}

int main(int argc, char** argv) {
  caffe2::GlobalInit(&argc, &argv);
  google::protobuf::ShutdownProtobufLibrary();
  cout << "caffe loaded correctly" << endl;
  return 0;
}
```

This is the result of the cmake

```
-- ******** Summary ********
-- General:
--   CMake version         : 3.5.1
--   CMake command         : /usr/bin/cmake
--   Git version           : v0.1.11-9987-gb885dea-dirty
--   System                : Linux
--   C++ compiler          : /usr/bin/c++
--   C++ compiler version  : 5.4.0
--   BLAS                  : MKL
--   CXX flags             : -msse3 -msse4.1 -msse4.2   --std=c++11  -fvisibility-inlines-hidden -rdynamic -DONNX_NAMESPACE=onnx_torch -fopenmp -D__GLIBCXX_USE_CXX11_ABI=0 -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow
--   Build type            : Release
--   Compile definitions   : USE_GCC_ATOMICS=1;HAVE_MMAP=1;_FILE_OFFSET_BITS=64;HAVE_SHM_OPEN=1;HAVE_SHM_UNLINK=1;HAVE_MALLOC_USABLE_SIZE=1
--   CMAKE_PREFIX_PATH     : /home/ficha/.envs/pytorch0_4/lib/python3.5/site-packages
--   CMAKE_INSTALL_PREFIX  : /home/ficha/Documents/external/pytorch/torch/lib/tmp_install
--
--   BUILD_CAFFE2          : 1
--   BUILD_ATEN_MOBILE     : OFF
--   BUILD_BINARY          : OFF
--   BUILD_CUSTOM_PROTOBUF : ON
--     Link local protobuf : ON
--   BUILD_DOCS            : OFF
--   BUILD_PYTHON          : 1
--     Python version      : 3.5.2
--     Python executable   : /home/ficha/.envs/pytorch0_4/bin/python
--     Pythonlibs version  : 3.5.2
--     Python library      : /home/ficha/.envs/pytorch0_4/lib/python3.5
--     Python includes     : /usr/include/python3.5m
--     Python site-packages: lib/python3.5/site-packages
--   BUILD_SHARED_LIBS     : ON
--   BUILD_TEST            : OFF
--   USE_ASAN              : OFF
--   USE_CUDA              : 1
--     CUDA static link    :
--     USE_CUDNN           : ON
--     CUDA version        : 9.0
--     cuDNN version       : 7.1.3
--     CUDA root directory : /usr/local/cuda-9.0
--     CUDA library        : /usr/local/cuda-9.0/lib64/stubs/libcuda.so
--     cudart library      : /usr/local/cuda-9.0/lib64/libcudart_static.a;-pthread;dl;/usr/lib/x86_64-linux-gnu/librt.so
--     cublas library      : /usr/local/cuda-9.0/lib64/libcublas.so;/usr/local/cuda-9.0/lib64/libcublas_device.a
--     cufft library       : /usr/local/cuda-9.0/lib64/libcufft.so
--     curand library      : /usr/local/cuda-9.0/lib64/libcurand.so
--     cuDNN library       : /usr/local/cuda-9.0/lib64/libcudnn.so.7
--     nvrtc               : /usr/local/cuda-9.0/lib64/libnvrtc.so
--     CUDA include path   : /usr/local/cuda-9.0/include
--     NVCC executable     : /usr/local/cuda-9.0/bin/nvcc
--     CUDA host compiler  : /usr/bin/cc
--     USE_TENSORRT        : OFF
--   USE_ROCM              : OFF
--   USE_EIGEN_FOR_BLAS    : ON
--   USE_FFMPEG            : OFF
--   USE_GFLAGS            : ON
--   USE_GLOG              : ON
--   USE_GLOO              : ON
--     USE_GLOO_IBVERBS    : OFF
--   USE_LEVELDB           : ON
--     LevelDB version     : 1.18
--     Snappy version      : 1.1.3
--   USE_LITE_PROTO        : OFF
--   USE_LMDB              : ON
--     LMDB version        : 0.9.17
--   USE_METAL             : OFF
--   USE_MKL               :
--   USE_MOBILE_OPENGL     : OFF
--   USE_MPI               : ON
--   USE_NCCL              : ON
--     USE_SYSTEM_NCCL     : OFF
--   USE_NERVANA_GPU       : OFF
--   USE_NNPACK            : 1
--   USE_OBSERVERS         : ON
--   USE_OPENCL            : OFF
--   USE_OPENCV            : ON
--     OpenCV version      : 3.2.0
--   USE_OPENMP            : OFF
--   USE_PROF              : OFF
--   USE_REDIS             : OFF
--   USE_ROCKSDB           : OFF
--   USE_ZMQ               : OFF
--   USE_DISTRIBUTED       : OFF
--   Public Dependencies  : Threads::Threads;gflags;glog::glog
--   Private Dependencies : nnpack;cpuinfo;/usr/lib/x86_64-linux-gnu/liblmdb.so;/usr/lib/x86_64-linux-gnu/libleveldb.so;/usr/lib/x86_64-linux-gnu/libsnappy.so;/usr/lib/x86_64-linux-gnu/libnuma.so;opencv_core;opencv_highgui;opencv_imgproc;opencv_imgcodecs;opencv_videoio;opencv_video;/usr/lib/openmpi/lib/libmpi_cxx.so;/usr/lib/openmpi/lib/libmpi.so;gloo;gloo;aten_op_header_gen;onnxifi_loader;rt;gcc_s;gcc;dl
-- Configuring done
-- Generating done
```

Can anyone please bring me some help? If any more information is required, let me know. Thanks community.

cc @malfet @seemethere @walterddr