Skip to content

Instantly share code, notes, and snippets.

@StanleyGoldman
Last active March 27, 2025 14:53
Show Gist options
  • Save StanleyGoldman/046e9cddad3e0c90c6d082749fdb1db0 to your computer and use it in GitHub Desktop.
Save StanleyGoldman/046e9cddad3e0c90c6d082749fdb1db0 to your computer and use it in GitHub Desktop.
Using swig_add_library in cmake
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}
)
#include "codebase.h"
#include <stdlib.h>
#include <string.h>
__declspec(dllexport) void helloWorld()
{
}
// codebase.h
#ifndef CODEBASE_H
#define CODEBASE_H
__declspec(dllexport) typedef struct {
int id;
} DataStruct;
__declspec(dllexport) void helloWorld();
#endif // CODEBASE_H
%module codebase_wrapper
%{
#include "codebase.h"
%}
%include <windows.i>
%include "codebase.h"
@StanleyGoldman
Copy link
Author

StanleyGoldman commented Mar 27, 2025

Software Details

  • cmake version 3.31.6
  • Microsoft Visual Studio 2022 Community
  • Windows 11

Running this example

C:\Users\Stanley\Documents\CMakeHelp> cmake -B build -S . -A x64
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.26100.
-- The CXX compiler identification is MSVC 19.43.34809.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SWIG: C:/Users/Stanley/AppData/Local/Microsoft/WinGet/Packages/SWIG.SWIG_Microsoft.Winget.Source_8wekyb3d8bbwe/swigwin-4.2.1/swig.exe (found version "4.2.1")
-- Configuring done (1.1s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Users/Stanley/Documents/CMakeHelp/build
C:\Users\Stanley\Documents\GitHub\CMakeHelp> cmake --build build --config Release
MSBuild version 17.13.19+0d9f5a35a for .NET Framework

  1>Checking Build System
  Swig compile codebase_wrapper.i for csharp
  Building Custom Rule C:/Users/Stanley/Documents/CMakeHelp/CMakeLists.txt
  codebase_wrapperCSHARP_wrap.cxx
     Creating library C:/Users/Stanley/Documents/CMakeHelp/build/Release/codebase_wrapper.lib and object C:/Users/S
  tanley/Documents/CMakeHelp/build/Release/codebase_wrapper.exp
codebase_wrapperCSHARP_wrap.obj : error LNK2019: unresolved external symbol "void __cdecl helloWorld(void)" (?helloWorld@@YA
XXZ) referenced in function CSharp_helloWorld [C:\Users\Stanley\Documents\CMakeHelp\build\codebase_wrapper.vcxproj] 
C:\Users\Stanley\Documents\CMakeHelp\build\Release\codebase_wrapper.dll : fatal error LNK1120: 1 unresolved externa 
ls [C:\Users\Stanley\Documents\CMakeHelp\build\codebase_wrapper.vcxproj]

Problem in build\codebase_wrapper.vcxproj

codebase.c is included in codebase_wrapper.vcxproj as None. Everything works if I were to manually change it to CLCompile.

<Project>
...
  <ItemGroup>
    <ClCompile Include="C:\Users\Stanley\Documents\CMakeHelp\build\Wrapper\codebase_wrapperCSHARP_wrap.cxx" />
    <None Include="C:\Users\Stanley\Documents\CMakeHelp\build\Generated\codebase_wrapper.cs">
    </None>
    <None Include="C:\Users\Stanley\Documents\CMakeHelp\build\Generated\codebase_wrapperPINVOKE.cs">
    </None>
    <None Include="C:\Users\Stanley\Documents\CMakeHelp\codebase.c">
    </None>
  </ItemGroup>
...
</Project>

@StanleyGoldman
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment