Adding cxx sources to swig_add_library

What is the correct way to add additional cxx sources with swig_add_library?

I am having trouble building an example for myself. It seems like what I am doing should work.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)
project(codebase_wrapper LANGUAGES CXX)

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

include_directories(${CMAKE_SOURCE_DIR})
set(SOURCES codebase_wrapper.i codebase.c)

set_property(SOURCE codebase_wrapper.i PROPERTY CPLUSPLUS ON)

swig_add_library(${PROJECT_NAME}
TYPE SHARED
LANGUAGE csharp
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/Generated"
OUTFILE_DIR "${CMAKE_CURRENT_BINARY_DIR}/Wrapper"
SOURCES ${SOURCES}
)

codebase_wrapper.i and codebase.c both reference codebase.h, which defines a very simple hello world function, but the cmake file results in unresolved external errors.

Investigating the vcxproj file reveals that codebase.c is included with a None tag.
If it were included with a CLCompile tag there would not be unresolved externals.

My understanding of SOURCES in swig_add_library leads me to believe this is possible.
What am I doing wrong or misunderstanding?

All content can be found in the following gist: https://gist.github.com/StanleyGoldman/046e9cddad3e0c90c6d082749fdb1db0

Thanks

1 Like

To use C language files (i.e. files with extension .c), you have to specify it:

project(codebase_wrapper LANGUAGES CXX C)

Thanks for the help and such a prompt reply. In my scenario changing the line as follows fixed my problem.

project(codebase_wrapper LANGUAGES C)