koba-jon / pytorch_cpp

Deep Learning sample programs using PyTorch in C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

build failing

fork-while-1 opened this issue · comments

Hi,
I'm trying to build this project according to the given instructions, but the make fails because it says it needs -std=c++11 support. Where do I add this flag? I do not have g++-8 (I'm not able to install it, so I have to work with g++-5).
Thank you so much!

Hello,

To use -std=c++11, please change 9th line of pytorch_cpp/utils/CMakeLists.txt.
I can't install g++-5 unlike your environment.
This is my guess because I can't reproduce your environment.

Before:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall")

After:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -std=c++11")

Additional notes

In compilers prior to g++-7, since the header "#include <filesystem>" for creating a directory cannot be recognized, you need to use "#include <sys/stat.h>" instead.

I used to create directories with old headers in v1.6.0.
It will be the older version in this repository, but this corresponds to the following source code.
https://github.com/koba-jon/pytorch_cpp/tree/support/v1.6.0

Hi,
Thank you for getting back to me!
I was able to install g++-8, and now I'm getting a different error. This time, I'm getting a linker error, cmake is for some reason not able to find libboost.
What is weird that the "display message" part of the cmake file does print out the information for boost:

-- ~~~ Boost Information ~~~
-- /usr/include;
-- /usr/lib/x86_64-linux-gnu/libboost_program_options.so;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~

But when I execute make, it throws linker errors that look like:

CMakeFiles/pix2pix.dir/home/trishita/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/main.cpp.o: In function `Set_Device(boost::program_options::variables_map&)':
main.cpp:(.text+0x425): undefined reference to `boost::program_options::abstract_variables_map::operator[](std::string const&) const'

I even added this to the pytorch_cpp/utils/CMakeLists.txt file:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -lboost_program_options -L/usr/lib/x86_64-linux-gnu/libboost_program_options.so.1.58.0")

But it still isn't able to find boost.
Do you have any ideas on what the issue might be?
Thank you!

Here is the output from cmake .. -DBoost_DEBUG=ON

