From ceaf6ef05eb50b84c5de159a71e5869e2ac38f85 Mon Sep 17 00:00:00 2001 From: Bernhard Guillon Date: Tue, 30 Jun 2026 20:29:11 +0200 Subject: Add FooBar library with CMake build integration --- CMakeLists.txt | 26 +++++++++++++++++++++++++- FooBar.cs | 10 ++++++++++ Program.cs | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 FooBar.cs diff --git a/CMakeLists.txt b/CMakeLists.txt index b76d217..2f12093 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,28 @@ set(CSC_DLL ${DOTNET_ROOT}/sdk/10.0.109/Roslyn/bincore/csc.dll) set(RUNTIME_DIR ${DOTNET_ROOT}/shared/Microsoft.NETCore.App/10.0.9) set(OUTPUT_DIR ${CMAKE_BINARY_DIR}) + +# FooBar library +set(FOOBAR_SOURCES ${CMAKE_SOURCE_DIR}/FooBar.cs) +set(FOOBAR_OUTPUT ${OUTPUT_DIR}/FooBar.dll) + +add_custom_command( + OUTPUT ${FOOBAR_OUTPUT} + COMMAND dotnet ${CSC_DLL} + -target:library + -out:${FOOBAR_OUTPUT} + -lib:${RUNTIME_DIR} + -reference:System.Private.CoreLib.dll + -reference:System.Runtime.dll + -reference:System.Console.dll + ${FOOBAR_SOURCES} + DEPENDS ${FOOBAR_SOURCES} + COMMENT "Compiling FooBar library with Roslyn csc..." +) + +add_custom_target(FooBar ALL DEPENDS ${FOOBAR_OUTPUT}) + +# HelloCMake executable set(SOURCES ${CMAKE_SOURCE_DIR}/Program.cs) set(TARGET HelloCMake) set(OUTPUT_FILE ${OUTPUT_DIR}/${TARGET}.dll) @@ -19,13 +41,15 @@ add_custom_command( -reference:System.Private.CoreLib.dll -reference:System.Runtime.dll -reference:System.Console.dll + -reference:${FOOBAR_OUTPUT} -main:Program ${SOURCES} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/HelloCMake.runtimeconfig.json ${OUTPUT_DIR}/${TARGET}.runtimeconfig.json - DEPENDS ${SOURCES} + DEPENDS ${SOURCES} ${FOOBAR_OUTPUT} COMMENT "Compiling ${TARGET} with Roslyn csc..." ) add_custom_target(${TARGET} ALL DEPENDS ${OUTPUT_FILE}) +add_dependencies(${TARGET} FooBar) diff --git a/FooBar.cs b/FooBar.cs new file mode 100644 index 0000000..1ccbe3b --- /dev/null +++ b/FooBar.cs @@ -0,0 +1,10 @@ +using System; + +public class FooBar +{ + public static void Print() + { + Console.WriteLine("FooBar"); + Console.WriteLine("FooBar"); + } +} diff --git a/Program.cs b/Program.cs index 5dd49a9..33e286c 100644 --- a/Program.cs +++ b/Program.cs @@ -5,5 +5,6 @@ class Program static void Main() { Console.WriteLine("Hello, CMake + C#!"); + FooBar.Print(); } } -- cgit v1.2.3