vitaut / modules Goto Github PK
View Code? Open in Web Editor NEWSimple C++20 module support for CMake
Simple C++20 module support for CMake
Hey, this is great. But I need to add -pthread to the compile options for the modules. I don't know how I can add this flag, so I just modify your code to include that flag.
Is there a way I can pass the flag to modules.cmake? I'm new to this CMake world.
Cheers,
Geoff.
as the title says, the build process fails if either flags are included in my CMAKE_CXX_FLAGS
. I've provided a replication of the error:
error:
CMake Error at cmake/modules.cmake:135 (add_library):
No SOURCES given to target: MODULES
Call Stack (most recent call first):
CMakeLists.txt:27 (add_module_library)
file structure:
.
├── cmake
│ └── modules.cmake
├── CMakeLists.txt
└── src
├── hello.cpp
├── main.cpp
└── subdir
└── bye.cpp
CMakeLists.txt:
cmake_minimum_required(VERSION 3.26)
project(modtest)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# this works
#set(CMAKE_CXX_FLAGS "-w -Wall -Wextra -Werror -Wshadow -Wconversion -Wunused -Wpedantic")
# but this doesn't
set(CMAKE_CXX_FLAGS "-w -Wall -Wextra -Werror -Wshadow -Wconversion -Wunused -Wpedantic -Wpedantic-errors -Weverything")
set(PROJECT "${CMAKE_CURRENT_SOURCE_DIR}")
set(TARGET "modtest")
file(GLOB_RECURSE SRC "${PROJECT}/src/*.cpp")
add_executable(${TARGET} ${PROJECT}/src/main.cpp)
include(cmake/modules.cmake)
add_module_library(MODULES ${SRC})
target_link_libraries(${TARGET} PRIVATE MODULES)
main:
import hello;
import bye;
int main() {
hello();
bye();
}
hello:
module;
#include <iostream>
export module hello;
export void hello() {
std::cout << "hello\n";
}
bye:
module;
#include <iostream>
export module bye;
export void bye() {
std::cout << "bye\n";
}
I'm building on Ubuntu 20.04 with the compiler
Ubuntu clang version 17.0.0 (++20230811073125+8f4dd44097c9-1
exp120230811073223.19)
Target: x86_64-pc-linux-gnu
Thread model: posix
Using build tool Unix Makefiles
GNU Make 4.2.1
Built for x86_64-pc-linux-gnu
With CMake version
cmake version 3.16.3
And I get the following compiler warning
[ 20%] Generating hello.pcm
[ 40%] Generating hello.o
Scanning dependencies of target hello
[ 60%] Linking CXX static library libhello.a
[ 60%] Built target hello
Scanning dependencies of target main
[ 80%] Building CXX object CMakeFiles/main.dir/main.cc.o
warning: the form '-fmodule-file=<BMI-path>' is deprecated for standard C++ named modules;consider to use '-fmodule-file=<module-name>=<BMI-path>' instead [-Weager-load-cxx-named-modules]
A simple fix to get rid of the compiler warning was to use the following in modules.cmake::167 - 171
get_filename_component(pcm_name ${src} NAME_WE)
set(pcm ${pcm_name}.pcm)
# Propagate -fmodule-file=*.pcm to targets that link with this library.
target_compile_options(
${name} INTERFACE -fmodule-file=${pcm_name}=${CMAKE_CURRENT_BINARY_DIR}/${pcm})
I'm still quite new to modules so I don't know how correct this actually is.
I found an issue while compiling the example by using CMake ninja generator. Complete log follows:
❯ CXX=g++-11 CC=gcc-11 cmake -S example -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug -GNinja
-- The CXX compiler identification is GNU 11.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++-11 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- C++ target standard = 20
-- modules support = TRUE
--
-- C++ latest standard = 23
-- modules support = TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/gbellizio/CLionProjects/GH/modules/cmake-build-debug
❯ cmake --build cmake-build-debug
[1/4] Building CXX object CMakeFiles/main.dir/main.cc.o
FAILED: CMakeFiles/main.dir/main.cc.o
/usr/bin/g++-11 -I/home/gbellizio/CLionProjects/GH/modules/example/include -g -fmodules-ts -std=c++20 -MD -MT CMakeFiles/main.dir/main.cc.o -MF CMakeFiles/main.dir/main.cc.o.d -o CMakeFiles/main.dir/main.cc.o -c /home/gbellizio/CLionProjects/GH/modules/example/main.cc
In module imported at /home/gbellizio/CLionProjects/GH/modules/example/main.cc:1:1:
hello: error: failed to read compiled module: No such file or directory
hello: note: compiled module file is ‘gcm.cache/hello.gcm’
hello: note: imports must be built before being imported
hello: fatal error: returning to the gate for a mechanical issue
compilation terminated.
[2/4] Building CXX object CMakeFiles/hello.dir/hello.cc.o
FAILED: CMakeFiles/hello.dir/hello.cc.o
/usr/bin/g++-11 -I/home/gbellizio/CLionProjects/GH/modules/example/include -g -fmodules-ts -std=c++20 -MD -MT CMakeFiles/hello.dir/hello.cc.o -MF CMakeFiles/hello.dir/hello.cc.o.d -o CMakeFiles/hello.dir/hello.cc.o -c /home/gbellizio/CLionProjects/GH/modules/example/hello.cc
inputs may not also have inputs
ninja: build stopped: subcommand failed.
With Makefile generator all works fine.
Hello, I'd like to suggest to add a license :)
Edit: oh, I see now that it is contained within the modules.cmake
file. Still, it would be good to mention it somewhere in the README.
I'm having trouble building the example provided. I've copied the modules.cmake
, and have main
and hello
in an src
folder.
Here is my output for clang-15
$ cmake ..
-- The C compiler identification is Clang 15.0.7
-- The CXX compiler identification is Clang 15.0.7
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang-15 - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++-15 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.6s)
-- Generating done (0.0s)
-- Build files have been written to: /home/tuero/Documents/test/module_test/build
$ make
[ 20%] Generating hello.pcm
clang: error: argument to '-I' is missing (expected 1 value)
make[2]: *** [CMakeFiles/hello.dir/build.make:74: hello.pcm] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/hello.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Here is my output for gcc-12
$ cmake ..
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.4s)
CMake Error at modules.cmake:124 (add_library):
No SOURCES given to target: hello
Call Stack (most recent call first):
CMakeLists.txt:9 (add_module_library)
CMake Generate step failed. Build files cannot be regenerated correctly.
My cmake file:
cmake_minimum_required(VERSION 3.26.0)
project("nativetest")
set(CMAKE_CXX_STANDARD 20)
include(modules.cmake)
add_module_library(hello src/hello.cc)
add_executable(main src/main.cpp)
target_link_libraries(main hello)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.