How to link to VTK library on Windows using g++ - vtk

I want to use the command line to compile a simple program using the VTK library:
g++ -IC:\VTK\Install\includes\vtk-5.10 SimpleTest.cpp -LC:\VTK\Install\lib
\vtk_5.10 -lvtkCommon -lvtkGraphics
SimpleTest.cpp
#include "vtkConeSource.h"
int main()
{
vtkConeSource* cone = vtkConeSource::New();
cone->SetHeight(5.0);
return 1;
}
but I always get many errors about "undefined references". For example:
C:\VTK\Install\lib\vtk_5.10/libvtkGraphics.a(vtkConeSource.cxx.obj):vtkConeSource.cxx:(.text+0x1c): undefined reference to `vtkInformationVector::GetInformationObject(int)'
C:\VTK\Install\lib\vtk_5.10/libvtkGraphics.a(vtkConeSource.cxx.obj):vtkConeSource.cxx:(.text+0x24): undefined reference to `vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES()'
C:\VTK\Install\lib\vtk_5.10/libvtkGraphics.a(vtkConeSource.cxx.obj):vtkConeSource.cxx:(.text+0x36): undefined reference to `vtkInformation::Set(vtkInformationIntegerKey*, int)'
C:\VTK\Install\lib\vtk_5.10/libvtkGraphics.a(vtkConeSource.cxx.obj):vtkConeSource.cxx:(.text+0x85): undefined reference to `vtkPolyDataAlgorithm::PrintSelf(std::ostream&, vtkIndent)'
C:\VTK\Install\lib\vtk_5.10/libvtkGraphics.a(vtkConeSource.cxx.obj):vtkConeSource.cxx:(.text+0x9f): undefined reference to `operator<<(std::ostream&, vtkIndent const&)'
and many more ...
I can't tell if I am using wrong syntax for g++ or if there is something wrong with my VTK library.
I am using:
VTK 5.10.1
minGW 4.7.2, 32 bit
VTK was compiled from source with minGW using CMake 2.8.11.2
update:
I did find this tutorial http://vtkblog.blogspot.com/2008/05/build-vtk-from-source-using-mingwmsys.html about this. However I used minGW32-make instead of installing MSYS and using it's "make". Could this be part of the problem?

As a very first test, try explicitly including all vtk libraries contained in Install\lib\vtk_5.10 - to ensure those undefined references are not coming from those missing libraries.

Related

Pytorch Tensor::data_ptr<long long>() not working on Linux

I cannot link my program to pytorch under Linux, get the following error:
/tmp/ccbgkLx2.o: In function `long long* at::Tensor::data<long long>() const':
test.cpp:(.text._ZNK2at6Tensor4dataIxEEPT_v[_ZNK2at6Tensor4dataIxEEPT_v]+0x14): undefined reference to `long long* at::Tensor::data_ptr<long long>() const'
I am building a very simple minimal example:
#include "torch/script.h"
#include <iostream>
int main() {
auto options = torch::TensorOptions().dtype(torch::kInt64);
torch::NoGradGuard no_grad;
auto T = torch::zeros(20, options).view({ 10, 2 });
long long *data = (long long *)T.data<long long>();
data[0] = 1;
return 0;
}
The command used to build it:
g++ -w -std=c++17 -o test-torch test.cpp -D_GLIBCXX_USE_CXX11_ABI=1 -Wl,--whole-archive -ldl -lpthread -Wl,--no-whole-archive -I../libtorch/include -L../libtorch/lib -ltorch -ltorch_cpu -lc10 -Wl,-rpath,../libtorch/lib
Pytorch has been downloaded from the link https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.7.0%2Bcpu.zip and unzipped (so I have the libtorch folder next to the folder with test.cpp).
Any ideas how to solve this problem? Same program works just fine under Visual C++.
P.S. I know pytorch is kind of designed for cmake, but I have zero experience with cmake and no desire to write a cmake-based build system for my app. Also, the examples they give are seemingly supposed to only work if pytorch is "installed" in the system. So I cannot just download the .zip with libs? And if I "install" it (e.g. from sources or in whatever other way) on an AVX512 system, will the binary I link to it and distribute to end-users work on non-AVX512? The documentation is completely incomprehensible for newbies.
UPDATE: I tried to do this via CMake following the tutorial https://pytorch.org/cppdocs/installing.html and got exactly the same error. Specifically, I renamed my directory to example-app and the source file to example-app.cpp. Then I created CMakeLists.txt in this directory with the following contents:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)
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 "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
Then
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=../../libtorch ..
cmake --build . --config Release
And here's the output:
CMakeFiles/example-app.dir/example-app.cpp.o: In function `long long* at::Tensor::data<long long>() const':
example-app.cpp:(.text._ZNK2at6Tensor4dataIxEEPT_v[_ZNK2at6Tensor4dataIxEEPT_v]+0x14): undefined reference to `long long* at::Tensor::data_ptr<long long>() const'
Makes me think, maybe I forgot to include some header or define some variable?
Oh, this is all on Mint 19.2 (equivalent to Ubuntu 18.04), g++ version is 7.5.0, glibc is 2.27. Compiling with g++-8 gives the same result.
This is not a cmake-related error, it's just how the library was implemented. I do not know why, but it appears that the specialization of T* at::Tensor::data<T> const with T = long long was forgotten/omitted.
If you want to get your signed 64-bits pointer, you can still get it with int64_t:
auto data = T.data<int64_t>();
It's good practice to use these types for which the size is explicit in general, in order to avoid compatibility issues.

Building gdb 10.1 from source with custom python

I'm trying to build the latest gdb 10.1 from source.
[My reason for wanting to do it is that I'm trying to debug a program that links to a custom build of Python 2.7.18, and my system gdb was linked to the build of Python 2.7.5 in my /lib64 directory and doesn't work with the newer version].
Having read through the README file, I have configured using:
../gdb-10.1/configure --with-python=<path to my 2.7.18 installation> --prefix=<path to where I want the new gdb to go>
...and then run
make all install
...per the instructions. However, every attempt to build then fails in a slew of error messages of the form:
python/py-arch.o: In function `gdbarch_to_arch_object(gdbarch*)':
.../build/gdb/../../../gdb-10.1/gdb/python/py-arch.c:86: undefined reference to `_Py_RefTotal'
python/py-arch.o: In function `gdbpy_ref_policy<_object>::decref(_object*)':
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_RefTotal'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_NegativeRefcount'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_Dealloc'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_RefTotal'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_RefTotal'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_RefTotal'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_NegativeRefcount'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_Dealloc'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_RefTotal'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_Dealloc'
.../build/gdb/../../../gdb-10.1/gdb/python/py-ref.h:36: undefined reference to `_Py_NegativeRefcount'
On inspecting the output of the configure step, and the Makefile itself, I can find no reference at all to the Python installation that I specified at configure time (and which I've also placed at the head of my LD_LIBRARY_PATH to ensure that the compiler and linker can find it when building).
What am I missing here?
I did something similar recently, and also struggled, although for different problems.
I suspect your build issue might be related to your use of LD_LIBRARY_PATH, or other things coming from your environment (PATH, CFLAGS, LDFLAGS etc). You shouldn't have to set these during the build.
Here's outline of what I did:
(1) For the build of gdb, I used approach like this:
export PATH=/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin
unset LD_LIBRARY_PATH
../gdb-10.1/configure --prefix=/opt/gdb-10.1 --with-python=/opt/conda-py2.7.18
make &> make.log
make install &> make-install.log
The set of PATH and unset of LD_LIBRARY_PATH are intended to sanitise the environment. This ensures the build can only use --with-python for locating python (which itself is at bin/python, under the python prefix). (CFLAGS & LDFLAGS were also not set, nor any PYTHON variables.)
I kept the output of the make stage. If you look in there you should see the with-python option is picked up.
This all built fine.
(2) To invoke the debugger (and use my python under /opt), I needed an extra step: to set LD_LIBRARY_PATH so that my pythons libpython is used:
export LD_LIBRARY_PATH=/opt/conda-py2.7.18/lib
/opt/gdb-10.1/bin/gdb
(gdb) python import sys; print(sys.version)
2.7.18 | Anaconda, Inc.
It would be nice to find a way to avoid this need to set LD_LIBRARY_PATH; this would possibly require linking libpython statically, or introduction of some build flags, eg, to use rpath.

OpenCV build Error: libwebp.so not found

I have two Arch Linux machines on one I can compile my code but on my new one I get the following errors:
:-1: warning: libwebp.so.5, needed by /usr/local/lib/libopencv_imgcodecs.so, not found (try using -rpath or -rpath-link)
/usr/local/lib/libopencv_imgcodecs.so:-1: error: undefined reference to `WebPEncodeBGRA'
/usr/local/lib/libopencv_imgcodecs.so:-1: error: undefined reference to `WebPDecodeBGRAInto'
/usr/local/lib/libopencv_imgcodecs.so:-1: error: undefined reference to `WebPEncodeLosslessBGR'
/usr/local/lib/libopencv_imgcodecs.so:-1: error: undefined reference to `WebPDecodeBGRInto'
/usr/local/lib/libopencv_imgcodecs.so:-1: error: undefined reference to `WebPEncodeLosslessBGRA'
/usr/local/lib/libopencv_imgcodecs.so:-1: error: undefined reference to `WebPGetFeaturesInternal'
/usr/local/lib/libopencv_imgcodecs.so:-1: error: undefined reference to `WebPEncodeBGR'
:-1: error: collect2: error: ld returned 1 exit status
My .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2015-11-15T16:30:56
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ColonyCounter
TEMPLATE = app
CONFIG += c++11
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
SOURCES += main.cpp\
mainwindow.cpp \
cellcounter.cpp
HEADERS += mainwindow.h \
cellcounter.h
FORMS += mainwindow.ui
DISTFILES += \
to-do.txt
How can I link the library or something like this, haven't found anything in the web right know.
I have checked my libs and found out that I do not have a libwep.so.5 but libwebp.so.6 and libwebp.so and libwebp.so.2, but I do not know how to fix it, install libwebp.so.5 or change something in my .pro file?
Looks like the version of OpenCV that you installed wants to link with an older version of libwebp (specifically version 0.4.4 which contains libwebp.so.5) than what you have installed (probably version 0.5.0-1). Some options are:
Downgrade libwebp to 0.4.4 and ignore libwebp 0.5.0-1 to avoid reverting back with future upgrades. This is probably not the best choice, but it is easy and would work unless you have other packages which depend on 0.5.0-1.
Manually download and build the 0.4.4 version of libwebp and install in a non-standard location. In this case, you would need to modify/use LD_LIBRARY_PATH to point opencv there. This is probably your easiest best option.
Download some more recent version of the OpenCV source and build it. It will find and link to your current libwep.so.6, if it is compatible. The latest version of OpenCV (as of this writing 3.1.0) is compatible. This option is the best option but the most involved as compiling OpenCV can be trivial or painful depending upon the features you want to include.
Now a comment about your currently marked solution: Making a link from one soname to another is highly not recommended. In many cases, it won't compile, but even if it does, your application may exhibit arbitrary and unstable behavior and/or segfault unless the binaries are completely compatible. But if they were compatible, the packager would probably not have changed the soname. If this is for a school project, you might be ok, but if this is for anything important, don't do it.
Hope this helps.
I solved the problem with a not so elegant solution:
I just created a symbolic link:
$ file libwebp.so.5
libwebp.so.5: symbolic link to libwebp.so.6.0.0
with:
ln /usr/lib/libwebp.so.6 /usr/lib/libwebp.so.5

