Skip to content

Commit 61945e1

Browse files
committed
Library Example Modules
1 parent 865882e commit 61945e1

File tree

6 files changed

+64
-46
lines changed

6 files changed

+64
-46
lines changed

examples/conan_cmake/library/CMakeLists.txt

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.30)
1+
cmake_minimum_required(VERSION 4.0)
22

33
project(mathutils
44
VERSION 1.0.0
@@ -12,26 +12,23 @@ include(CMakePackageConfigHelpers)
1212
# Dependencies
1313
find_package(fmt REQUIRED)
1414

15-
# Library target
16-
add_library(mathutils src/mathutils.cpp)
15+
# Library target with module support
16+
add_library(mathutils)
1717
add_library(mathutils::mathutils ALIAS mathutils)
1818

19-
target_compile_features(mathutils PUBLIC cxx_std_17)
20-
target_include_directories(mathutils PUBLIC
21-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
22-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
19+
target_sources(mathutils
20+
PUBLIC
21+
FILE_SET CXX_MODULES FILES
22+
src/mathutils.ixx
2323
)
24+
25+
target_compile_features(mathutils PUBLIC cxx_std_23)
2426
target_link_libraries(mathutils PUBLIC fmt::fmt)
2527

2628
# Installation
2729
install(TARGETS mathutils
2830
EXPORT mathutilsTargets
29-
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
30-
)
31-
32-
install(DIRECTORY include/
33-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
34-
FILES_MATCHING PATTERN "*.h*"
31+
FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_LIBDIR}/cxx/miu
3532
)
3633

3734
install(EXPORT mathutilsTargets
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from conan import ConanFile
2+
from conan.tools.cmake import CMakeDeps, CMakeToolchain
3+
4+
5+
class MathUtilsConan(ConanFile):
6+
name = 'mathutils'
7+
version = '1.0.0'
8+
9+
settings = 'os', 'compiler', 'build_type', 'arch'
10+
11+
def configure(self):
12+
# Enable fmt module support
13+
self.options['fmt'].with_modules = True
14+
15+
def generate(self):
16+
deps = CMakeDeps(self)
17+
deps.generate()
18+
19+
tc = CMakeToolchain(self)
20+
tc.variables['FMT_MODULE'] = True
21+
tc.generate()

examples/conan_cmake/library/src/mathutils.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/conan_cmake/library/include/mathutils/mathutils.h renamed to examples/conan_cmake/library/src/mathutils.ixx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#pragma once
1+
export module mathutils;
22

3-
namespace mathutils
3+
import fmt;
4+
5+
export namespace mathutils
46
{
57
/**
68
* Add two numbers and return the result with formatted output
@@ -27,3 +29,28 @@ namespace mathutils
2729
*/
2830
void print_result(const char *operation, double a, double b, double result);
2931
}
32+
33+
// Implementation
34+
namespace mathutils
35+
{
36+
double add(double a, double b)
37+
{
38+
double result = a + b;
39+
print_result("addition", a, b, result);
40+
return result;
41+
}
42+
43+
double multiply(double a, double b)
44+
{
45+
double result = a * b;
46+
print_result("multiplication", a, b, result);
47+
return result;
48+
}
49+
50+
void print_result(const char *operation, double a, double b, double result)
51+
{
52+
fmt::print(fg(fmt::terminal_color::green),
53+
"MathUtils {}: {} + {} = {}\n",
54+
operation, a, b, result);
55+
}
56+
}

examples/conan_cmake/library/test_package/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
cmake_minimum_required(VERSION 3.24)
1+
cmake_minimum_required(VERSION 4.0)
22

33
project(MathUtilsConsumer LANGUAGES CXX)
44

5-
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD 23)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

88
find_package(mathutils REQUIRED)

examples/conan_cmake/library/test_package/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <mathutils/mathutils.h>
2-
#include <iostream>
1+
import mathutils;
2+
import std;
33

44
int main()
55
{

0 commit comments

Comments
 (0)