Hi all!
I am trying to run this minimal example of libtorch on Bela https://pytorch.org/cppdocs/installing.html , but I always run into the same error. I was wondering if someone had the same issue or might now how to approach it.
Regarding my setup: I am booting from a 64 GB SD card (I tried to run the same on EMMC but it ran out of disk memory). Compiling with g++ ran out of memory too (I guess RAM memory since I don't think it was taking the 64GB), so I tried using swap memory and got the same error as compiling with clang-7 (without swap memory)
Here is my folder structure:
example-app/
example-app.cpp
CMakeLists.txt
and example-app.cpp code:
#include <torch/torch.h>
#include <iostream>
int main() {
torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;
}
and CMakeLists.txt:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)
set(DCMAKE_PREFIX_PATH "/root/Bela/projects/example-app/libtorch")
set(Torch_DIR "/root/Bela/projects/example-app/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(example-app example-app.cpp)
target_link_libraries(example-app pthread "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
To compile the project I do
mkdir build
cd build
cmake ..
which runs errorless
and then
cmake --build . --config Release
which gives me the following error with clang:
root@bela:~/Bela/projects/example-app/build# cmake --build . --config Release
Scanning dependencies of target example-app
[ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o
[100%] Linking CXX executable example-app
../libtorch/lib/libtorch.so: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/example-app.dir/build.make:98: recipe for target 'example-app' failed
make[2]: *** [example-app] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/example-app.dir/all' failed
make[1]: *** [CMakeFiles/example-app.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
and the following CMakeError.log:
Determining if the pthread_create exist failed with the following output:
Change Dir: /root/Bela/projects/example-app/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_3bf71/fast"
/usr/bin/make -f CMakeFiles/cmTC_3bf71.dir/build.make CMakeFiles/cmTC_3bf71.dir/build
make[1]: Entering directory '/root/Bela/projects/example-app/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_3bf71.dir/CheckSymbolExists.c.o
/usr/bin/cc -o CMakeFiles/cmTC_3bf71.dir/CheckSymbolExists.c.o -c /root/Bela/projects/example-app/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTC_3bf71
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_3bf71.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/cmTC_3bf71.dir/CheckSymbolExists.c.o -o cmTC_3bf71 -rdynamic
CMakeFiles/cmTC_3bf71.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x4): undefined reference to `pthread_create'
CheckSymbolExists.c:(.text+0x8): undefined reference to `pthread_create'
CheckSymbolExists.c:(.text+0x20): undefined reference to `pthread_create'
CheckSymbolExists.c:(.text+0x24): undefined reference to `pthread_create'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/cmTC_3bf71.dir/build.make:97: recipe for target 'cmTC_3bf71' failed
make[1]: *** [cmTC_3bf71] Error 1
make[1]: Leaving directory '/root/Bela/projects/example-app/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_3bf71/fast' failed
make: *** [cmTC_3bf71/fast] Error 2
File /root/Bela/projects/example-app/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /root/Bela/projects/example-app/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_180e8/fast"
/usr/bin/make -f CMakeFiles/cmTC_180e8.dir/build.make CMakeFiles/cmTC_180e8.dir/build
make[1]: Entering directory '/root/Bela/projects/example-app/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_180e8.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_180e8.dir/CheckFunctionExists.c.o -c /usr/share/cmake-3.7/Modules/CheckFunctionExists.c
Linking C executable cmTC_180e8
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_180e8.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_180e8.dir/CheckFunctionExists.c.o -o cmTC_180e8 -rdynamic -lpthreads
/usr/bin/ld: cannot find -lpthreads
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/cmTC_180e8.dir/build.make:97: recipe for target 'cmTC_180e8' failed
make[1]: *** [cmTC_180e8] Error 1
make[1]: Leaving directory '/root/Bela/projects/example-app/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_180e8/fast' failed
make: *** [cmTC_180e8/fast] Error 2
I don't think the problem is cannot find -lpthreads
since I tried adding find_package(Threads)
to the CMakeLists and it did find the package (-- Found Threads: TRUE
)
A similar error is reported in https://stackoverflow.com/questions/31948521/building-error-using-cmake-cannot-find-lpthreads/57371779#57371779 and it is apparently due a version missmatch, but in my laptop it ran errorfree and did not complain about versions...
any ideas?