switch to cmake and add c++ modules

i have to switch to cmake to get proper c++ modules support.
This commit is contained in:
Fries 2023-06-21 01:24:50 -07:00
parent 16504e70a4
commit b1ac6cfb47
10 changed files with 47 additions and 18 deletions

23
CMakeLists.txt Normal file
View file

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.26)
project(LearningProject CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
string(CONCAT CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE
"<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> <SOURCE>"
" -MT <DYNDEP_FILE> -MD -MF <DEP_FILE>"
" ${flags_to_scan_deps} -fdep-file=<DYNDEP_FILE> -fdep-output=<OBJECT>"
)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT "gcc")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG
"${compiler_flags_for_module_map} -fmodule-mapper=<MODULE_MAP_FILE>")
endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory(src)

View file

@ -1,2 +0,0 @@
project('LearningProject', 'cpp')
subdir('src')

7
src/CMakeLists.txt Normal file
View file

@ -0,0 +1,7 @@
add_subdirectory(math)
add_executable(hello hello.cc)
add_executable(sqrt sqrt.cc)
target_link_libraries(sqrt PRIVATE Math)
add_executable(inheritance inheritance.cc)
add_executable(enumerations enumerations.cc)

View file

@ -1,6 +1,6 @@
#include <iostream> #include <iostream>
std::string generate_crewmate_string(std::string type); constexpr std::string generate_crewmate_string(std::string type);
enum class CrewmateType { enum class CrewmateType {
Sus, Sus,
@ -34,7 +34,7 @@ public:
} }
}; };
std::string generate_crewmate_string(std::string type) constexpr std::string generate_crewmate_string(std::string type)
{ {
return "Your crewmate type is " + type; return "Your crewmate type is " + type;
} }

View file

@ -1,5 +0,0 @@
#include "math.hh"
double Math::square(double x)
{
return x * x;
}

View file

@ -1,4 +0,0 @@
class Math {
public:
double static square(double x);
};

2
src/math/CMakeLists.txt Normal file
View file

@ -0,0 +1,2 @@
add_library(Math)
target_sources(Math PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES math.cc)

12
src/math/math.cc Normal file
View file

@ -0,0 +1,12 @@
module;
export module Math;
export class Math {
public:
double static square(double x);
};
double Math::square(double x)
{
return x * x;
}

View file

@ -1,4 +0,0 @@
executable('hello', 'hello.cc')
executable('sqrt', ['math.cc', 'sqrt.cc'])
executable('inheritance', 'inheritance.cc')
executable('enumerations', 'enumerations.cc')

View file

@ -1,4 +1,4 @@
#include "math.hh" import Math;
#include <iostream> #include <iostream>
void print_squared(double x); void print_squared(double x);