VTK Undefined Reference - c++

I installed VTK 7.1.1 as suggested here.
I'm working on a new project with the following code:
#include <iostream>
#include <vtk-7.1/vtkImageData.h>
#include <vtk-7.1/vtkMetaImageReader.h>
#include <vtk-7.1/vtkSmartPointer.h>
#include <vtk-7.1/vtkRenderer.h>
#include <vtk-7.1/vtkImageActor.h>
#include <vtk-7.1/vtkImageMapper3D.h>
#include <vtk-7.1/vtkRenderWindow.h>
#include <vtk-7.1/vtkRenderWindowInteractor.h>
int main()
{
vtkSmartPointer<vtkMetaImageReader> reader = vtkSmartPointer<vtkMetaImageReader>::New();
reader->SetFileName("Test.mhd");
reader->Update();
std::cout<<"Hello World";
return 0;
}
I am using the following to compile:
g++ -g -Wall -I /usr/local/include -L /usr/local/lib -o main main.cpp
However, I keep getting errors such as the following:
/usr/local/include/vtk-7.1/vtkSmartPointer.h:29: error: undefined reference to `vtkSmartPointerBase::~vtkSmartPointerBase()'
Any idea as to why? I think it might be the linking maybe?

Your assumption is correct the linker command is missing the required VTK libraries. Look in the following link for more details VTK in Makefiles.
For instance
g++ -g -Wall -I /usr/local/include -L/usr/local/lib -lvtkCommon
-lvtkFiltering -lvtkImaging -lvtkGraphics -lvtkGenericFiltering -lvtkIO
-lvtkRendering -lvtkVolumeRendering
To ease building your VTK applications you could use CMake

Related

Unable to statically link boost-locale due to libicu linking errors on Ubuntu 18.04

The following code:
#include <boost/locale.hpp>
int main()
{
using namespace boost::locale;
generator gen;
}
Built with the following flags:
g++ -static -static-libstdc++ -pthread main.cpp -lboost_locale -lboost_system -licuuc -licudata -licui18n
Fails to link with the following errors:
(.text+0x16b8): undefined reference to `icu_60::CacheKeyBase::~CacheKeyBase()'
(.text+0x1c8d): undefined reference to `icu_60::UVector64::~UVector64()'
(.text+0x7dc): undefined reference to `icu_60::UCharsTrie::Iterator::~Iterator()'
...hundreds of other undefined symbols
...(the sample code also has boost linking errors, but let's ignore them, as they only appear in my attempt of reproduction; the real code I have issues with has only icu-related link errors)
I'm using Ubuntu 18.04, with both libboost-all-dev and libicu-dev installed from the Ubuntu repositories.
I'm probably missing some trivial component/flag/library needed to make it link together, but am unable to find it.

gmp.h missing when trying to use boost library

I am trying to use the boost library with QT on windows. I've successfully build the library and also managed to include it in my project. However, on including gmp (#include "boost/multiprecision/gmp.hpp") and creating an object (boost::multiprecision::mpz_int myint;) I get the following error:
C:\Users\Laurenz\Documents\libraries\boost_1_66_0\include\boost\multiprecision\gmp.hpp:31: error: gmp.h: No such file or directory
And indeed, I haven't been able to find any such file in the boost directory. What did I do wrong?
Install the dependency and link to it. (See What is an undefined reference/unresolved external symbol error and how do I fix it?)
Alternatively, consider not using GMP, using cpp_int.hpp instead.
Since you already installed the GMP library, here's the last step:
Live On Coliru
#include <boost/multiprecision/gmp.hpp>
#include <iostream>
int main() {
boost::multiprecision::mpz_int i("1238192389824723487823749827349879872342834792374897923479");
std::cout << pow(i, 3) << "\n";
}
Note the -lgmp flag at the end of the compile/link command:
g++ -std=c++11 -O2 -Wall -Wextra -pedantic main.cpp -o demo -lgmp
Running it:
./demo
1898298004808110659499396020993351679788129852647955073547637871096272981567489303363372689896302906549189545322451852317205769760555889831589125591739044248515246136031239

C++ Include path on x86_64-w64-mingw32-g++ with OpenGL

I am trying to get an OpenGL program working on both linux and windows.
Here's my code [file=main.cc]:
#include <iostream>
#include "GL/glew.h"
using namespace std;
int main(int argc, char *argv[]){
cout << "Hello World\n";
return 0;
}
Simple enough. I'm on Linux and using
g++ main.cc -lGL -lGLEW -lSDL2
to compile my program. It works perfectly fine and if I run ./a.out I get a Hello World on my screen.
Then I try to compile it on Linux for Windows using the command
x86_64-w64-mingw32-g++ main.cc -lGL -LGLEW -LSDL2
Then however i get the error:
main.cc:3:21: fatal error: GL/glew.h: No such file or directory
#include "GL/glew.h"
^
compilation terminated.
I've already tried adding the -I/inclulde/path option with paths like /usr/include /usr/include/GL usr/include and the like, yet nothing wants to compile.
The Libraries that I'm using (or planning to) were installed using
#apt install libgl-dev libglew-dev libsdl2-dev
Any help would be very much appreciated (although I feel this is an incredibly easy fix that I'm just too stupid to figure out on my own)

C++ library header not found

I am OS X user and I've recently installed "cppunit" library using brew. When I try to compile "test.cpp" file using TestCase.h header the error occurs:
> test.cpp:3:10: fatal error: 'TestCase.h' file not found
> #include "TestCase.h"
I am compiling this file:
test.cpp
#include <iostream>
#include "TestCase.h"
using namespace CppUnit;
class EmptyTest : public TestCase
{
};
int main()
{
}
Using this command:
g++ -Wall -pedantic -std=c++14 test.cpp -o test.x -lcppunit
I've also tried compiling with -I giving the path to the library directory but still with the same error.
All my friends using cppunit and brew can simply include the header and the program works fine.
I would appreciate every answer.
I've solved this problem. I had issues with Xcode. Reinstall works fine.

Undefined reference to boost::timer::auto_cpu_timer

I try to compile small .cpp file with boost library on remote server on Debian using g++ 4.4. I use Netbeans for this purpose. My home machine is on windows 7. After resolving some issues with linking next code
#include <boost/timer/timer.hpp>
#include <iostream>
#include <string>
int main()
{
boost::timer::auto_cpu_timer ac; //line 5
return 0; //line 6
}
Produces 2 errors:
line 5: undefined reference to boost::timer::auto_cpu_timer::auto_cpu_timer(short)'
line 6: undefined reference to boost::timer::auto_cpu_timer::~auto_cpu_timer()'
Same result if i use header boost/thread.hpp but for thread constructor/destructor.
But for example boost/shared_ptr compiles without any problem.
Result compile command in neatbeans is
g++ -m64 -I/usr/include/boost/boost_1_49_0 -lboost_system -o dist/Debug/GNU-Linux-x86/test build/Debug/GNU-Linux-x86/main.o
-L/usr/include/boost/boost_1_49_0/stage/lib -Wl,-rpath /usr/include/boost/boost_1_49_0/stage/lib build/Debug/GNU-Linux-x86/main.o
What i missed?
You need to link against boost_timer. Add -lboost_timer to the gcc command line. Consult the Netbeans documentation on how to add libraries to a project.