EDIT: The answer given for: How to support C++20 modules on CLion for Mac (1 answer)
- Which was sited as the reason to close my question as a Dupe, did not work for folks using Xcode or Command Line on MacOS. It worked fine on Visual Studio, but I knew that and stated it below. Still need help with clang/Xcode --DD I went here: How to support C++20 modules on CLion for Mac
And from there to LLVM: https://clang.llvm.org/docs/StandardCPlusPlusModules.html#id14
Tried several things including the simplest:
~/development/Xcode/C++/LearningCPP/LearningCPP/FOO/BAR
❯ cat Hello.cpp
// Hello.cpp
module;
#include <iostream>
export module Hello;
export void hello() {
std::cout << "Hello World!\n";
}
~/development/Xcode/C++/LearningCPP/LearningCPP/FOO/BAR
❯ cat use.cpp
// use.cpp
import Hello;
int main() {
hello();
return 0;
}
Then tried to build like they suggested:
Now the filename of the module interface ends with .cpp instead of .cppm, we can’t compile them by the original command lines. But we are still able to do it by:
clang++ -std=c++20 -x c++-module Hello.cpp --precompile -o Hello.pcm
clang++ -std=c++20 use.cpp -fprebuilt-module-path=. Hello.pcm -o Hello.out
./Hello.out
Hello World!
When I tried, I got nowhere:
~/development/Xcode/C++/LearningCPP/LearningCPP/FOO/BAR
❯ clang++ -std=c++20 -x c++-module Hello.cpp --precompile -o Hello.pcm
error: module interface compilation requires '-std=c++20' or '-fmodules-ts'
1 error generated.
~/development/Xcode/C++/LearningCPP/LearningCPP/FOO/BAR
❯ ls -lrt
total 16
-rw-r--r-- 1 ddelgado staff 119 Mar 12 03:31 Hello.cpp
-rw-r--r-- 1 ddelgado staff 63 Mar 12 03:31 use.cpp
~/development/Xcode/C++/LearningCPP/LearningCPP/FOO/BAR
❯ clang++ -std=c++20 -x c++-module Hello.cpp --precompile -o Hello.pcm
error: module interface compilation requires '-std=c++20' or '-fmodules-ts'
1 error generated.
~/development/Xcode/C++/LearningCPP/LearningCPP/FOO/BAR
❯ clang++ -std=c++20 Hello.cpp --precompile -o Hello.pcm
~/development/Xcode/C++/LearningCPP/LearningCPP/FOO/BAR
❯ clang++ -std=c++20 use.cpp -fprebuilt-module-path=. Hello.pcm -o Hello.out
use.cpp:2:1: error: unknown type name 'import'
import Hello;
^
use.cpp:4:3: error: use of undeclared identifier 'hello'
hello();
^
2 errors generated.
fatal error: file 'Hello.pcm' is not a valid precompiled AST file
1 error generated.
--- Original below:
I'm trying to brush up on C++ and started with the Stroustrup "Tour" book.
Tried this with an example from A Tour of C++, Third Edition (which required some fixing for const-correctness) in Visual Studio and I got it to work. But it failed in Xcode. Unfortunately I am quite unfamiliar with C++ in Xcode so doubt I was close to right.
I tried a couple simpler ones, like: math.cpp (tried math.cppm too)
export module math;
export int add(int fir, int sec) {
return fir + sec;
}
followed by:
// client.cpp
import math;
int main() {
add(2000, 20);
}
then tried to build this with variations read from: https://www.modernescpp.com/index.php/c-20-module-interface-unit-and-module-implementation-unit/
So, for example:
❯ clang++ -std=c++2a -stdlib=libc++ -fmodules -fprebuilt-module-path=. client.cpp math.pcm -o client
client.cpp:2:1: error: unknown type name 'import'
import math;
^
client.cpp:5:4: error: use of undeclared identifier 'add'
add(2000, 20);
^
2 errors generated.
Any idea on what I need to do to get this working even in simplest form?
I am running on a MacBook Pro 16 2019. Xcode is Version 15.2 clang at command-line:
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: x86_64-apple-darwin23.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I suspect it's that "Apple clang" that might be the key. But even so, I really wanted to get this to work in the Xcode GUI but once failed switched to command line. I really do not want to install alternate clang to work in command line. If so, I will simply continue to use the Windows PC for going along with my book.