-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1673 ] _boost_TEST_VERSIONS = "1.77.0;1.77;1.76.0;1.76;1.75.0;1.75;1.74.0;1.74;1.73.0;1.73;1.72.0;1.72;1.71.0;1.71;1.70.0;1.70;1.69.0;1.69;1.68.0;1.68;1.67.0;1.67;1.66.0;1.66;1.65.1;1.65.0;1.65;1.64.0;1.64;1.63.0;1.63;1.62.0;1.62;1.61.0;1.61;1.60.0;1.60;1.59.0;1.59;1.58.0;1.58;1.57.0;1.57;1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1674 ] Boost_USE_MULTITHREADED = "TRUE"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1675 ] Boost_USE_STATIC_LIBS = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1676 ] Boost_USE_STATIC_RUNTIME = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1677 ] Boost_ADDITIONAL_VERSIONS = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1678 ] Boost_NO_SYSTEM_PATHS = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1710 ] BOOST_ROOT = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1711 ] ENV{BOOST_ROOT} = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1712 ] BOOST_INCLUDEDIR = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1713 ] ENV{BOOST_INCLUDEDIR} = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1714 ] BOOST_LIBRARYDIR = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1715 ] ENV{BOOST_LIBRARYDIR} = <unset>
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1803 ] location of version.hpp: /usr/include/boost/version.hpp
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1843 ] Boost_VERSION = "105800"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1844 ] Boost_VERSION_STRING = "1.58.0"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1845 ] Boost_VERSION_MACRO = "105800"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1846 ] Boost_VERSION_MAJOR = "1"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1847 ] Boost_VERSION_MINOR = "58"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1848 ] Boost_VERSION_PATCH = "0"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1849 ] Boost_VERSION_COUNT = "3"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1873 ] Boost_LIB_PREFIX = ""
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1874 ] Boost_NAMESPACE = "boost"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:953 ] _boost_COMPILER = "-gcc55" (guessed)
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1910 ] _boost_MULTITHREADED = "-mt"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1988 ] _boost_ARCHITECTURE_TAG = "" (detected)
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1992 ] _boost_RELEASE_ABI_TAG = "-"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1993 ] _boost_DEBUG_ABI_TAG = "-d"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2053 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = "/usr/lib/x86_64-linux-gnu;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2054 ] _boost_LIBRARY_SEARCH_DIRS_DEBUG = "/usr/lib/x86_64-linux-gnu;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2239 ] Searching for PROGRAM_OPTIONS_LIBRARY_RELEASE: boost_program_options-gcc55-mt-1_58;boost_program_options-gcc55-mt;boost_program_options-gcc55-mt;boost_program_options-mt-1_58;boost_program_options-mt;boost_program_options-mt;boost_program_options-mt;boost_program_options
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:807 ] Boost_LIBRARY_DIR_RELEASE = "/usr/lib/x86_64-linux-gnu"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:809 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = "/usr/lib/x86_64-linux-gnu;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2294 ] Searching for PROGRAM_OPTIONS_LIBRARY_DEBUG: boost_program_options-gcc55-mt-d-1_58;boost_program_options-gcc55-mt-d;boost_program_options-gcc55-mt-d;boost_program_options-mt-d-1_58;boost_program_options-mt-d;boost_program_options-mt-d;boost_program_options-mt;boost_program_options
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:807 ] Boost_LIBRARY_DIR_DEBUG = "/usr/lib/x86_64-linux-gnu"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:809 ] _boost_LIBRARY_SEARCH_DIRS_DEBUG = "/usr/lib/x86_64-linux-gnu;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH"
-- 
-- ~~~ PyTorch Information ~~~
-- /usr/src/libtorch/include;/usr/src/libtorch/include/torch/csrc/api/include;
-- torch;torch_library;/usr/src/libtorch/lib/libc10.so;/usr/src/libtorch/lib/libkineto.a;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- 
-- ~~~ OpenCV Information ~~~
-- /usr/local/include/opencv4;
-- opencv_objdetect;opencv_stitching;opencv_videoio;opencv_photo;opencv_dnn;opencv_core;opencv_gapi;opencv_flann;opencv_features2d;opencv_ml;opencv_highgui;opencv_calib3d;opencv_video;opencv_imgproc;opencv_imgcodecs;opencv_reg;opencv_plot;opencv_aruco;opencv_ximgproc;opencv_surface_matching;opencv_videostab;opencv_line_descriptor;opencv_img_hash;opencv_dpm;opencv_quality;opencv_stereo;opencv_phase_unwrapping;opencv_bioinspired;opencv_intensity_transform;opencv_xphoto;opencv_mcc;opencv_bgsegm;opencv_hfs;opencv_dnn_objdetect;opencv_text;opencv_face;opencv_xfeatures2d;opencv_rgbd;opencv_barcode;opencv_structured_light;opencv_saliency;opencv_dnn_superres;opencv_rapid;opencv_freetype;opencv_ccalib;opencv_optflow;opencv_shape;opencv_superres;opencv_datasets;opencv_tracking;opencv_xobjdetect;opencv_wechat_qrcode;opencv_fuzzy;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~
-- 
-- ~~~ Boost Information ~~~
-- /usr/include;
-- /usr/lib/x86_64-linux-gnu/libboost_program_options.so;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~
-- 
-- ~~~ libpng Information ~~~
-- /usr/include;/usr/include;
-- /usr/lib/x86_64-linux-gnu/libpng.so;/usr/lib/x86_64-linux-gnu/libz.so;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/trishita/pytorch_cpp/Image-to-Image_Translation/pix2pix/build

I understand that only the link failed.

There are two possibilities:
(a) The binary files between the libraries are not compatible.
(b) The implementation part of Boost is not found.

In the case of (a), please try updating the version of Boost.
And, please make sure the version of libtorch is for cxx11-abi, not pre-cxx11-abi.

Hi!
I tried looking into it more. I tried compiling a dummy program that uses boost_program_options. It appears that it doesn't compile on my system (I get a linker error), unless I give it the following flag: -lboost_program_options
So I had to compile my dummy program like this:

g++ a.cpp -o a.out -lboost_program_options

Otherwise I got a linker error for this too.
I think I need to somehow pass this flag to pix2pix as well, but I am not sure where to add it. I tried adding it to pytorch_cpp/utils/CMakeLists.txt
like so:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -lboost_program_options")

