使用包装器 libtorch c++ 库的 C 程序未链接

问题描述 投票:0回答:1

我正在尝试制作一个 libtorch c++ 库以在 C 程序中使用它,使用

extern "C"
导出库函数。该库似乎可以正确编译和链接。但主要可执行文件没有链接。

我已将所有

libtorch/lib/*
文件复制到
/lib/

编译器显示很多对 libtorch 函数的未定义引用。

这是一个最小的例子。生成文件:

CFLAGS = -fPIC -Wall -Wshadow -O3 -pthread -I./
CFLAGS2 = -fPIC -Wall -O3 -pthread -I./
LDLIBS = -lm

all: libnet test

libnet: 
    g++ -shared -o ./libnet.so ./libnet.cpp $(CFLAGS2) \
      -I../../libtorch/include/ \
      -I../../libtorch/include/torch/csrc/api/include -L/lib \
      $(LDLIBS) -ltorch

test: 
    gcc -o ./test.exe ./test.c $(CFLAGS) $(LDLIBS) -ltorch ./libnet.so

C主程序test.c:

#include <libnet.h>

int main() {
  void* lstm = create_lstm(2, 64, 256, 1, 60);
}

C 头文件 libnet.h:

void* create_lstm(int layers, int hidden, int nbinputs, int outputs, int batchsize);

库 libnet.cpp:

#include <torch/torch.h>

using namespace torch;

struct LSTM_Model : torch::nn::Module {
    torch::nn::LSTM lstm{ nullptr };
    torch::nn::Linear linear{ nullptr };
    torch::optim::Adam* opt{ nullptr };
    uint64_t _batchsize;
    uint64_t _hidden;

    LSTM_Model(uint64_t layers, uint64_t hidden, uint64_t inputs,
               uint64_t outputs, uint64_t batchsize) {
        lstm = register_module("lstm", torch::nn::LSTM(
                   torch::nn::LSTMOptions(inputs, hidden)
                   .num_layers(layers)));
        linear = register_module("linear", torch::nn::Linear(
                   hidden, outputs));
    _batchsize = batchsize;
    _hidden = hidden;
    }

    torch::Tensor forward(torch::Tensor x) {
        auto lstm1 = lstm->forward(x.view({ x.size(0), _batchsize, -1 })); 
        auto out = linear->forward(
                  std::get<0>(lstm1)
                  .view({ _batchsize, x.size(0), _hidden }));
        return out; 
    }
};

extern "C" void* create_lstm(int layers, int hidden, int nbinputs,
                             int outputs, int batchsize) {
  LSTM_Model* model = new LSTM_Model(layers, hidden, nbinputs, outputs, batchsize);
    model->opt = new torch::optim::Adam(model->parameters(),
                       torch::optim::AdamOptions(2e-4).
                         betas(std::make_tuple(0.5, 0.5)));
  return (void*)model;
}

以下是未定义的参考错误,是用法语写的:

/usr/bin/ld : ./libnet.so : référence indéfinie vers « vtable for torch::optim::Optimizer »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::to(c10::Device, c10::ScalarType, bool) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::LSTMOptions::LSTMOptions(long, long) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, char const*) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::LSTMImpl::LSTMImpl(torch::nn::LSTMOptions const&) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::train(bool) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::LinearOptions::LinearOptions(long, long) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « typeinfo for torch::nn::Module »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::is_serializable() const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::pretty_print(std::ostream&) const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::zero_grad(bool) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « vtable for torch::nn::Module »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « vtable for torch::optim::AdamOptions »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::optim::OptimizerParamGroup::options() const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « typeinfo for torch::nn::LinearImpl »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::optim::AdamOptions::AdamOptions(double) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « c10::UndefinedTensorImpl::_singleton »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::LinearImpl::LinearImpl(torch::nn::LinearOptions const&) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::clone(std::optional<c10::Device> const&) const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « vtable for torch::optim::Adam »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::to(c10::ScalarType, bool) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::parameters(bool) const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::optim::OptimizerParamGroup::has_options() const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::is_training() const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::to(c10::Device, bool) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::optim::OptimizerParamGroup::params() const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::optim::Optimizer::add_param_group(torch::optim::OptimizerParamGroup const&) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::load(torch::serialize::InputArchive&) »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::save(torch::serialize::OutputArchive&) const »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::Module() »
/usr/bin/ld : ./libnet.so : référence indéfinie vers « torch::nn::Module::clone_(torch::nn::Module&, std::optional<c10::Device> const&) »
collect2: error: ld returned 1 exit status
make: *** [Makefile:14 : test] Erreur 1
c++ c linker linker-errors libtorch
1个回答
0
投票

由于我下载了 libtorch 仅 CPU 版本,因此通过使用

-ltorch_cpu
而不是
-ltorch
解决了问题,尽管文件
/lib/libtorch.so
存在。

© www.soinside.com 2019 - 2024. All rights reserved.