# PYTHON\_wrap.cxx can not open file

**URL:** https://discourse.cmake.org/t/python-wrap-cxx-can-not-open-file/6039
**Category:** Usage
**Created:** [July 9, 2022, 5:19am UTC](https://discourse.cmake.org/t/python-wrap-cxx-can-not-open-file/6039 "2022-07-09T05:19:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zhang-qiang-github/32/1913_2.png) [@zhang-qiang-github](https://discourse.cmake.org/u/zhang-qiang-github)
#### Post date: [July 9, 2022, 5:19am UTC](https://discourse.cmake.org/t/python-wrap-cxx-can-not-open-file/6039/1 "2022-07-09T05:19:11Z")

</div>

I am using `swig` to wrap my c++ code to python. And my file structure is:

```auto
build
Project
    PackA
        PackA.h
        PackA.cpp
        PackA.i
        CMakeLists.txt (c)
    CMakeLists.txt (b)
CMakeLists.txt (a)

```

The `PackA.h/PackA.cpp/PackA.i` is:

```auto
// PackA.h
#ifndef PACKA_HEADER_H
#define PACKA_HEADER_H

#include "PackAGlobal.h"
#include <string>
class PackA
{
public:
	PackA();

};

#endif

// PackA.cpp
#include "PackA.h"
#include <iostream>
PackA::PackA()
{
	std::cout << "PackA" << std::endl;
}

// PackA.i
%module PackA

%{
#define SWIG_FILE_WITH_INIT
#include "PackA.h"
%}
%include "PackA.h"

```

And my `CMakeLists.txt (c)` is:

```auto
add_library(PackA SHARED PackA.h PackA.cpp)

SET_TARGET_PROPERTIES(PackA
	PROPERTIES
	RUNTIME_OUTPUT_DIRECTORY ${Bin_Root}
	ARCHIVE_OUTPUT_DIRECTORY ${Lib_Root}
)

set_property(SOURCE PackA.i PROPERTY CPLUSPLUS ON)
swig_add_library(PackAWrapping 
                LANGUAGE python 
                SOURCES PackA.i PackA.cpp)

```

Then, the `Configure` and `Generate` of `CMake` is OK, but vs reports a bug:

```auto
PackAPYTHON_wrap.cxx(2800,10): fatal error C1083: cannot open include file: “PackA.h”: No such file or directory

```

How can I fix this bug? Ang suggestion is appreciated~~~

---

<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: [July 9, 2022, 8:39am UTC](https://discourse.cmake.org/t/python-wrap-cxx-can-not-open-file/6039/2 "2022-07-09T08:39:39Z")

</div>

As clearly specified by the compiler error: the directory where `PackA.h` is located must be specified.  
Two possibilities:

```cmake
set_property(SOURCE PackA.i PROPERTY GENERATED_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}")

```

or

```auto
set_property(TARGET PackAWrapping TARGET INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}")

```

And by the way, what is the purpose of library `PackA`? Because library `PackAWrapping` contains already all the files (`PackA.i` and `PackA.cpp`)

---

<div class="post-metadata">

### Author: ![Mizux](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mizux/32/1920_2.png) [@Mizux](https://discourse.cmake.org/u/Mizux)
#### Post date: [June 26, 2023, 1:03pm UTC](https://discourse.cmake.org/t/python-wrap-cxx-can-not-open-file/6039/3 "2023-06-26T13:03:14Z")

</div>

My 2 cents,  
It seems strange to add PackA.cpp to the swig macro instead of linking PackWrapping to it:

```cmake
target_include_directories(PackA
 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

...

# Maybe needed too
target_include_directories(pyFoo PRIVATE . ${Python3_INCLUDE_DIRS})
set_property(TARGET PackWrapping PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON)

# PackA will provide include dir
target_link_libraries(PackWrapping PRIVATE PackA)

```
