# undefind symbol in Swig python build

**URL:** https://discourse.cmake.org/t/undefind-symbol-in-swig-python-build/1331
**Category:** Usage
**Created:** [June 4, 2020, 8:28pm UTC](https://discourse.cmake.org/t/undefind-symbol-in-swig-python-build/1331 "2020-06-04T20:28:35Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![poseid](https://discourse.cmake.org/user_avatar/discourse.cmake.org/poseid/32/666_2.png) [@poseid](https://discourse.cmake.org/u/poseid)
#### Post date: [June 4, 2020, 8:28pm UTC](https://discourse.cmake.org/t/undefind-symbol-in-swig-python-build/1331/1 "2020-06-04T20:28:35Z")

</div>

Hello,  
I am trying to build an example swig module for Python3. The build works fine but the resulting module is not working. The symbol can not be found.

CMakeLists.txt

```auto
cmake_minimum_required(VERSION 3.12)

project(swighello)

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PACKAGE(TCL)
FIND_PACKAGE(Python3 COMPONENTS Interpreter Development)
message(STATUS ${Python3_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${Python3_INCLUDE_DIR})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-includeall")

add_library(mylib example.cc)
add_executable(hello hello.cc)
target_link_libraries(hello mylib)

swig_add_library(pyexample TYPE SHARED LANGUAGE python SOURCES pyexample.i example.cc)
SWIG_ADD_LIBRARY(example LANGUAGE tcl SOURCES example.i example.cc)

SWIG_LINK_LIBRARIES(pyexample ${Python3_LIBRARY} mylib)
SWIG_LINK_LIBRARIES(example ${TCL_LIBRARIES})

```

Pyexample.i

```auto

/* example.i */
%module pyexample
%{
  /* Put header files here or function declarations like below */

  /* extern int fact(int n); */
  #include "mylib.h"
%}

%include "mylib.h"
/*extern int fact(int n);*/

```

pytest.py

```auto
import pyexample

```

Gives

```auto
python3 pytest.py
Traceback (most recent call last):
  File "pytest.py", line 1, in <module>
    import pyexample
  File "/a/projects/swig/cmake2/build/pyexample.py", line 15, in <module>
    import _pyexample
ImportError: /a/projects/swig/cmake2/build/_pyexample.so: undefined symbol: fact

```