But I still get the linker error. Maybe it is because I'm not adding the flag in the right cmake file. Could you let me know where to add it?
Thanks!

I ran various pattern of builds.
As a result, the file of LibTorch may be incorrect.
This is not directly related to boost, but it may affect link errors.

(1) Pre-cxx11 API

File: https://download.pytorch.org/libtorch/cu102/libtorch-shared-with-deps-1.10.0%2Bcu102.zip

$ make

Scanning dependencies of target pix2pix
[  7%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/main.cpp.o
[ 15%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/train.cpp.o
[ 23%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/valid.cpp.o
[ 30%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/test.cpp.o
[ 38%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/loss.cpp.o
[ 46%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/networks.cpp.o
[ 53%] Building CXX object build/CMakeFiles/pix2pix.dir/transforms.cpp.o
[ 61%] Building CXX object build/CMakeFiles/pix2pix.dir/datasets.cpp.o
[ 69%] Building CXX object build/CMakeFiles/pix2pix.dir/dataloader.cpp.o
[ 76%] Building CXX object build/CMakeFiles/pix2pix.dir/losses.cpp.o
[ 84%] Building CXX object build/CMakeFiles/pix2pix.dir/visualizer.cpp.o
[ 92%] Building CXX object build/CMakeFiles/pix2pix.dir/progress.cpp.o
[100%] Linking CXX executable /home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/pix2pix
/usr/bin/ld: CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/main.cpp.o: in function `Set_Device(boost::program_options::variables_map&)':
main.cpp:(.text+0x32e): undefined reference to `boost::program_options::abstract_variables_map::operator[](std::string const&) const'
/usr/bin/ld: CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/main.cpp.o: in function `Set_Options(boost::program_options::variables_map&, int, char const**, boost::program_options::options_description&, std::string)':
...
...
...
make[2]: *** [build/CMakeFiles/pix2pix.dir/build.make:285: /home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/pix2pix] エラー 1
make[1]: *** [CMakeFiles/Makefile2:94: build/CMakeFiles/pix2pix.dir/all] エラー 2
make: *** [Makefile:84: all] エラー 

(2) cxx11 API

File: https://download.pytorch.org/libtorch/cu102/libtorch-cxx11-abi-shared-with-deps-1.10.0%2Bcu102.zip

$ make

Scanning dependencies of target pix2pix
[  7%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/main.cpp.o
[ 15%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/train.cpp.o
[ 23%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/valid.cpp.o
[ 30%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/test.cpp.o
[ 38%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/loss.cpp.o
[ 46%] Building CXX object build/CMakeFiles/pix2pix.dir/home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/src/networks.cpp.o
[ 53%] Building CXX object build/CMakeFiles/pix2pix.dir/transforms.cpp.o
[ 61%] Building CXX object build/CMakeFiles/pix2pix.dir/datasets.cpp.o
[ 69%] Building CXX object build/CMakeFiles/pix2pix.dir/dataloader.cpp.o
[ 76%] Building CXX object build/CMakeFiles/pix2pix.dir/losses.cpp.o
[ 84%] Building CXX object build/CMakeFiles/pix2pix.dir/visualizer.cpp.o
[ 92%] Building CXX object build/CMakeFiles/pix2pix.dir/progress.cpp.o
[100%] Linking CXX executable /home/koba/Desktop/github/pytorch_cpp/pytorch_cpp/Image-to-Image_Translation/pix2pix/pix2pix
[100%] Built target pix2pix

If you download the file (2), will the build succeed?

Hello!
Thank you for pointing me in this direction! I was able to successfully build pix2pix. I ended up having to add one line to the cmake file in /utils: set(Torch_DIR /usr/src/libtorch/share/cmake/Torch)

So now my cmake file looks like this, and it works!

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

# LibTorch
set(Torch_DIR /usr/src/libtorch/share/cmake/Torch)
set(LIBTORCH_DIR $ENV{HOME}/libtorch)
list(APPEND CMAKE_PREFIX_PATH ${LIBTORCH_DIR})

# Set Compiler Options
set(CXX_LIBRARIES "stdc++fs")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8)
    set(CMAKE_CXX_COMPILER "g++-8")
endif ()

Thank you so much! I have one more issue that I am working on, but I will open a separate issue for that :)