PYTHON_wrap.cxx can not open file

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

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:

// 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:

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:

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~~~

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

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

or

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)

1 Like

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

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)