atomic_thread_fence is not a member of std - c++

Compiling with:
g++ -std=c++0x -Wall -pthread test2.cc -o hello
I'm getting this error:
test2.cc: atomic_thread_fence is not a member of std
I am using g++ 4.5.2 in Ubuntu. What am I missing?

Do you have the following in test2.cc?
#include <atomic>

Related

Link modern std libraries to gcc or clang

my gcc version is the following:
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
while clang
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
but the following code doesn't compile:
#include <thread>
#include <vector>
int main(){
std::vector<std::jthread> myJThreads;
return 0;
}
I've tried these so far:
g++ -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
clang -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
clang++-12 -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
getting:
error: ‘jthread’ is not a member of ‘std’;
error: no member named 'jthread' in namespace 'std'
how can I fix this?

How do I compile with C++98 on MacOS?

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.

Compile pthread.h stuff on AIX using g++

I try to compile this very simplified program:
#include <pthread.h>
int main(){
pthread_yield();
return 0;
}
using -pthread like the IBM side says:
$ g++ -pthread test.cpp -o test
and get this error:
test.cpp: In function 'int main()':
test.cpp:4:15: error: 'pthread_yield' was not declared in this scope
pthread_yield();
I tried lots of other falgs too, but nothing worked so far. The pthread.h is in /usr/includes but pthread_yield() needs _AIX_PTHREADS_D7 defined.
Do I have to define this myselfe or is this done by adding some flag?
THX!
Other than defining symbol _AIX_PTHREADS_D7 you have to use library libpthreads_compat as well.
g++ -o marscode marscode.cc -D_AIX_PTHREADS_D7 -lpthreads_compat -lpthreads

Code compiles with g++ 5.2.1 but not with g++ 4.9.3

Why this code compiles using g++ 5.2.1, but fails with g++ 4.9.3?
//exception.h
class MyError: public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};
// nothing else here
//main.cpp
#include <iostream>
#include "exception.h"
int main() {}
5.2.1 compilation:
$ g++ --version
g++ 5.2.1
$ g++ -std=c++11 -c main.cpp -o main.o
$ g++ main.o -o a.out
Compilation successfull.
4.9.3 compilation:
$ g++ --version
g++ 4.9.3
$ g++ -std=c++11 -c main.cpp -o main.o
$ g++ main.o -o a.out
In file included from main.cpp:2:0:
exception.h:3:1: error: expected class-name before ‘{’ token
{
^
exception.h:5:14: error: ‘std::runtime_error’ has not been declared
using std::runtime_error::runtime_error;
....
Solution is to add #include <stdexcept> to exception.h
Now it works with both versions.
When I remove #include <iostream> from main.cpp, then compilation fails even with 5.2.1 version and #include <stdexcept> is required too.
Why this code works on 5.2.1 version without including stdexcept header?
It's included in iostream on 5.2.1 version but not in 4.9.3 version? Reading GCC changes didn't help.
The standard library headers are allowed to include other headers, but there are no guarantees.
Back in the ancient days of g++ 4.0 and 4.1, you could pull in most of the standard library with just #include <iostream> and #include <deque>. But that stopped working in version 4.3 (or something like that).
In order for your code to be portable it should explicitly include all the required headers.

c++11 #include <thread> gives compile error

I get a compile error when trying to create an object file from a compiled source file. I am using the header which came with c++11. I am also using a c++ pattern recognition library with several other includes.
All I did was add #include <thread> to my rbm_test.cc source file.
My compile command:
g++ -std=c++11 -O3 -DQUIET -fPIC -pthread -ansi -pedantic -DARCH_INTEL -Wall -W -Wchar-subscripts -Wpointer-arith -Wcast-qual -Wwrite-strings -Wconversion -Wno-old-style-cast -Wctor-dtor-privacy -Wnon-virtual-dtor -I../src -I../.. -DPATREC -D_UNIX_ -o rbm_test.o -c ../src/rbm_test.cc
The compile error I get is:
error: #error This file requires compiler and library support for the
ISO C++ 2011 standard. This support is currently experimental, and
must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
Strangely, when I compile the following code example with
g++ -std=c++11 -pthread -c main.cpp -o main.o
then I have no error.
Here Is main.cpp
#include <iostream>
#include <thread>
void f1()
{
std::cout << "Thread executing\n";
}
int main()
{
std::thread t1(f1);
std::thread t2(f1);
t1.join();
t2.join();
}
Is it possible that some of the compile flags are conflicting when I try to compile rbm_test.cc?
The -ansi flag conflicts with the -std=c++11 flag. -ansi is equivalent to -std=c++98. Removing the -ansi flag solves the problem.