Hi,
I am trying to use the modules feature (since C++20) but the compiler does not recognize it. I tried with VS2019 and VS2022 as compilers but the result is the same.
Below the content of my CMakeLists script:
cmake_minimum_required(VERSION 3.5)
project(Source LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(Source main.cpp
AirlineTicket.cppm
AirlineTicket.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
include(GNUInstallDirs)
install(TARGETS Source
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
cmake_minimum_required(VERSION 3.5)
project(Source LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(Source main.cpp
AirlineTicket.cppm
AirlineTicket.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
include(GNUInstallDirs)
install(TARGETS Source
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
To copy to clipboard, switch view to plain text mode
And a very simple example of module:
export module airline_ticket;
import <string>;
export class AirlineTicket
{
public:
AirlineTicket();
~AirlineTicket();
double calculatePriceInDollars();
std::string getPassengerName();
void setPassengerName(std::string name);
int getNumberOfMiles();
void setNumberOfMiles(int miles);
bool getHasEliteSuperRewardsStatus();
void setHasEliteSuperRewardsStatus(bool status);
private:
std::string m_passengerName;
int m_numberOfMiles;
bool m_hasEliteSuperRewardsStatus;
std::string m_passengerName{ "Unknown Passenger" };
int m_numberOfMiles{ 0 };
bool m_hasEliteSuperRewardsStatus{ false };
};
export module airline_ticket;
import <string>;
export class AirlineTicket
{
public:
AirlineTicket();
~AirlineTicket();
double calculatePriceInDollars();
std::string getPassengerName();
void setPassengerName(std::string name);
int getNumberOfMiles();
void setNumberOfMiles(int miles);
bool getHasEliteSuperRewardsStatus();
void setHasEliteSuperRewardsStatus(bool status);
private:
std::string m_passengerName;
int m_numberOfMiles;
bool m_hasEliteSuperRewardsStatus;
std::string m_passengerName{ "Unknown Passenger" };
int m_numberOfMiles{ 0 };
bool m_hasEliteSuperRewardsStatus{ false };
};
To copy to clipboard, switch view to plain text mode
When I try to build the project, the compiler does not recognize absolutely anything about the module.
I import the module in AirlineTicket.cpp with the following code:
module airline_ticket;
using namespace std;
...more code...
module airline_ticket;
using namespace std;
...more code...
To copy to clipboard, switch view to plain text mode
Is there anything specific I have to do to write C++20 code?
Thanx
Bookmarks