PrevUpHomeNext

c++ modules shared library, gcc 16.0 (04-23, 2025)


Prepare

Read:

gcc import std; c++ modules (03-04, 2025)

Alias gpp command

alias gpp="/path/to/g++ -fmodules-ts -std=c++26"

c++ source file: my_class.cpp

c++ module declaration starts with a keyword export or module.

E.X:

export module module-name;

Declares a primary module interface unit and export, its name is module-name .

my_class.cpp :

export module my_space;

import std;

export namespace my_space
{
	class my_class
	{
	private:
		int __x;
	public:
		my_class():
			__x{123}
		{
		}
		my_class(int x__):
			__x{x__}
		{
		}
		int get() const
		{
			return __x;
		}
		void greeting() const
		{
			std::cout << "Cheers, c++!" << std::endl;
		}
	};
}

Build it to shared library:

gpp my_class.cpp -c -shared -o libmy_class.so

c++ source file: main.cpp

import my_space;
import std;

int main()
{
	my_space::my_class obj1;
	my_space::my_class obj2{-234};
	std::cout << "values:\n" << obj1.get() << '\n' << obj2.get() << "\n";
	obj2.greeting();
	std::cout << std::endl;
}

Build program:

gpp main.cpp -L./ -lmy_class -o main

Run program:

> ./main
values:
123
-234
Cheers, c++!

Date

April 23, 2025

Back

Up

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++                       #
  #  Deck                           #
  ===================================

Home: cppfx.xyz


PrevUpHomeNext