Undefined symbols in libTKMath

Hello all,

In short, I get the following error during run time:
/home/gordan/CLionProjects/untitled1/cmake-build-release/untitled1: symbol lookup error: /lib/x86_64-linux-gnu/libTKMath.so.7: undefined symbol: _ZN28TCollection_BasicMapIteratorC2Ev

My setup (working with linux machine):

1. Cloned OCCT repo from git (0032585)

2. Installed 3rd party libs by
sudo apt-get install tcllib tklib tcl-dev tk-dev libfreetype-dev libx11-dev libgl1-mesa-dev libfreeimage-dev rapidjson-dev
as described in the build documentation.

2. CMake config related to TK:
3RDPARTY_TK_DIR: <empty>
3RDPARTY_TK_INCLUDE_DIR: /usr/include/tcl (this was suspicious so tried also /usr/include/tk, although wouldn't think this would produce the given error)
3RDPARTY_TK_LIBRARY_DIR: /usr/lib/x86_64-linux-gnu
INSTALL_TK: OFF
USER_TK: ON

3. config passes, make and make install no issues

4. Created a simple "Hello gp_Pnt" program (see attached files: main.cpp and CMakeLists.txt) that compiles and links correctly. Runs correctly.

5. Tried adding a Cone from <BRepPrimAPI_MakeCone.hxx>.
- main.cpp compiles and links
- The error "symbol lookup error" appears during runtime.

6. Some debugging

ldd untitled1 | grep TK
libTKPrim.so.7 => /home/gordan/Programs/occt_install/lib/libTKPrim.so.7 (0x00007fd07879c000)
libTKernel.so.7 => /home/gordan/Programs/occt_install/lib/libTKernel.so.7 (0x00007fd0785ab000)
libTKTopAlgo.so.7 => /home/gordan/Programs/occt_install/lib/libTKTopAlgo.so.7 (0x00007fd0782a6000)
libTKBRep.so.7 => /lib/x86_64-linux-gnu/libTKBRep.so.7 (0x00007fd077d7f000)
libTKGeomBase.so.7 => /lib/x86_64-linux-gnu/libTKGeomBase.so.7 (0x00007fd077809000)
libTKG3d.so.7 => /lib/x86_64-linux-gnu/libTKG3d.so.7 (0x00007fd0776ce000)
libTKG2d.so.7 => /lib/x86_64-linux-gnu/libTKG2d.so.7 (0x00007fd07766c000)
libTKMath.so.7 => /lib/x86_64-linux-gnu/libTKMath.so.7 (0x00007fd0773ea000)
libTKGeomAlgo.so.7 => /lib/x86_64-linux-gnu/libTKGeomAlgo.so.7 (0x00007fd076c5a000)

As can be seen the executable links to /lib/x86_64-linux-gnu/libTKMath.so.7

nm -D /lib/x86_64-linux-gnu/libTKMath.so.7 | grep ZN28T
U _ZN28TCollection_BasicMapIterator10InitializeERK20TCollection_BasicMap
U _ZN28TCollection_BasicMapIterator4NextEv
U _ZN28TCollection_BasicMapIteratorC2ERK20TCollection_BasicMap
U _ZN28TCollection_BasicMapIteratorC2Ev

and, indeed the _ZN28TCollection_BasicMapIteratorC2Ev is undefined

7. A point of confusion for me.

If you look at the CMakeLists.txt I provided, I wanted to link:
add_library(libTKMath SHARED IMPORTED)
set_property(TARGET libTKMath PROPERTY IMPORTED_LOCATION "/home/gordan/Programs/occt_install/lib/libTKMath.so")

But as seen from the ldd of the executable the link points to a different location.

Looking at symbolic links from the occt installation folder the following is seen:
gordan@gordan-G3-3500:~/Programs/occt_install/lib$ ls -lh | grep libTKMath
lrwxrwxrwx 1 gordan gordan 14 Oct 1 23:00 libTKMath.so -> libTKMath.so.7
lrwxrwxrwx 1 gordan gordan 18 Oct 1 23:00 libTKMath.so.7 -> libTKMath.so.7.6.0
-rw-r--r-- 1 gordan gordan 2.7M Oct 1 21:44 libTKMath.so.7.6.0

Why is libTKMath.so.7.6.0 even built (and from where?) if native /lib/x86_64-linux-gnu/libTKMath.so.7 should be used?
Running 'nm -D libTKMath.so.7.6.0 | grep ZN28T' gives no results.

Questions:
So, how to deal with this situation?
Am I missing something in the config/build process?
Would compiling TK from source resolve this issue?

P.S. My first post here! Recently I started to explore OCC an I'm quite happy this library exists. I did some work on Windows where I compiled occt with VTK following Quaoar's tutorials which are very nice!

Thank you!
- Gordan

Attachments: 
Kirill Gavrilov's picture

You have built OCCT from source code (~/Programs/occt_install/) and also has installed OCCT libraries system-wide (/lib/x86_64-linux-gnu, most likely by installing some package from Linux repository - which one only you may tell).

Try reading about things like LD_LIBRARY_PATH to setup environment for using a proper version of OCCT libraries before starting your sample program.

Gordan Šegon's picture

Thanks, that did the trick!

I'm using CLion and the setup goes as follows:

Run->Edit Configurations->Environment variables
- add a user variable (LD_LIBRARY_PATH=/home/gordan/Programs/occt_install/lib)

same done manually in terminal:
gordan@gordan-G3-3500:~/CLionProjects/untitled1/cmake-build-release$ ldd untitled1 | grep TK
libTKPrim.so.7 => /home/gordan/Programs/occt_install/lib/libTKPrim.so.7 (0x00007f9c005d9000)
libTKernel.so.7 => /home/gordan/Programs/occt_install/lib/libTKernel.so.7 (0x00007f9c003e8000)
libTKTopAlgo.so.7 => /home/gordan/Programs/occt_install/lib/libTKTopAlgo.so.7 (0x00007f9c000e3000)
libTKBRep.so.7 => /lib/x86_64-linux-gnu/libTKBRep.so.7 (0x00007f9bffbc8000)
libTKGeomBase.so.7 => /lib/x86_64-linux-gnu/libTKGeomBase.so.7 (0x00007f9bff652000)
libTKG3d.so.7 => /lib/x86_64-linux-gnu/libTKG3d.so.7 (0x00007f9bff517000)
libTKG2d.so.7 => /lib/x86_64-linux-gnu/libTKG2d.so.7 (0x00007f9bff4b5000)
libTKMath.so.7 => /lib/x86_64-linux-gnu/libTKMath.so.7 (0x00007f9bff233000)
libTKGeomAlgo.so.7 => /lib/x86_64-linux-gnu/libTKGeomAlgo.so.7 (0x00007f9bfeaa3000)

gordan@gordan-G3-3500:~/CLionProjects/untitled1/cmake-build-release$ ./untitled1
./untitled1: symbol lookup error: /lib/x86_64-linux-gnu/libTKMath.so.7: undefined symbol: _ZN28TCollection_BasicMapIteratorC2Ev

gordan@gordan-G3-3500:~/CLionProjects/untitled1/cmake-build-release$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/gordan/Programs/occt_install/lib

gordan@gordan-G3-3500:~/CLionProjects/untitled1/cmake-build-release$ echo $LD_LIBRARY_PATH
:/home/gordan/Programs/occt_install/lib

gordan@gordan-G3-3500:~/CLionProjects/untitled1/cmake-build-release$ ./untitled1
Hello, World!
aPnt1: -0.5
shape.Closed(): 0

gordan@gordan-G3-3500:~/CLionProjects/untitled1/cmake-build-release$ ldd untitled1 | grep TK
libTKPrim.so.7 => /home/gordan/Programs/occt_install/lib/libTKPrim.so.7 (0x00007facaa150000)
libTKernel.so.7 => /home/gordan/Programs/occt_install/lib/libTKernel.so.7 (0x00007faca9f5f000)
libTKTopAlgo.so.7 => /home/gordan/Programs/occt_install/lib/libTKTopAlgo.so.7 (0x00007faca9c5a000)
libTKBRep.so.7 => /home/gordan/Programs/occt_install/lib/libTKBRep.so.7 (0x00007faca970c000)
libTKGeomBase.so.7 => /home/gordan/Programs/occt_install/lib/libTKGeomBase.so.7 (0x00007faca9204000)
libTKG3d.so.7 => /home/gordan/Programs/occt_install/lib/libTKG3d.so.7 (0x00007faca90d7000)
libTKG2d.so.7 => /home/gordan/Programs/occt_install/lib/libTKG2d.so.7 (0x00007faca9078000)
libTKMath.so.7 => /home/gordan/Programs/occt_install/lib/libTKMath.so.7 (0x00007faca8ddc000)
libTKGeomAlgo.so.7 => /home/gordan/Programs/occt_install/lib/libTKGeomAlgo.so.7 (0x00007faca8728000)

On a related note, in case anyone stumbles on this issue:
https://www.hpc.dtu.dk/?page_id=1180

Cheers!
- Gordan