Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Creating and Importing Modules | Modules
C++ Namespaces and Modules

bookCreating and Importing Modules

To work with C++20 modules, you start by creating a module interface file—this file defines what the module provides to other code. In the example above, the file math.ixx is the module interface for a module named math. The line export module math; declares the module and gives it a name. The function add is made available to users of the module with the export keyword before its definition. This means any code importing the math module can call add.

To use the module, you write another file, such as main.cpp. Instead of using a traditional #include directive, you use the import math; statement at the top of your file. This tells the compiler that you want to use the exported functions from the math module. Once imported, you can call add just like any other function. In this example, main.cpp calls add(3, 4) and prints the result.

math.ixx

math.ixx

main.cpp

main.cpp

copy
123
export module math; export int add(int a, int b) { return a + b; }

This process involves three key steps:

  1. Declare and name your module in a .ixx interface file with export module modulename;;
  2. Export functions, classes, or variables you want to share using the export keyword;
  3. Import your module in other files using import modulename;, then use the exported features as needed.

By following these steps, you can organize your code into well-defined modules, making it easier to maintain and less prone to name conflicts.

question mark

Which statement best describes how to share a function from a C++20 module with other files?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain how to compile and link C++20 modules?

What are the benefits of using modules over traditional headers?

Are there any limitations or compatibility issues with C++20 modules?

Awesome!

Completion rate improved to 12.5

bookCreating and Importing Modules

Pyyhkäise näyttääksesi valikon

To work with C++20 modules, you start by creating a module interface file—this file defines what the module provides to other code. In the example above, the file math.ixx is the module interface for a module named math. The line export module math; declares the module and gives it a name. The function add is made available to users of the module with the export keyword before its definition. This means any code importing the math module can call add.

To use the module, you write another file, such as main.cpp. Instead of using a traditional #include directive, you use the import math; statement at the top of your file. This tells the compiler that you want to use the exported functions from the math module. Once imported, you can call add just like any other function. In this example, main.cpp calls add(3, 4) and prints the result.

math.ixx

math.ixx

main.cpp

main.cpp

copy
123
export module math; export int add(int a, int b) { return a + b; }

This process involves three key steps:

  1. Declare and name your module in a .ixx interface file with export module modulename;;
  2. Export functions, classes, or variables you want to share using the export keyword;
  3. Import your module in other files using import modulename;, then use the exported features as needed.

By following these steps, you can organize your code into well-defined modules, making it easier to maintain and less prone to name conflicts.

question mark

Which statement best describes how to share a function from a C++20 module with other files?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
some-alt