From 21758476d43ba894e5489802608f3cc88c084921 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 7 Feb 2026 21:31:12 +0100 Subject: [PATCH] - For `c3c init` with library templates, provide example exported functions. #2898 --- releasenotes.md | 1 + src/build/project_creation.c | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/releasenotes.md b/releasenotes.md index e896b18f6..fe7a74a8c 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -7,6 +7,7 @@ - Improve error message when using functions as values #2856 - Improve support for Android with Termux. - Integrated download of the MSVC SDK when compiling for Windows. +- For `c3c init` with library templates, provide example exported functions. #2898 ### Stdlib changes - Summarize sort macros as generic function wrappers to reduce the amount of generated code. #2831 diff --git a/src/build/project_creation.c b/src/build/project_creation.c index 846aef5d1..6a4e40c39 100644 --- a/src/build/project_creation.c +++ b/src/build/project_creation.c @@ -167,6 +167,18 @@ const char *MAIN_TEMPLATE = "\treturn 0;\n" "}\n"; +const char *LIB_TEMPLATE = + "module %s;\n" + "\n" + "fn int add(int a, int b) @export(\"add_something\")\n" + "{\n" + "\treturn a + b;\n" + "}\n\n\n" + "fn int sub(int a, int b) @export(\"sub_something\")\n" + "{\n" + "\treturn a - b;\n" + "}\n"; + const char *GITIGNORE_TEMPLATE = "build/\n" "out/\n"; @@ -259,17 +271,21 @@ void create_library(BuildOptions *build_options) void create_project(BuildOptions *build_options) { const char *template; + const char *main_template; if (!build_options->template || strcmp(build_options->template, "exe") == 0) { template = JSON_EXE; + main_template = MAIN_TEMPLATE; } else if (strcmp(build_options->template, "static-lib") == 0) { template = JSON_STATIC; + main_template = LIB_TEMPLATE; } else if (strcmp(build_options->template, "dynamic-lib") == 0) { template = JSON_DYNAMIC; + main_template = LIB_TEMPLATE; } else { @@ -327,7 +343,7 @@ CREATE: mkdir_or_fail(build_options, "src"); chdir_or_fail(build_options, "src"); - create_file_or_fail(build_options, "main.c3", MAIN_TEMPLATE, module_name(build_options)); + create_file_or_fail(build_options, "main.c3", main_template, module_name(build_options)); chdir_or_fail(build_options, ".."); mkdir_or_fail(build_options, "test");