Cross-compiling with cmake for embedded systems QNX

shyney7: <p>Hi,<br>
I would like to use the C++ API for a RTOS (QNX). I’m using cmake 3.10.2 for the cross compilation.<br>
To make the cross compilation possible I use a toolchain file that looks like this:</p>
<pre><code class="lang-auto">SET(CMAKE_SYSTEM_PROCESSOR "x86_64")
SET(CMAKE_SYSTEM_NAME QNX)

IF(CMAKE_HOST_LINUX)
  SET(HOST_EXECUTABLE_SUFFIX "")
ENDIF(CMAKE_HOST_LINUX)

FIND_PATH(QNX_HOST
  NAME usr/bin/qcc${HOST_EXECUTABLE_SUFFIX}
  PATHS $ENV{QNX_HOST}
  NO_CMAKE_PATH
  NO_CMAKE_ENVIRONMENT_PATH
)
  
FIND_PATH(QNX_TARGET
  NAME usr/include/errno.h
  PATHS $ENV{QNX_TARGET}
  NO_CMAKE_PATH
  NO_CMAKE_ENVIRONMENT_PATH
)

IF(CMAKE_HOST_LINUX)
  FIND_PATH(QNX_CONFIGURATION
    NAME /etc/qnx/bin/qnxactivate
    PATHS $ENV{QNX_CONFIGURATION}
    "$ENV{QNX_HOST}/usr/bin/qconfig"
    NO_CMAKE_PATH
    NO_CMAKE_ENVIRONMENT_PATH
 )
ENDIF(CMAKE_HOST_LINUX)

SET(ENV{QNX_HOST} ${QNX_HOST})
SET(ENV{QNX_TARGET} ${QNX_TARGET})
IF(CMAKE_HOST_LINUX)
  SET(ENV{QNX_CONFIGURATION} ${QNX_CONFIGURATION})
  SET(ENV{PATH} "$ENV{PATH};${QNX_HOST}/usr/bin")
ENDIF(CMAKE_HOST_LINUX)

SET(CMAKE_MAKE_PROGRAM "${QNX_HOST}/usr/bin/make${HOST_EXECUTABLE_SUFFIX}"    CACHE PATH "QNX Make Program")
SET(CMAKE_SH           "${QNX_HOST}/usr/bin/ksh${HOST_EXECUTABLE_SUFFIX}"      CACHE PATH "QNX shell Program")
SET(CMAKE_AR           "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-ar${HOST_EXECUTABLE_SUFFIX}"      CACHE PATH "QNX ar Program")
SET(CMAKE_RANLIB       "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-ranlib${HOST_EXECUTABLE_SUFFIX}"      CACHE PATH "QNX ranlib Program")
SET(CMAKE_NM           "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-nm${HOST_EXECUTABLE_SUFFIX}"      CACHE PATH "QNX nm Program")
SET(CMAKE_OBJCOPY      "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-objcopy${HOST_EXECUTABLE_SUFFIX}" CACHE PATH "QNX objcopy Program")
SET(CMAKE_OBJDUMP      "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-objdump${HOST_EXECUTABLE_SUFFIX}" CACHE PATH "QNX objdump Program")
SET(CMAKE_LINKER       "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-ld"     CACHE PATH "QNX Linker Program")
SET(CMAKE_STRIP        "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-strip${HOST_EXECUTABLE_SUFFIX}"   CACHE PATH "QNX Strip Program")

