Jacopo N
Jacopo N

Reputation: 19

Error: module declaration must occur at the start of the translation unit export module occupati

I am a beginner to C ++ and I am practicing with structures. Every time I try to compile the program the compiler gives me this error:

FAILED: CMakeFiles/jacoProject.dir/main.cpp.o 
/Library/Developer/CommandLineTools/usr/bin/c++   -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -std=gnu++2a -MD -MT CMakeFiles/jacoProject.dir/main.cpp.o -MF CMakeFiles/jacoProject.dir/main.cpp.o.d -o CMakeFiles/jacoProject.dir/main.cpp.o -c /Users/jacopodosio/CLionProjects/jacoProject/main.cpp
In file included from /Users/jacopodosio/CLionProjects/jacoProject/main.cpp:3:
/Users/jacopodosio/CLionProjects/jacoProject/occupati.ixx:1:8: error: module declaration must occur at the start of the translation unit
export module occupati;

The code in the files is shown below. main.cpp

#include <iostream>
#include "occupati.ixx"
#include "test.h"
using std::cout;
using std::endl;
using std::cin;

int main()
{
    Employes employeesCompany;
    employeesCompany.initialEmployee = 'f';
}

occupati.ixx

export module employes;

export struct Employes
{
    char initialEmployee {};
    char ultimaLetteraDipendente {};
    int occupatiTotali {};
    int stipendio {};
};

Upvotes: 0

Views: 1441

Answers (2)

Adrian Mole
Adrian Mole

Reputation: 51874

Modules (in *.ixx files) aren't something you #include in other source files. Rather, you import the components that are exported by those module files.

So, try this for your "main.cpp":

#include <iostream>
import employes; // Imports from "occupati.ixx"
#include "test.h"
using std::cout;
using std::endl;
using std::cin;

int main()
{
    Employes employeesCompany;
    employeesCompany.initialEmployee = 'f';
}

Note that the "occupati.ixx" file should be part of the project/build.


Note: You have said, in comments, that you use CLion as your IDE/build-system. I don't use this, but this blog may offer some advice and/or remedies for how to use C++20 Modules in that environment.

Upvotes: 1

Nicol Bolas
Nicol Bolas

Reputation: 474116

You do not #include modules. #include means to copy-and-paste a file into this one. That's not how C++ modules work.

You import modules. Assuming your build is set up to handle C++ modules, you should import employes; at the top.

Upvotes: 0

Related Questions