Repository: pytorch
Issue: Static Linking Pytorch
Labels: triaged,module: static linking

## 🐛 Bug

I tried to statically link pytorch but it does not work.

## To Reproduce

I build pytorch the following way:

```sh
BUILD_ARGS=()
BUILD_ARGS+=("-DBUILD_SHARED_LIBS=OFF")
BUILD_ARGS+=("-DBUILD_PYTHON=OFF")
BUILD_ARGS+=("-DUSE_MKLDNN=OFF")
BUILD_ARGS+=("-DBUILD_CAFFE2=OFF")
BUILD_ARGS+=("-DUSE_FBGEMM=ON")
BUILD_ARGS+=("-DUSE_NNPACK=ON")
BUILD_ARGS+=("-DUSE_QNNPACK=OFF")
BUILD_ARGS+=("-DUSE_PYTORCH_QNNPACK=OFF")
BUILD_ARGS+=("-DUSE_XNNPACK=OFF")
BUILD_ARGS+=("-DUSE_DISTRIBUTED=OFF")
BUILD_ARGS+=("-DUSE_NCCL=OFF")
BUILD_ARGS+=("-DCMAKE_BUILD_TYPE=Release")
BUILD_ARGS+=("-DCMAKE_INSTALL_PREFIX=${PWD}/libtorch")
   
BUILD_ARGS+=("$@")

# Display gathered arguments
printf "\n\ntorch:: Libtorch build arguments:\n\n"
echo "${BUILD_ARGS[@]}"

echo "torch:: Cloning and building Libtorch..."
git clone --branch v1.8.1 --depth 1 --recursive https://github.com/pytorch/pytorch.git
cd pytorch

rm -rf build || ls
mkdir -p build
cd build
cmake "${BUILD_ARGS[@]}" ..
cmake --build . --target install --parallel 12

cd ../..
```

then I try to compile the following cpp:

dcgan.cpp
```cpp
#include <torch/torch.h>
#include <iostream>


struct Net : torch::nn::Module {
  Net(int64_t N, int64_t M)
      : linear(register_module("linear", torch::nn::Linear(N, M))) {
    another_bias = register_parameter("b", torch::randn(M));
  }
  torch::Tensor forward(torch::Tensor input) {
    return linear(input) + another_bias;
  }
  torch::nn::Linear linear;
  torch::Tensor another_bias;
};

int main() {
    Net net(4, 5);
    for (const auto& pair : net.named_parameters()) {
    std::cout << pair.key() << ": " << pair.value() << std::endl;
    }
    std::cout << net.forward(torch::ones({2, 4})) << std::endl;

}
```

with this cmakelists.txt


```cmake

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(dcgan)

    # static build
    set(LIBTORCH "${CMAKE_CURRENT_SOURCE_DIR}/../pytorch/build")
    # I also tried ${CMAKE_CURRENT_SOURCE_DIR}/../libtorch , same error
    set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${LIBTORCH}")

    find_package(Torch REQUIRED)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

    include_directories(
        "${TORCH_INCLUDE_DIRS}" # libtorch headers
    )

    add_executable(dcgan dcgan.cpp)

    target_link_libraries(dcgan PRIVATE -lm
            -Wl,--whole-archive "${TORCH_LIBRARIES}"
            -Wl,--no-whole-archive
            -lpthread
            ${CMAKE_DL_LIBS})

    set_property(TARGET dcgan PROPERTY CXX_STANDARD 14)

```

but I get the following error (I already checked the cmake paths, they seem to be correct): 
```sh
CMake Warning at /home/markus/Downloads/pytorch_test/static_pytorch/pytorch/build/TorchConfig.cmake:22 (message):
  static library eigen_blas_LIBRARY-NOTFOUND not found.
Call Stack (most recent call first):
  /home/markus/Downloads/pytorch_test/static_pytorch/pytorch/build/TorchConfig.cmake:113 (append_torchlib_if_found)
  CMakeLists.txt:13 (find_package)


CMake Warning at /home/markus/Downloads/pytorch_test/static_pytorch/pytorch/build/TorchConfig.cmake:22 (message):
  static library sleef_LIBRARY-NOTFOUND not found.
Call Stack (most recent call first):
  /home/markus/Downloads/pytorch_test/static_pytorch/pytorch/build/TorchConfig.cmake:123 (append_torchlib_if_found)
  CMakeLists.txt:13 (find_package)


-- Found Torch: /home/markus/Downloads/pytorch_test/static_pytorch/pytorch/build/lib/libtorch.a  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/markus/Downloads/pytorch_test/static_pytorch/src/build
[ 50%] Building CXX object CMakeFiles/dcgan.dir/dcgan.cpp.o
/home/markus/Downloads/pytorch_test/static_pytorch/src/dcgan.cpp:1:10: fatal error: torch/torch.h: No such file or directory
    1 | #include <torch/torch.h>
      |          ^~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/dcgan.dir/build.make:76: CMakeFiles/dcgan.dir/dcgan.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/dcgan.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
build_static.sh: line 6: ./dcgan: No such file or directory
finished

```


 - PyTorch Version (e.g., 1.0): 1.8.1
 - OS (e.g., Linux): manjaro
 - How you installed PyTorch (`conda`, `pip`, source): source
 - Build command you used (if compiling from source): see above
 - Python version: --
 - CUDA/cuDNN version: --
 - GPU models and configuration: -- 
 - Any other relevant information: --