Repository: pytorch
Issue: Libtorch C++ mobile build linking error
Labels: module: build,oncall: mobile

### 🐛 Describe the bug

I am implementing model training program on mobile platform using Libtorch C++ API. I run `script/build_mobile.sh` with `CMAKE_ARGS+=("-DBUILD_SHARED_LIBS=ON")` to build shared libraries. And in order to support training features, I also change the root CMakeLists.txt as follows:
```
option(BUILD_MOBILE_AUTOGRAD "Build autograd function in mobile build (in development)" ON)
set(NO_API OFF)
```
After successfully building Libtorch, the libraries and headers are generated under `pytorch/build_mobile/install`, and I use the following CMakeLists.txt to build my own source code:
```
cmake_minimum_required(VERSION 3.1)
project(pytorch_android)

set(CMAKE_CXX_STANDARD 14)

add_executable(pytorch_android main.cpp)
target_include_directories(pytorch_android PRIVATE
        ${CMAKE_SOURCE_DIR}/../pytorch/build_mobile/install/include
        ${CMAKE_SOURCE_DIR}/../pytorch/build_mobile/install/include/torch/csrc/api/include
        ${CMAKE_SOURCE_DIR}/../pytorch/aten/src
        )
target_link_libraries(pytorch_android PRIVATE
        ${CMAKE_SOURCE_DIR}/../pytorch/build_mobile/install/lib/libc10.so
        ${CMAKE_SOURCE_DIR}/../pytorch/build_mobile/install/lib/libtorch_global_deps.so
        ${CMAKE_SOURCE_DIR}/../pytorch/build_mobile/install/lib/libtorch.so
        ${CMAKE_SOURCE_DIR}/../pytorch/build_mobile/install/lib/libtorch_cpu.so)
```
My source code is simply including the header file:
```
#include <iostream>
#include <torch/torch.h>

int main() {
    return 0;
}
```
And the following error occurred after running cmake:
```
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ultraz/Project/pytorch_android/build_x86_linux
Scanning dependencies of target pytorch_android
[ 50%] Building CXX object CMakeFiles/pytorch_android.dir/main.cpp.o
[100%] Linking CXX executable pytorch_android
/usr/bin/ld: ../../pytorch/build_mobile/install/lib/libtorch_cpu.so: undefined reference to `torch::jit::ExportModule(torch::jit::Module const&, std::function<unsigned long (void const*, unsigned long)> const&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<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> > > > > const&, bool, bool, bool)'
/usr/bin/ld: ../../pytorch/build_mobile/install/lib/libtorch_cpu.so: undefined reference to `torch::jit::ExportModule(torch::jit::Module const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<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> > > > > const&, bool, bool, bool)'
/usr/bin/ld: ../../pytorch/build_mobile/install/lib/libtorch_cpu.so: undefined reference to `torch::jit::ExportModule(torch::jit::Module const&, std::ostream&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<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> > > > > const&, bool, bool, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/pytorch_android.dir/build.make:88: pytorch_android] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/pytorch_android.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
```
Do you know how to fix this error? 
Is there any issue with my pytorch/CMakeLists.txt options? If so, How can I enable training features (torch::nn, torch::optim, torch::data) on mobile platform? 
Another potential problem could be my own CMakeLists.txt. Is this the right way to link Libtorch libraries?
### Versions

```
Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.10

Python version: 3.7.4 (default, Aug 13 2019, 20:35:49)  [GCC 7.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-84-generic-x86_64-with-debian-bullseye-sid
Is CUDA available: N/A
CUDA runtime version: 11.1.105
GPU models and configuration: 
GPU 0: NVIDIA RTX A4000
GPU 1: NVIDIA RTX A4000
GPU 2: NVIDIA RTX A4000
GPU 3: NVIDIA RTX A4000
GPU 4: NVIDIA RTX A4000
GPU 5: NVIDIA RTX A4000
GPU 6: NVIDIA RTX A4000
GPU 7: NVIDIA RTX A4000

Nvidia driver version: 470.63.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.2
[pip3] torch==1.11.0
[pip3] torchaudio==0.11.0
[pip3] torchvision==0.12.0
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               11.3.1               h2bc3f7f_2  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2021.4.0           h06a4308_640  
[conda] mkl-service               2.4.0            py37h7f8727e_0  
[conda] mkl_fft                   1.3.1            py37hd3c417c_0  
[conda] mkl_random                1.2.2            py37h51133e4_0  
[conda] numpy                     1.20.1                   pypi_0    pypi
[conda] numpy-base                1.21.2           py37h79a1101_0  
[conda] pytorch                   1.11.0          py3.7_cuda11.3_cudnn8.2.0_0    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                0.11.0               py37_cu113    pytorch
[conda] torchvision               0.12.0               py37_cu113    pytorch
```

cc @malfet @seemethere