Can't include OpenCV functions in Matlab in Linux

I have a .cpp that I'm trying to mex using MATLAB R2011a in Ubuntu 14.04. The file uses some OpenCV functions and imports the correponding library:
#include <opencv/cv.h>
I'm mexing using the following command in Matlab:
mex -I/usr/local/include FaceDetect.cpp
As the opencv libraries are installed in /usr/local/include/opencv. The crash result is as follows:
Warning: You are using gcc version "4.8.2-19ubuntu1)". The version
currently supported with MEX is "4.3.4".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
FaceDetect.o: In function `mexFunction':
FaceDetect.cpp:(.text+0xc6): undefined reference to `cvLoad'
FaceDetect.cpp:(.text+0x132): undefined reference to `cvCreateMemStorage'
FaceDetect.cpp:(.text+0x155): undefined reference to `cvCreateImage'
FaceDetect.cpp:(.text+0x23a): undefined reference to `cvHaarDetectObjects'
FaceDetect.cpp:(.text+0x285): undefined reference to `cvGetSeqElem'
FaceDetect.cpp:(.text+0x354): undefined reference to `cvReleaseImage'
FaceDetect.cpp:(.text+0x393): undefined reference to `cvClearMemStorage'
FaceDetect.cpp:(.text+0x39f): undefined reference to `cvReleaseHaarClassifierCascade'
collect2: error: ld returned 1 exit status
mex: link of ' "FaceDetect.mexa64"' failed.
??? Error using ==> mex at 208
Unable to complete successfully
I have tried compiling with -L and -lcv and it still crashes. What I am doing wrong?
The Computer Vision System Toolbox now includes a support package for OpenCV interface. It includes a library of conversion routines between MATLAB and OpenCV data types, and an easy way to compile mex functions, which use OpenCV.

PJSIP libsocket compile error

I get the following error when I try to compile the pjsip libraries in the Blackberry 10 IDE:
libpj-i486-pc-nto-qnx8.0.0.a(sock_bsd.o): undefined reference to symbol 'send##libsocket.so.2'
'send##libsocket.so.2' is defined in DSO /home/f3795594/bbndk/target_10_0_9_386/qnx6/x86/lib/libsocket.so.3 so try adding it to the linker command line
Has anybody come across this error? I know it's to do with the libsocket library but not sure where to begin.
Thanks
Simple insertion into linking process:
Add the following library:
LIBS += /Applications/bbndk/target_10_0_9_386/qnx6/x86/lib/libsocket.so.3
My limited c compile knowledge got in the way.

Resources