[CMake] UseSWIG creates module instead of shared library, which can't be dynamically loaded

Stephen Roderick kiwi.net at mac.com
Thu Aug 6 08:27:03 EDT 2009


On Aug 6, 2009, at 06:02 , Mathieu Malaterre wrote:

> On Wed, Aug 5, 2009 at 6:55 PM, Stephen Roderick<kiwi.net at mac.com>  
> wrote:
>> The existing UseSWIG.cmake file creates a MODULE and not a SHARED  
>> library.
>> The module is unuseable by any program trying to load dynamic  
>> libraries (eg
>> wrapped Java). The attached patch fixes this problem for our  
>> situations.
>> Demonstrated on both Mac OS X Leopard and Ubuntu Jaunty.
>>
>> Is this a known issue? I searched what I could of the list and the  
>> 'net, and
>> couldn't find anything on this.
>
> No, you are confusing the C++ library and the Java binding. Instead:
>
> ADD_LIBRARY(foo SHARED ${foo_SRCS}) # the actual shared lib
> SWIG_ADD_MODULE(foojni java foo.i)
> SWIG_LINK_LIBRARIES(foojni foo
>  ${JNI_LIBRARIES}
> )
>
> The next time you will want to create -say- a python module you'll
> simply link to your *shared C++* library:
>
> SWIG_ADD_MODULE(foopython python foo.i)
> SWIG_LINK_LIBRARIES(foopython foo
>  ${PYTHON_LIBRARIES}
> )

Unfortunately, that is what we currently have.

<code>
   ADD_LIBRARY(MyInterfaceCpp ...)

   Find_Package(SWIG REQUIRED)
   Find_Package(JNI REQUIRED)
   INCLUDE(UseSWIG)
   INCLUDE_DIRECTORIES(${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})

   SET_SOURCE_FILES_PROPERTIES(MyInterface.i PROPERTIES CPLUSPLUS 1)
   # compile swig with package flag
   SET(CMAKE_SWIG_FLAGS -package x.y.z)

   SWIG_ADD_MODULE(MyInterface java MyInterface.i)
   SWIG_LINK_LIBRARIES(MyInterface
	MyInterfaceCpp ...)
</code>

The failure comes when you try to load the SWIG-output, JNI library  
from within java, using something like
<code>
	static {
		System.loadLibrary("MyInterface");
	}
</code>

With a module in a "MyInterface.so" file (what SWIG currently  
outputs), the above call fails on both Mac OS X and Linux. They both  
need a correctly named dynamic library, hence the patch.

After searching some more I did find a previous post of this on the  
CMake ML, and the solution there is basically the same as mine (except  
the user copied the SWIG macro into their own CMakeLists.txt and then  
made the same changes). If that poster and myself are not doing  
anything wrong, then UseSWIG.cmake needs to be modified to support  
both naming styles. Else others will just hit this again in the future.

http://www.cmake.org/pipermail/cmake/2008-October/024727.html

TIA
Stephen


More information about the CMake mailing list