SET(CMAKE_C_COMPILER ${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-gcc${HOST_EXECUTABLE_SUFFIX})
SET(CMAKE_C_FLAGS_DEBUG "-g")
SET(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")

SET(CMAKE_CXX_COMPILER ${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-c++${HOST_EXECUTABLE_SUFFIX})
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")

SET(CMAKE_FIND_ROOT_PATH ${QNX_TARGET}) 
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
</code></pre>
<p>If I compile a program without Pytorch everything works fine. But when I customize the CMakeLists.txt for Pytorch I get an error message when I run the following cmake command:</p>
<pre><code class="lang-auto">cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/qnx_7.0.0_linux_x86_64.cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-gcc
-- Check for working C compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-gcc -- 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: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-c++
-- Check for working CXX compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Could NOT find torch (missing: TORCH_LIBRARY) 
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
torch;torch_library;C10_LIBRARY
    linked by target "owndataset_tsp" in directory /home/marcel/Documents/pytroch_cpp_owndataset_tsp

-- Configuring incomplete, errors occurred!
See also "/home/marcel/Documents/pytroch_cpp_owndataset_tsp/build/CMakeFiles/CMakeOutput.log".
</code></pre>
<p>My CMakeLists.txt File looks like this:</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)

project(pytorch_cpp_owndataset)

list(APPEND CMAKE_PREFIX_PATH "/home/marcel/Documents/libs/libtorch1.5linux/libtorch")
set(CMAKE_MODULE_PATH "/home/marcel/Documents/libs/libtorch1.5linux/libtorch/share/cmake/Torch")

find_package(Torch REQUIRED)

if ("${CMAKE_SYSTEM_NAME}" STREQUAL QNX)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14")
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

link_directories(${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

set(EXECUTABLE_NAME owndataset_tsp)

add_executable(${EXECUTABLE_NAME} main.cpp src/csv2vector.cpp src/owndata.cpp)

target_link_libraries(${EXECUTABLE_NAME} "${TORCH_LIBRARIES}")
set_property(TARGET ${EXECUTABLE_NAME} PROPERTY CXX_STANDARD 14)
</code></pre>
<p>I can’t explain why cmake can’t find the torch library. The path is correctly set in the CMakeLists.txt. Because if I run cmake without the toolchain file and compile for my host machine everything works.</p>
<p>I hope someone can help me with that.</p>
<p>Here is the CMakeOutput.log:</p>
<aside class="onebox githubgist">
  <header class="source">
      <a href="https://gist.github.com/shyney7/02c1d6cb5ce9418b7bb8df2817cbf024" target="_blank" rel="nofollow noopener">gist.github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://gist.github.com/shyney7/02c1d6cb5ce9418b7bb8df2817cbf024" target="_blank" rel="nofollow noopener">https://gist.github.com/shyney7/02c1d6cb5ce9418b7bb8df2817cbf024</a></h4>
<h5>CMakeOutput.log</h5>
<pre><code class="">The target system is: QNX -  - x86_64
The host system is: Linux - 5.3.0-51-generic - x86_64
Compiling the C compiler identification source file "CMakeCCompilerId.c" succeeded.
Compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-gcc 
Build flags: 
Id flags:  

The output was:
0
</code></pre>
This file has been truncated. <a href="https://gist.github.com/shyney7/02c1d6cb5ce9418b7bb8df2817cbf024" target="_blank" rel="nofollow noopener">show original</a>

<p>
</p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


glaringlee: <p><a class="mention" href="/u/shyney7">@shyney7</a><br>
Can you remove “set(CMAKE_MODULE_PATH” line and try again?<br>
If it doesn’t work, I suggest to follow <a href="https://pytorch.org/cppdocs/installing.html" rel="nofollow noopener">https://pytorch.org/cppdocs/installing.html</a> to create a simple example and make it compiled and then merge the Camke lines into your project.</p>

shyney7: <p><a class="mention" href="/u/glaringlee">@glaringlee</a>  Thank you for your help! Unfortunately I have the same problem after removing the set(CMAKE_MODULE_PATH… command. This is how my CMakeLists.txt file looks like right now. I have created a simple test program from the cppdocs where I just create a Tensor and print it out with std::cout.</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(libtorch_cross_test)
list(APPEND CMAKE_PREFIX_PATH "/home/marcel/Documents/libs/libtorch1.5linux/libtorch")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app main.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
</code></pre>
<p>If I compile this <strong>without</strong>  the cmake toolchain file for the cross compilation everything works just fine.<br>
As soon as I add the cmake flag<br>
<em>-DCMAKE_TOOLCHAIN_FILE=…/cmake/qnx_7.0.0_linux_x86_64.cmake</em><br>
to use my toolchain I get the same missing: TORCH_LIBRARY message.</p>
<pre><code class="lang-auto">cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/qnx_7.0.0_linux_x86_64.cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-gcc
-- Check for working C compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-gcc -- 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: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-c++
-- Check for working CXX compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Could NOT find torch (missing: TORCH_LIBRARY) 
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
torch;torch_library;C10_LIBRARY
    linked by target "example-app" in directory /home/marcel/Documents/libtorch_cross_test

-- Configuring incomplete, errors occurred!
See also "/home/marcel/Documents/libtorch_cross_test/build/CMakeFiles/CMakeOutput.log".
</code></pre>
<p>The QNX compiler (qcc) seems to be recognized just fine by cmake so I cant explain why this happens.</p>

shyney7: <p>Ok I was able to fix it by myself by changing <em>SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)</em> to<br>
<em>SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)</em></p>
<p>With this change I was able to create the make file with cmake.<br>
But now as soon i try to build the source file with the “make” command I get the following compiler errors:<br>
<aside class="onebox githubgist">
  <header class="source">
      <a href="https://gist.github.com/shyney7/ca3e59186c2904024ab037df11213e57" target="_blank" rel="nofollow noopener">gist.github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://gist.github.com/shyney7/ca3e59186c2904024ab037df11213e57" target="_blank" rel="nofollow noopener">https://gist.github.com/shyney7/ca3e59186c2904024ab037df11213e57</a></h4>
<h5>Libtorch_compiler_error_QNX</h5>
<pre><code class="">marcel@ubuntu:~/Documents/libtorch_cross_test/build$ make
Scanning dependencies of target example-app
[ 50%] Building CXX object CMakeFiles/example-app.dir/main.cpp.o
In file included from /home/marcel/qnx700/target/qnx7/usr/include/c++/v1/algorithm:628:0,
                 from /home/marcel/qnx700/target/qnx7/usr/include/c++/v1/string:439,
                 from /home/marcel/qnx700/target/qnx7/usr/include/c++/v1/__locale:15,
                 from /home/marcel/qnx700/target/qnx7/usr/include/c++/v1/ios:216,
                 from /home/marcel/qnx700/target/qnx7/usr/include/c++/v1/iostream:38,
                 from /home/marcel/Documents/libtorch_cross_test/main.cpp:1:
/home/marcel/qnx700/target/qnx7/usr/include/c++/v1/memory: In instantiation of 'struct std::__1::allocator_traits&lt;std::__1::allocator_traits&lt;std::__1::allocator&lt;std::__1::pair&lt;c10::IValue, c10::IValue&gt; &gt; &gt;::rebind_alloc&lt;ska_ordered::detailv3::sherwood_v3_entry&lt;std::__1::pair&lt;c10::IValue, c10::IValue&gt; &gt; &gt; &gt;':</code></pre>
This file has been truncated. <a href="https://gist.github.com/shyney7/ca3e59186c2904024ab037df11213e57" target="_blank" rel="nofollow noopener">show original</a>

<p>
</p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p><img src="https://discuss.pytorch.org/images/emoji/apple/worried.png?v=9" title=":worried:" class="emoji only-emoji" alt=":worried:"></p>

glaringlee: <p><a class="mention" href="/u/shyney7">@shyney7</a><br>
Once you tell cmake to do CMAKE_TOOLCHAIN_FILE, cmake enter into cross-compiling mode, your library search root will be set here:<br>
SET(CMAKE_FIND_ROOT_PATH ${QNX_TARGET})<br>
This will override your CMAKE_PREFIX_PATH.</p>
<p>I am not sure how many other things will be override if specifying toolchain file, you can try specify your torch root in toolchain file, something like SET(CMAKE_FIND_ROOT_PATH $(QNX_TARGET), $(TORCH_ROOT)). or you can put torch root into system root and then set SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)</p>

shyney7: <p><a class="mention" href="/u/glaringlee">@glaringlee</a><br>
As you can see from the second post I have already managed to generate the makefiles with cmake by adjusting the toolchain file with the following command:</p>
<pre><code class="lang-auto">SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
</code></pre>
<p>Since I told cmake to search for libraries not only in the root of the target but also in the root of the host it was able to find the libtorch library. But the problem I have now is that the compiler shows error messages during the build process. Unfortunately I am not a programmer but a mechanical engineer working at a research lab of a german university trying to implement libtorch into production. So I am not sure what the compiler errors mean. I guess it is because of the stricter memory policies of QNX since it is an RTOS? But then I wonder why in the announcement videos where Libtorch and the C++ API were introduced, it was always said that the C++ API is specifically for production environments. The fact is that in production environments you mostly work with real-time operating systems like QNX or FreeRTOS.<br>
Or is there a way to run Libtorch on an RTOS that I’m not aware of? The most important thing would be if at least pretrained models would run in realtime in the so-called online training.</p>

glaringlee: <p>I think we were typing at the same time and you posted yours right before mine <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"><br>
I personally don’t think this error is due to RTOS strict memory policies.</p>
<p>This is a compile error, this is related to how you use the libtorch apis in main.cpp, to me, this is more like an implementation error.</p>
<p>Can you try just include several libtorch headers without doing anything in main.cpp, see whether you can compile it successfully?</p>

shyney7: <p><a class="mention" href="/u/glaringlee">@glaringlee</a><br>
My main.cpp just had the example code from the cppdocs. But now I’ve done as you suggested and just created a main.cpp with the torch.h header file like this:</p>
<pre><code class="lang-auto">
#include &lt;torch/torch.h&gt;

int main() {

return 0;
}

</code></pre>
<p>But Im still getting the same compiler errors:<br>
<aside class="onebox githubgist">
  <header class="source">
      <a href="https://gist.github.com/shyney7/6430c088cdeff1d120c7620aa9ab2289" target="_blank" rel="nofollow noopener">gist.github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://gist.github.com/shyney7/6430c088cdeff1d120c7620aa9ab2289" target="_blank" rel="nofollow noopener">https://gist.github.com/shyney7/6430c088cdeff1d120c7620aa9ab2289</a></h4>
<h5>Libtorch cross-compilation error QNX</h5>
<pre><code class="">marcel@ubuntu:~/Documents/libtorch_cross_test/build$ source ~/qnx700/qnxsdp-env.sh 
QNX_HOST=/home/marcel/qnx700/host/linux/x86_64
QNX_TARGET=/home/marcel/qnx700/target/qnx7
MAKEFLAGS=-I/home/marcel/qnx700/target/qnx7/usr/include
marcel@ubuntu:~/Documents/libtorch_cross_test/build$ make
[ 50%] Building CXX object CMakeFiles/example-app.dir/main.cpp.o
In file included from /home/marcel/Documents/libs/libtorch/include/c10/core/Allocator.h:4:0,
                 from /home/marcel/Documents/libs/libtorch/include/ATen/ATen.h:3,
                 from /home/marcel/Documents/libs/libtorch/include/torch/csrc/api/include/torch/types.h:3,
                 from /home/marcel/Documents/libs/libtorch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,</code></pre>
This file has been truncated. <a href="https://gist.github.com/shyney7/6430c088cdeff1d120c7620aa9ab2289" target="_blank" rel="nofollow noopener">show original</a>

<p>
</p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>I hope you can help me with that.</p>

glaringlee: <p><a class="mention" href="/u/shyney7">@shyney7</a><br>
It seems to me that your make file still breaks something in building chain. This is not caused by libtorch.<br>
I am not familiar with QNX, from your error log, I can see it compiled c++ code using v1/memory instead of standard memory lib. libtorch is compiled with standard c++14 libs, we can not guarantee it compiled with other version libs. I checked QNX a little bit, v1/memory is used for llvm compiler infrastructure, while libtorch is compiled using gcc/g++. You should compile your code using the memory lib in 5.4.0 folder (same level as ‘v1’ folder).</p>
<p>Can you try this, I am not sure whether it will work though.<br>
if ("${CMAKE_SYSTEM_NAME}" STREQUAL QNX)<br>
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -std=gnu++14 -stdlib=libstdc++”)<br>
endif()</p>
<p>And another thing is that, I think it would be better to add your libtorch path into CMAKE_FIND_ROOT_PATH and put back CMAKE_FIND_ROOT_PATH_MODE_PACKAGE from BOTH to ONLY.</p>

shyney7: <p><a class="mention" href="/u/glaringlee">@glaringlee</a><br>
Thank you so much for replying and trying to help! I really appreciate this!<br>
This is also what I found in another repo on github while searching the web for similar errors.<br>
So I have changed the CMakelists.txt file as follows:</p>
<p><code>set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14 -stdlib=libstdc++ ${TORCH_CXX_FLAGS}")</code></p>
<p>But now I’m getting the following compiler errors:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/shyney7/libtorch-cross-RTOS-test/blob/fa8f42964be0b4e194f402f8296909f0e438d470/Compilerlogs/comperr_with-stdlib%3Dlibstdc%2B%2B_flag.md">
  <header class="source">

      <a href="https://github.com/shyney7/libtorch-cross-RTOS-test/blob/fa8f42964be0b4e194f402f8296909f0e438d470/Compilerlogs/comperr_with-stdlib%3Dlibstdc%2B%2B_flag.md" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/shyney7/libtorch-cross-RTOS-test/blob/fa8f42964be0b4e194f402f8296909f0e438d470/Compilerlogs/comperr_with-stdlib%3Dlibstdc%2B%2B_flag.md" target="_blank" rel="noopener nofollow ugc">shyney7/libtorch-cross-RTOS-test/blob/fa8f42964be0b4e194f402f8296909f0e438d470/Compilerlogs/comperr_with-stdlib=libstdc++_flag.md</a></h4>


      <pre><code class="lang-md">```console
marcel@ubuntu:~/Documents/libtorch_cross_test/build$ cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/qnx_7.0.0_linux_x86_64.cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-gcc
-- Check for working C compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-gcc -- 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: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-c++
-- Check for working CXX compiler: /home/marcel/qnx700/host/linux/x86_64/usr/bin/ntox86_64-c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
</code></pre>



  This file has been truncated. <a href="https://github.com/shyney7/libtorch-cross-RTOS-test/blob/fa8f42964be0b4e194f402f8296909f0e438d470/Compilerlogs/comperr_with-stdlib%3Dlibstdc%2B%2B_flag.md" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I have also opened an issue on github describing everything and with all the files from a repo I created.</p><aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/38310#issue-616494038">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/38310#issue-616494038" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/38310#issue-616494038" target="_blank" rel="noopener nofollow ugc">Compiling errors when trying to cross-compile the C++ API for RTOS (QNX)</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-05-12" data-time="09:12:52" data-timezone="UTC">09:12AM - 12 May 20 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/shyney7" target="_blank" rel="noopener nofollow ugc">
          <img alt="shyney7" src="https://avatars.githubusercontent.com/u/17785769?v=4" class="onebox-avatar-inline" width="20" height="20">
          shyney7
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: build
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: cpp
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">I am currently trying to implement the Pytorch C++ API into production. RTOS are<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden"> indispensable in production environments so I'm trying to cross-compile with cmake for QNX Neutrino 7.0.0 (x86_64). 
However, when compiling a simple test program (empty main() function just including "#include &lt;torch/torch.h&gt;") 
I get the following compiler errors:

https://github.com/shyney7/libtorch-cross-RTOS-test/blob/master/Compilerlogs/comperr-without-stdlib-flag.md

I had to set the following command so that cmake finds the torch library in the host path.

` SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)`

This is how my **cmake Toolchain File** looks like:

https://github.com/shyney7/libtorch-cross-RTOS-test/blob/master/cmake/qnx_7.0.0_linux_x86_64.cmake

This is my **CMakeLists.txt** file:

https://github.com/shyney7/libtorch-cross-RTOS-test/blob/master/CMakeLists.txt

Unfortunately I am not a computer scientist but a mechanical engineer working at a research lab of a german university trying to implement libtorch into production. So I am not sure what the compiler errors mean. Thats why I searched the web for similar errors and found an issue of another repo here: 
https://github.com/eProsima/Fast-RTPS/issues/300
There it is mentioned that it is also needed to prevent the qcc compiler from using the LLVM Compiler Infrastructure from v1/memory by using the `-stdlib=libstdc++` flag.
I also tried this by changing my CMakeLists.txt file to use the following flags:
`set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14 -stdlib=libstdc++ ${TORCH_CXX_FLAGS}")` 

But then I'm getting the following **compiler errors**:

https://github.com/shyney7/libtorch-cross-RTOS-test/blob/master/Compilerlogs/comperr_with-stdlib%3Dlibstdc%2B%2B_flag.md

In the announcement videos where Libtorch and the C++ API were introduced, it was always said that the C++ API is specifically for production environments. The fact is that in production environments you mostly work with real-time operating systems like QNX or FreeRTOS, so I'm sure that there is somehow a way to get libtorch working on QNX.

Here is the repo with all the files:
https://github.com/shyney7/libtorch-cross-RTOS-test

Here is also a link to my first post on the discussion forums where I faced some issues with setting up the cmake toolchain file correctly:
https://discuss.pytorch.org/t/cross-compiling-with-cmake-for-embedded-systems-qnx/79681

## Environment

 - Libtorch Version : 1.5 Release
 - OS : cross-compiling on Ubuntu 18.04 (VM) for **QNX 7.0.0 (x84_64)**
 - How you installed PyTorch : libtorch 1.5 zip from pytorch.org (for linux tried with both Pre &amp; cxx11 ABI)
 - Build command you used : 

`cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/qnx_7.0.0_linux_x86_64.cmake ..`
`make`
 - GNU version: 5.4.0
 - CUDA/cuDNN version: none
 - GPU models and configuration: none
 - cmake version: 3.10.2 


cc @malfet @yf225 @glaringlee</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<aside class="quote no-group" data-username="glaringlee" data-post="9" data-topic="79681">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/9de053/48.png" class="avatar"> glaringlee:</div>
<blockquote>
<p>And another thing is that, I think it would be better to add your libtorch path into CMAKE_FIND_ROOT_PATH and put back CMAKE_FIND_ROOT_PATH_MODE_PACKAGE from BOTH to ONLY.</p>
</blockquote>
</aside>
<p>I’ve tried this by moving the libtorch folder to the QNX Target Environment as stated here (<a href="http://www.qnx.com/developers/docs/7.0.0/#com.qnx.doc.neutrino.prog/topic/devel_USINGLIB.html" rel="noopener nofollow ugc">QNX Docs</a>):</p>
<p><code>/home/marcel/qnx700/target/qnx7/x86_64/lib/libtorch/</code></p>
<p>And changed the line in the CMakelists.txt as follows:<br>
<code>list(APPEND CMAKE_PREFIX_PATH "${QNX_TARGET}/x86_64/lib/libtorch")</code></p>
<p>But this change did not lead to a succesful build and I’m getting the same compiler errors as shown above.<br>
<a href="https://gist.github.com/shyney7/3548fcc10495bfcc28ad76bc2ed3dcbd" rel="noopener nofollow ugc">Compiler Errors</a></p>

glaringlee: <p><a class="mention" href="/u/shyney7">@shyney7</a><br>
What’s your operating system? ubuntu? centos?<br>
Did you installed <code>libc</code>  and  <code>libc6-dev</code>? if so can you upgrade glibc?<br>
From your latest error, that is caused by missing libs, please check the document of your operating system see how to install them. If you are on the system above, try install <code>libc</code> and <code>libc6-dev</code> and upgrade them.</p>
<p>Thanks for filing an issue with us, I will assign that issue to proper people to take a look at, but again we are not QNX people, if you can show your error to a QNX engineer, they might give you a quicker answer, the reason is that libtorch is built using standard libraries while QNX is built using extensions, these errors to me is caused by QNX env.</p>