# Swig doesn't generate Python-file when called via CMake

**URL:** <https://discourse.cmake.org/t/swig-doesnt-generate-python-file-when-called-via-cmake/6806>\
**Category:** Code\
**Created:** [November 7, 2022, 12:37pm UTC](https://discourse.cmake.org/t/swig-doesnt-generate-python-file-when-called-via-cmake/6806 "2022-11-07T12:37:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ottmar-zittlau](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ottmar-zittlau/32/2713_2.png) [@ottmar-zittlau](https://discourse.cmake.org/u/ottmar-zittlau)\
**Post date:** [November 7, 2022, 12:37pm UTC](https://discourse.cmake.org/t/swig-doesnt-generate-python-file-when-called-via-cmake/6806/1 "2022-11-07T12:37:12Z")

</div>

Hi,

I want to run Swig from CMake.  
My CMakeLists.txt looks like this:

```auto
cmake_minimum_required(VERSION 3.22.0)
project(PyTest CXX)

find_package(SWIG 4.0 REQUIRED python)
include(UseSWIG)

set(UseSWIG_MODULE_VERSION 1)
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_LIST_DIR}/bin)
set(SWIG_OUTFILE_DIR ${CMAKE_CURRENT_LIST_DIR}/bin)
set(SWIG_USE_TARGET_INCLUDE_DIRECTORIES TRUE)
set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
find_package(Python3 REQUIRED COMPONENTS Development)

#set(CMAKE_SWIG_FLAGS "")

swig_add_library(test TYPE SHARED LANGUAGE python SOURCES test.i)
set_target_properties(test PROPERTIES LINKER_LANGUAGE CXX)

target_link_libraries(test PRIVATE Python3::Module)

```

When I run this, a \_test.so file is created in my build-folder.  
However, it doesn’t create the desired bin-folders, it doesn’t generate a python-file and it doesn’t throw an error.

I have already tried to debug the UseSWIG.cmake file. What else can I do? Is Swig still supported by CMake or is it suggested to use a custom script for this purpose?

Thanks  
oz

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [November 7, 2022, 4:38pm UTC](https://discourse.cmake.org/t/swig-doesnt-generate-python-file-when-called-via-cmake/6806/2 "2022-11-07T16:38:43Z")

</div>

Why do you need a `.py` file? Can you `import _test` directly? Or is SWIG expected to also generate some Python-ic interface on top of its generated glue (I’m not that familiar with SWIG myself)?

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [November 10, 2022, 4:41pm UTC](https://discourse.cmake.org/t/swig-doesnt-generate-python-file-when-called-via-cmake/6806/3 "2022-11-10T16:41:56Z")

</div>

I am not able to reproduce your problem.

Are you sure there are no files generated under your bin directory, which is **inside** your source file tree? If you want to generate files in your build folder, use “${CMAKE\_CURRENT\_BINARY\_DIR}” rather than “${CMAKE\_CURRENT\_LIST\_DIR}”.
