C++ 20 modules in Xcode 14.0.1 - c++

I would like to experiment with newer C++ features from (at least) C++20 and I'm new to C++ development on macOS in particular.
How do I enable importing of library headers in the 'module way'?
import <iostream>;
using namespace std;
int main(int argc, const char * argv[]) {
cout << "Hello, World!\n";
return 0;
}
The build errors are
Use of undeclared identifier 'iostream'
Use of undeclared identifier 'std'
I was looking at this answer and tried installing llvm and I suppose that I could run the provided commands from the terminal, but I have not tried. Is there a way to make Xcode issue the commands provided in that answer (see below)? Where would I enter them in XCode? And what do they do? I'd like to avoid thinking about terminal commands and just have something that works.
/opt/homebrew/opt/llvm/bin/clang++ -std=c++20 -c -Xclang -emit-module-interface mathlib.cpp -o mathlib.pcm
/opt/homebrew/opt/llvm/bin/clang++ -std=c++20 -fmodules -c -fprebuilt-module-path=. main.cpp -o main.o
/opt/homebrew/opt/llvm/bin/clang++ -std=c++2a -fmodules -o main main.o *.pcm
I saw that when making a new C++ project, Xcode defaults to the Apple Clang compiler and GNU++20 [-std=gnu++20] C++ Language dialect. There is also another option C++20 [-std=c++20] (what's the difference?) which I also tried, resulting in the same errors.
FYI, when I do clang -v I get
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
And a general question about other build systems such as CMake, which I discovered during my research for C++ compilation on mac. After making a CMake project, is it possible to use the regular IDE tools such as code suggestions and debugging?

Related

Need help compiling with g++ if the code have threading functionalities using std::thread [duplicate]

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

'thread' was not declared in this scope [duplicate]

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

Thread was not declared in this scope error [duplicate]

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

std::thread is not a member of namespace std using Eclipse Kepler MinGW

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

builds in xcode 4.6 but fails using command line

When I run following code snippet from Xcode4.6 it compiles and runs fine. But when I try to compile it using command line tool (clang++) it fails to do so.
#include <iostream>
#include <memory>
int main(int argc, const char * argv[])
{
std::unique_ptr<int> foo(new int(0));
// insert code here...
std::cout << "Hello, this is cool giri World!\n";
return 0;
}
Here is compile log:
$ clang --version
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
$ clang++ main.cpp -stdlib=libc++ -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ -I /usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include/
main.cpp:7:10: error: no member named 'unique_ptr' in namespace 'std'
std::unique_ptr foo(new int(0));
~~~~~^
main.cpp:7:24: error: expected '(' for function-style cast or type construction
std::unique_ptr foo(new int(0));
~~~^
main.cpp:7:26: error: use of undeclared identifier 'foo'
std::unique_ptr foo(new int(0));
^
3 errors generated.
Try using clang's own standard library:
clang -std=c++11 -stdlib=libc++ main.cpp
The default is GNU's standard library (libstdc++), but the version Apple included is quite old and doesn't have C++11 support.
You can look for yourself to see what command line Xcode used.
Build your project in Xcode.
Switch to log view. The icon for it looks like a speech bubble with a couple of lines in it.
Click on the latest build.
A list of build steps will show up in the main editing area. Right-click on "Compile main.cpp" and select "Copy Transcript for Shown Results".
Paste this into your favorite text editor to see the exact command line that Xcode used to build your project.
Make sure you are invoking clang++, not clang, for both the compiler and linker.
clang++ (as compiler) needs the -std=c++11 and -stdlib=libc++ compiler flags, and clang++ (as linker) needs the -stdlib=libc++ linker flag.
thanks Everyone for suggesting me solutions which kept me going.
Finally this is what worked for me.
I uninstalled command line tools using shell script mentioned in http://www.cocoanetics.com/2012/07/you-dont-need-the-xcode-command-line-tools/
and then used
$xcode-select -switch /Applications/Xcode.app/Contents/Developer/
to set xcode version . and finally used
$xcrun clang++ main1.cpp -stdlib=libc++
to compile my code.
This worked fine. thanks!!