Swig with CMake not able to find header includes

Viewed 803

I am trying to run swig using CMake and I am getting

java.i:9: Error: Unable to find 'api.h'
java.i:10:Error: Unable to find 'TestStruct.h'

I have tried setting USE_LIBRARY_INCLUDE_DIRECTORIES, SWIG_USE_TARGET_INCLUDE_DIRECTORIES, SWIG_USE_LIBRARY_INCLUDE_DIRECTORIES, without any luck. I am probably failing to understand something that is going on. My understanding was that I am linking to the api target so those include directories should be visible.

Note: That it works if I copy those headers into the build directory where swig is getting called.

Any information will be helpful. Thanks.

java.i

 %module example
 %include "std_string.i"
 %{
 /* Put header files here or function declarations like below */
 #include "api.h"
 extern TestStruct TestFunc();
 %}

 %include "api.h"
 %include "TestStruct.h"
 extern TestStruct TestFunc();

CMakeLists.txt for the swig target

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

find_package(Java REQUIRED)
find_package(JNI REQUIRED)

set_property(SOURCE java.i PROPERTY CPLUSPLUS ON)

swig_add_library(javaExample
  TYPE SHARED
    LANGUAGE java
    SOURCES java.i)

target_include_directories(javaExample PRIVATE ${JNI_INCLUDE_DIRS})
target_link_libraries(javaExample PRIVATE api)

CMakeLists.txt for the api target

add_library( api SHARED 
    inc/api.h
    src/api.cpp
)

target_include_directories( api
  PUBLIC
    inc
)

target_link_libraries( api
  PUBLIC
    FooTypes
  PRIVATE
    FooBar
)

add_subdirectory(src/FooBar)

1 Answers

Found the problem. I was unfortunately using cmake 3.10 instead of latest (The default apt repo for cmake is old?). Upgraded to 3.17 and there was no issue.

All that is required is:

set_property(TARGET javaExample PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES TRUE)

Related