Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linker problem: undefined reference to `cv::imread(std::string const&, int)' #7

Closed
TheTimKiely opened this issue Jun 3, 2020 · 1 comment

Comments

@TheTimKiely
Copy link

Thanks for the great sample.
I can't seem to this project to link if I include both OpenCV and Torch.
When both libraries are included in the cmake file, I get the error:
PyTorch-CPP/prediction.cpp:22: undefined reference to `cv::imread(std::string const&, int)'
collect2: error: ld returned 1 exit status

If I remove all references to Torch, the project builds, links, and runs.

Any suggestions would be appreciated.
I asked this question on StackOverflow, but I haven't gotten any responses.

Here the CMakeError.txt file:
erforming C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /home/tim/samples/PyTorch-CPP/cmake-build-debug/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/make cmTC_3c4f7/fast && /usr/bin/make -f CMakeFiles/cmTC_3c4f7.dir/build.make CMakeFiles/cmTC_3c4f7.dir/build
make[1]: Entering directory '/home/tim/samples/PyTorch-CPP/cmake-build-debug/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_3c4f7.dir/src.c.o
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_3c4f7.dir/src.c.o -c /home/tim/samples/PyTorch-CPP/cmake-build-debug/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_3c4f7
/home/tim/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/201.7223.86/bin/cmake/linux/bin/cmake -E cmake_link_script CMakeFiles/cmTC_3c4f7.dir/link.txt --verbose=1
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD -rdynamic CMakeFiles/cmTC_3c4f7.dir/src.c.o -o cmTC_3c4f7
/usr/bin/ld: CMakeFiles/cmTC_3c4f7.dir/src.c.o: in function main': src.c:(.text+0x46): undefined reference to pthread_create'
/usr/bin/ld: src.c:(.text+0x52): undefined reference to pthread_detach' /usr/bin/ld: src.c:(.text+0x63): undefined reference to pthread_join'
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_3c4f7.dir/build.make:87: cmTC_3c4f7] Error 1
make[1]: Leaving directory '/home/tim/samples/PyTorch-CPP/cmake-build-debug/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_3c4f7/fast] Error 2

Source file was:
#include <pthread.h>

void* test_func(void* data)
{
return data;
}

int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, test_func, NULL);
pthread_detach(thread);
pthread_join(thread, NULL);
pthread_atfork(NULL, NULL, NULL);
pthread_exit(NULL);

return 0;
}

Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/tim/samples/PyTorch-CPP/cmake-build-debug/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/make cmTC_6b9bf/fast && /usr/bin/make -f CMakeFiles/cmTC_6b9bf.dir/build.make CMakeFiles/cmTC_6b9bf.dir/build
make[1]: Entering directory '/home/tim/samples/PyTorch-CPP/cmake-build-debug/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_6b9bf.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_6b9bf.dir/CheckFunctionExists.c.o -c /home/tim/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/201.7223.86/bin/cmake/linux/share/cmake-3.16/Modules/CheckFunctionExists.c
Linking C executable cmTC_6b9bf
/home/tim/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/201.7223.86/bin/cmake/linux/bin/cmake -E cmake_link_script CMakeFiles/cmTC_6b9bf.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -rdynamic CMakeFiles/cmTC_6b9bf.dir/CheckFunctionExists.c.o -o cmTC_6b9bf -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_6b9bf.dir/build.make:87: cmTC_6b9bf] Error 1
make[1]: Leaving directory '/home/tim/samples/PyTorch-CPP/cmake-build-debug/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_6b9bf/fast] Error 2

I looked for the cause of the error "/usr/bin/ld: cannot find -lpthreads".
I don't think that is relevant. The linker command is looking for 'pthread', not 'pthreads:
$ cat link.txt
/usr/bin/c++ -g -rdynamic CMakeFiles/classifier.dir/prediction.cpp.o -o classifier -Wl,-rpath,/usr/local/lib:/home/tim/tools/libtorch/lib /usr/local/lib/libopencv_dnn.so.4.3.0 /usr/local/lib/libopencv_gapi.so.4.3.0 /usr/local/lib/libopencv_highgui.so.4.3.0 /usr/local/lib/libopencv_ml.so.4.3.0 /usr/local/lib/libopencv_objdetect.so.4.3.0 /usr/local/lib/libopencv_photo.so.4.3.0 /usr/local/lib/libopencv_stitching.so.4.3.0 /usr/local/lib/libopencv_video.so.4.3.0 /usr/local/lib/libopencv_videoio.so.4.3.0 /home/tim/tools/libtorch/lib/libtorch.so /home/tim/tools/libtorch/lib/libc10.so /usr/local/lib/libopencv_imgcodecs.so.4.3.0 /usr/local/lib/libopencv_calib3d.so.4.3.0 /usr/local/lib/libopencv_features2d.so.4.3.0 /usr/local/lib/libopencv_flann.so.4.3.0 /usr/local/lib/libopencv_imgproc.so.4.3.0 /usr/local/lib/libopencv_core.so.4.3.0 -Wl,--no-as-needed,/home/tim/tools/libtorch/lib/libtorch_cpu.so -Wl,--as-needed /home/tim/tools/libtorch/lib/libc10.so -lpthread -Wl,--no-as-needed,/home/tim/tools/libtorch/lib/libtorch.so -Wl,--as-needed

libpthread is in "/usr/lib/x86_64-linux-gnu":
$ ls /usr/lib/x86_64-linux-gnu/libpth*.*
/usr/lib/x86_64-linux-gnu/libpthread-2.31.so /usr/lib/x86_64-linux-gnu/libpthread.so
/usr/lib/x86_64-linux-gnu/libpthread.a /usr/lib/x86_64-linux-gnu/libpthread.so.0

And that directory is found by "ld":
ld --verbose | grep SEARCH_DIR
SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib");

@TheTimKiely
Copy link
Author

I found that the object file created with a reference to Torch looks different than the object file without a reference to Torch.
My C++ experience is not very deep, so I don't know what to do with that.
The good object file has a reference to imread like this:
_ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi

The bad object file:
_ZN2cv6imreadERKSsi

Is there any way to troubleshoot the compiler to figure out what's happenting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants