686InSomNia686 686InSomNia686 - 1 month ago 20
C# Question

CMake, SWIG & shared library

Good Morning everyone,

I contact you because I am stuck on a problem.
I am using CMake to compile and build a SWIG wrapper C++/C# dynamic library.
This wrapper allows me to use a DLL C++ with a C# execute program.

I would like to be abble to launch CTEST on my library to verify that I have no bug.
The problem with "Windows" library is that you need to export the symbol to be abble to use it.
With a usual library, I will add the command "SHARED" to the "add_library" :

add_library( ${PROJECT_NAME} SHARED ${${PROJECT_NAME}_SOURCES} ${${PROJECT_NAME}_HEADERS} )


But to build the library threw SWIG I need to use "SWIG_ADD_MODULE" which doesn't know the command "SHARED".

The only solution that I have found yet, it's to create two libraries.
One builds by SWIG and used by the C# code.
The second builds usually with "add_library" and used by my CTEST program.
This is my CMakeList :

###############################
# SWIG LYBRARY
###############################
include(GenerateExportHeader)
SET (MODULE_NAME Mylib)
SET (INTERFACE_FILES Mylib.i)

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

# call swig as: 'swig -csharp -c++ -outdir XXX'
SET_SOURCE_FILES_PROPERTIES(${INTERFACE_FILES} PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(${INTERFACE_FILES} PROPERTIES SWIG_FLAGS "-includeall")

SWIG_ADD_MODULE(${MODULE_NAME} csharp ${INTERFACE_FILES} ${${PROJECT_NAME}_SOURCES} ${${PROJECT_NAME}_HEADERS} )
SWIG_LINK_LIBRARIES(${MODULE_NAME} ${CSHARP_LIBRARIES})
###############################
# DYNAMIC LYBRARY
###############################
add_library( ${PROJECT_NAME}Tester SHARED ${${PROJECT_NAME}_SOURCES} ${${PROJECT_NAME}_HEADERS} )
generate_export_header(${PROJECT_NAME}Tester EXPORT_MACRO_NAME OTHER_NAME_EXPORT)


I wish I could find a way to build only one and useable by both.
Thanks for your help and have a nice weekend,
Clément

Answer

@fedino @Scollii Thanks for your answers, finally I have solved my problem by creating a macro in CMake which compilling the *.o of the swig command and use it back into the "add_library". With that solution I have a real SHARED library that I can use for my test.