[SOLVED] Build the C++ frontend using g++ or any other compiler (no cmake)

Hi All,

I was trying to add the libtorch library to our project which is being build by using g++ or visual studio (it’s a multiplatform project) now I was trying to include the specified files in the g++ command but it does not seem to work, or I’m doing something wrong or the files are not meant to be included this way into a compliler.

I used the standard (most simple) example taken from: https://pytorch.org/cppdocs/installing.html#minimal-example

then I tried to use the command: g++ -std=c++17 -o test *.cpp -I /home/user/libtorch/include/ -L /home/user/libtorch/lib/

but I got the error:
example-app.cpp:1:10: fatal error: torch/torch.h: No such file or directory
#include <torch/torch.h>

The cmake example worked great, but writing the cmake for the entire project will take quite some time (especially since I have never done it before), so my question is if there is any way to use the libtorch in a way described above, with a normal compiler?

Any help would be greatly appreciated!

When I just posted this I found the other include directory in the api folder, and I managed to compile it perfectly after that :slight_smile:
If other people might be interested this is the command I used:

g++ -std=c++17 -o test *.cpp -I /home/user/libtorch/include/torch/csrc/api/include/ -I /home/user/libtorch/include/ -L /home/user/libtorch/lib/ -lc10 -lcaffe2 -ltorch -D_GLIBCXX_USE_CXX11_ABI=0


@pim Thank you very much for documenting this! It would have taken me years to figure out, if ever, especially the -D parameter.

Grant Rostig

For future wanderers, I finally got it to work using the following (referencing the cmake tutorial and the commands it invokes)

 g++ main.cpp -I/libtorch/include -I/libtorch/include/torch/csrc/api/include -D_GLIBCXX_USE_CXX11_ABI=0 -std=gnu++11 -L/libtorch/lib/ -ltorch -lc10 -o output

Don’t forget to add /libtorch/lib/ to LD_LIBRARY_PATH

1 Like