Trouble with simplest module example in C++ 20

Hi,

Using VStudio 2019 v 16.9.1 with C++20 support it does not compile correctly.

My interface file is called math.ixx and contains this:

1
2
3
4
5
6
7
8
9
10
11
12
13
module;

#include <vector>

export module mathematics;


export namespace mathematics
{
	int add(int first, int second);
	int getProduct(const std::vector<int>& vec);

}


The implementation file is called math.cpp and contains this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

module mathematics;
// module math;


#include <vector>
#include <numeric>


namespace mathematics
{
	int add(int first, int second)
	{
		return first + second;
	}
	int getProduct(const std::vector<int>& vec)
	{
		return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>());
	}
}



The client code is called client.cpp and contains:


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>


import mathematics;

void use()
{
	std::cout << "math::add(2000,20)  " << mathematics::add(2000, 20) << std::endl;
	
}



The errors are:


>math.obj : error LNK2019: unresolved external symbol "void * __cdecl operator new(unsigned __int64,enum mathematics::std::align_val_t)" (??2@YAPEAX_KW4align_val_t@std@mathematics@@@Z) referenced in function "public: static void * __cdecl mathematics::std::_Default_allocate_traits::_Allocate_aligned(unsigned __int64,unsigned __int64)" (?_Allocate_aligned@_Default_allocate_traits@std@mathematics@@SAPEAX_K0@Z)
1>math.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl mathematics::std::_Lockit::_Lockit(int)" (__imp_??0_Lockit@std@mathematics@@QEAA@H@Z::<!mathematics>) referenced in function "public: __cdecl mathematics::std::_Iterator_base12::~_Iterator_base12(void)" (??1_Iterator_base12@std@mathematics@@QEAA@XZ::<!mathematics>)
1>  Hint on symbols that are defined and could potentially match:
1>    "__declspec(dllimport) public: __cdecl std::_Lockit::_Lockit(int)" (__imp_??0_Lockit@std@@QEAA@H@Z)
1>math.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl mathematics::std::_Lockit::~_Lockit(void)" (__imp_??1_Lockit@std@mathematics@@QEAA@XZ::<!mathematics>) referenced in function "public: __cdecl mathematics::std::_Iterator_base12::~_Iterator_base12(void)" (??1_Iterator_base12@std@mathematics@@QEAA@XZ::<!mathematics>)
1>  Hint on symbols that are defined and could potentially match:
1>    "__declspec(dllimport) public: __cdecl std::_Lockit::~_Lockit(void)" (__imp_??1_Lockit@std@@QEAA@XZ)
1>math.obj : error LNK2019: unresolved external symbol __std_exception_copy::<!mathematics> referenced in function "public: __cdecl mathematics::std::exception::exception(class mathematics::std::exception const &)" (??0exception@std@mathematics@@QEAA@AEBV012@@Z::<!mathematics>)
1>math.obj : error LNK2019: unresolved external symbol __std_exception_destroy::<!mathematics> referenced in function "public: virtual __cdecl mathematics::std::exception::~exception(void)" (??1exception@std@mathematics@@UEAA@XZ::<!mathematics>)
1>C:\Users\juan_\source\repos\Exploring C++ 20\x64\Debug\Modules.exe : fatal error LNK1120: 5 unresolved externals


What is going on here?

Regards,
Juan
I would try getting rid of your module stuff, and try compiling from there, then if that works, try the modules.

Also make sure you're linking the files together properly in VS. I have used it before, and it is very, very easy to make mistakes. Which is why I use LLVM's clang compiler instead.

Here's a list of compilers that support C++20:
https://en.cppreference.com/w/cpp/compiler_support/20

VS is listed in there as having only partial support for modules; that may be your issue.
Current Visual Studio 2019 (16.9.2) support for C++20 modules is spotty at best.

The basic modules example [ https://docs.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160 ] compiles with C5050 warning, as well as Intellisense reporting errors that aren't actual errors.

At least they've worked at getting modules somewhat to work in the current version. Previous versions of VS 2019 wouldn't compile at all despite saying module support was available.
Topic archived. No new replies allowed.