# Adding cxx sources to swig\_add\_library

**URL:** <https://discourse.cmake.org/t/adding-cxx-sources-to-swig-add-library/13838>\
**Category:** Code\
**Created:** [March 27, 2025, 2:48pm UTC](https://discourse.cmake.org/t/adding-cxx-sources-to-swig-add-library/13838 "2025-03-27T14:48:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanprogram](https://discourse.cmake.org/user_avatar/discourse.cmake.org/stanprogram/32/5455_2.png) [@stanprogram](https://discourse.cmake.org/u/stanprogram)\
**Post date:** [March 27, 2025, 2:48pm UTC](https://discourse.cmake.org/t/adding-cxx-sources-to-swig-add-library/13838/1 "2025-03-27T14:48:39Z")

</div>

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

```auto
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](https://gist.github.com/StanleyGoldman/046e9cddad3e0c90c6d082749fdb1db0)

Thanks

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [March 27, 2025, 2:55pm UTC](https://discourse.cmake.org/t/adding-cxx-sources-to-swig-add-library/13838/2 "2025-03-27T14:55:42Z")

</div>

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

```cmake
project(codebase_wrapper LANGUAGES CXX C)

```

---

<div class="post-metadata">

**Author:** ![stanprogram](https://discourse.cmake.org/user_avatar/discourse.cmake.org/stanprogram/32/5455_2.png) [@stanprogram](https://discourse.cmake.org/u/stanprogram)\
**Post date:** [March 27, 2025, 3:04pm UTC](https://discourse.cmake.org/t/adding-cxx-sources-to-swig-add-library/13838/3 "2025-03-27T15:04:54Z")

</div>

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

```auto
project(codebase_wrapper LANGUAGES C)

```
