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

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