I need to use C++98 for university programs, however even when passing the -std=c++98 flag to clang++ or to g++ it still seems to compile with c++11 and does not give errors if I use c++11 features. Here is a simple example:
#include <string>
#include <sstream>
using namespace std;
int main()
{
int i;
string number = "12";
i = stoi(number);
}
My makefile:
all:
clang++ -std=c++98 -c *.cpp
clang++ -o main *.o
clean:
rm -f *.o main
run: clean all
./main
Then I run the command make from Terminal (I tried using clang++ instead of g++ but it yields the same result) and receive the following output:
➜ cppversion make
g++ -std=c++98 -c *.cpp
g++ -o main *.o
➜ cppversion make
clang++ -std=c++98 -c *.cpp
clang++ -o main *.o
➜ cppversion
I believe this code should not have compiled if the -std=c++98 flag was working. How do I force code to compile with c++98?
Here is the version of clang:
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin\
Here is the version of g++:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I have also tried adding the flag -pedantic but it does not fix the problem.
Using the flag -stdlib=libc++ yields the following:
➜ cppversion make
clang++ -stdlib=libstdc++ -std=c++98 -c *.cpp
clang: warning: include path for libstdc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
main.cpp:1:10: fatal error: 'string' file not found
#include <string>
^~~~~~~~
1 error generated.
make: *** [all] Error 1
If I change it to just -stdlib=libc++ then it still compiles:
➜ cppversion make
clang++ -stdlib=libc++ -std=c++98 -c *.cpp
clang++ -o main *.o
➜ cppversion
I found an easy solution: Use homebrew to install gcc and use g++-11 to compile.
Try using -std=c++98 -pedantic.
This should strictly enforce the specific standard.
Disclaimer: This is partly guesswork since I don't have a Mac
From my understanding, clang++ is the default compiler on Mac and I would therefore not be surprised if even g++ uses LLVM:s libc++ and headers by default. std::stoi is unconditionaly declared in the libc++ headers.
If you instead useg++:s libstdc++ toolchain, you will probably get the error you want:
clang++ -stdlib=libstdc++ -std=c++98 -o main main.cpp
I found an easy solution: Use homebrew to install gcc and use g++-11 to compile.
Related
I can build c++ projects without c++ modules with build2, but when i try to configure and use build2 with c++ modules, I have "compiler does not support modules" error.
I'm sure my compiler is capable of building modules, because I can manually build using these commands:
clang++ --std=c++17 -fmodules-ts --precompile foo.cppm -o foo.pcm
clang++ --std=c++17 -fmodules-ts --precompile foo2.cppm -o foo2.pcm
clang++ --std=c++17 -fmodules-ts -c foo.pcm -o foo.o
clang++ --std=c++17 -fmodules-ts -c foo2.pcm -o foo2.o
clang++ --std=c++17 -fmodules-ts -fprebuilt-module-path=. foo.o foo2.o bar.cpp
Version of my clang is 7.0.0:
$ clang++ --version
clang version 7.0.0- (trunk)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
To enable modules support in build2 I added following lines to root buildfile:
cxx.std = experimental
using cxx
assert $cxx.features.modules 'compiler does not support modules'
mxx{*}: extension = mxx
cxx{*}: extension = cxx
What can be wrong? It's my first time with build2, so I can be missing something very simple.
Managed it to work.
As I understand the problem was that I changed buildfile, but should use build/root.build instead.
I am trying to get the #include <experimental/any> to compile in my C++ program on clang OSX
// test.cpp
#include <experimental/any>
int main() {
return 0;
}
Tried following commands/options as learnt from here
clang++ -std=c++14 test.cpp -o test -std=c++1z -stdlib=libc++
clang++ -std=c++1x test.cpp -o test -std=c++1z -stdlib=libc++
clang++ -std=c++1y test.cpp -o test -std=c++1z -stdlib=libc++
clang++ -std=c++1z test.cpp -o test -std=c++1z -stdlib=libc++
But it doesn't compile & complains of the following error:
fatal error: 'experimental/any' file not found
clang++ --version yields following:
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
How can I get #include <experimental/any> to compile?
Should I upgrade clang on my machine?
Is C++17 supported on clang currently as of today? If yes how can get the support for it?
For OSX 10.12.5 (using Xcode Developer tools), we get
> clang++ -v
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
and there is no any in /Library/Developer/CommandLineTools/usr/include/c++/v1/experimental, but only
chrono
optional
string_view
tuple
utility
algorithm
dynarray
ratio
system_error
type_traits
So, it appears that Apple's libc++ does not provide any (there is no any in /Library/Developer/CommandLineTools/usr/include/c++/v1/ either). You must either use GCC or your own clang or boost/any.hpp, all of which you can install via homebrew.
You misspelt it. It's "experimental", not "experimentals".
However, since Clang 4.0, you should just be using <any>.
I'm still having an issue with the bug from g++ 4.6.1 however the solution provided doesn't seem relevant to g++ 5.3.0. Is there any other workaround besides installing mingw-w64?
My build command is:
g++ main1.cpp main2.cpp -g -std=gnu++11 -o program.exe
I am trying to make using g++. At first, I upgraded my gcc version by compiling the package locally, and add some environment path to my ~/.bashrc
alias gcc='/home/rescape/lib/bin/gcc'
alias g++='/home/rescape/lib/bin/g++'
export CC=/home/rescape/lib/bin/gcc
export CPP=/home/rescape/lib/bin/cpp
export CXX=/home/rescape/lib/bin/c++
And I try g++ -v in terminal:
[rescape#iZ231twjza6Z mxnet]$ g++ -v
Using built-in specs.
COLLECT_GCC=/home/rescape/lib/bin/g++
COLLECT_LTO_WRAPPER=/home/rescape/lib/libexec/gcc/x86_64-unknown-linux-gnu/4.8.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib --prefix=/home/rescape/lib/
Thread model: posix
gcc version 4.8.0 (GCC)
Still, When I do make, such error message occurs:
[rescape#iZ231twjza6Z mxnet]$ make
g++ -std=c++0x -DMSHADOW_FORCE_STREAM -Wall -O3 -I./mshadow/ -I./dmlc-core/include -fPIC -Iinclude -msse3 -funroll-loops -Wno-unused-parameter -Wno-unknown-pragmas -DMSHADOW_USE_CUDA=0 -DMSHADOW_USE_CBLAS=1 -DMSHADOW_USE_MKL=0 -DMSHADOW_RABIT_PS=0 -DMSHADOW_DIST_PS=0 -DMXNET_USE_OPENCV=1 `pkg-config --cflags opencv` -fopenmp -MM -MT build/resource.o src/resource.cc >build/resource.d
cc1plus: error: unrecognized command line option "-std=c++0x"
make: *** [build/resource.o] Error 1
Any suggestions of how to fix this? Thanks!
According to this:
[rescape#iZ231twjza6Z mxnet]$ make
g++ ...
You not use CXX variable in your Makefile, so just replace g++ with CXX in your Makefile. aliases works only when you enter commands in your shell, if you type g++ something.cpp bash execute /home/bin/g++ something.cpp, that's all, bash aliasing not help if external process (in our case make) execute g++
Hi I am trying to use std::thread with G++. Here is my test code
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
It compiles, but when I try to run it the result is:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted
My compiler version:
$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
What is wrong with my test code?
UPDATE: I use the following command line to compile and run my code.
$ g++ -std=c++0x test.cpp
$ ./a.out
and I tried
$ g++ -std=c++0x -lpthread test.cpp
$ ./a.out
still the same.
I think on Linux pthread is used to implement std::thread so you need to specify the -pthread compiler option.
As this is a linking option, this compiler option need to be AFTER the source files:
$ g++ -std=c++0x test.cpp -pthread
In addition to using -std=c++0x and -pthread you must not use -static.
-std=c++11 -static -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive works together with -static!!!
See here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590#c4
Here's a simple CMake file for compiling a C++11 program that uses threads:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
list(APPEND CMAKE_CXX_FLAGS "-pthread -std=c++11 ${CMAKE_CXX_FLAGS}")
add_executable(main main.cpp)
One way of building it is:
mkdir -p build
cd build
cmake .. && make
Try compiling this way in single command:
g++ your_prog.cpp -o your_output_binary -lpthread -std=gnu++11
You can also try C++11 instead of gnu++11. Hope this works.