I was working on this thread swap c++ map objects in multithreaded environment
However,
#include <memory>
#include <thread>
#include <chrono>
#include <atomic>
#include <iostream>
using namespace std;
shared_ptr<std::string> the_string;
int main()
{
atomic_store(&the_string, std::make_shared<std::string>("first string"));
}
gives a compile time error
error: no matching function for call to 'atomic_store'
atomic_store(&the_string, std::make_shared<std::string>("first string"));
^~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/atomic:1165:1: note: candidate template ignored: could not match 'atomic' against 'shared_ptr'
atomic_store(volatile atomic<_Tp>* __o, _Tp __d) _NOEXCEPT
I did see a few threads on this problem and understand that it could be related to C++ version I have /usr/include/c++/4.2.1/ and /usr/include/c++/4.8.5/ on another box, both give the same issue. Should I upgrade the C++ version?
I resolved this issue by passing -std=c++11 flag.
I resolved this issue by passing the flag -std=c++11
It compiles fine here with GCC 8.3 and Clang 8.0, so yes, you should upgrade your compiler.
Related
I thought g++ 10.3 should have supported the C++20 feature of atomic shared_ptr? But I am still getting the following error
#include <atomic>
#include <thread>
#include <memory>
int main() {
std::atomic<std::shared_ptr<int>> a = std::make_shared<int>(1);
}
In file included from test.cc:1:
/usr/include/c++/10/atomic: In instantiation of ‘struct std::atomic<std::shared_ptr<int> >’:
test.cc:6:37: required from here
/usr/include/c++/10/atomic:195:21: error: static assertion failed: std::atomic requires a trivially copyable type
195 | static_assert(__is_trivially_copyable(_Tp),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
The documentation lists it as supported only since GCC 12.1, i.e. the next release, and compiler explorer shows that your code compiles on GCC trunk: https://godbolt.org/z/4ThzMrjM9
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
#include <iostream>
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main() {
thread t(thread_c);
t.join();
return 0;
}
Such simple example, but it doesn't work on my g++ (MinGW.org GCC-6.3.0-1) 6.3.0, Windows 8.1
Here's the error:
main.cpp: In function 'int main()':
main.cpp:13:5: error: 'thread' was not declared in this scope
thread t(thread_c);
^~~~~~
main.cpp:14:5: error: 't' was not declared in this scope
t.join();
^
*I've been working on a big project, and noticed about some compilation errors. I wrote this code for testing my compiler out
What's wrong with it?
Loking at your code and the compiler results I see the following:
#include <thread> is not including a file that defines thread as your code expects
GCC 6.3.0 is very old (current is 10.2.0), and chances are it was built without POSIX thread support (and probably only support for Windows threads)
I always recommend using MinGW-w64 instead of MinGW as it is more up to date and supports both 32-bit and 64-bit Windows.
When I build your example with the MinGW-w64 from http://winlibs.com/ (which has POSIX thread support) I get no errors and the example works (output is 012345678910).
I am able to compile the following a std=c++11 required code with g++ using the following command:
g++ test.cpp -std=c++11 -Wl,-rpath,/share/apps/gcc/6.3.0/lib64
the code:
#include <chrono>
#include <map>
#include <memory>
#include <thread>
#include <utility>
int main() {
typedef std::unique_ptr<int> intPointer;
intPointer p(new int(10));
std::map<int, std::unique_ptr<int>> m;
m.insert(std::make_pair(5, std::move(p)));
auto start = std::chrono::system_clock::now();
if (std::chrono::system_clock::now() - start < std::chrono::seconds(2))
{
std::thread t;
}
}
The very same command(probably I don't know the correct one) wont work for intel compiler:
icpc test.cpp -std=c++11 -Wl,-rpath,/share/apps/intel/2016.1.056/vtune_amplifier_xe_2016.1.1.434111/target/linux64/lib64
The error is:
In file included from /share/apps/gcc/6.3.0/include/c++/6.3.0/map(60),
from test.cpp(2):
/share/apps/gcc/6.3.0/include/c++/6.3.0/bits/stl_tree.h(1437):
error: identifier "_Compare" is undefined
&& is_nothrow_move_assignable<_Compare>::value)
^
In file included from /share/apps/gcc/6.3.0/include/c++/6.3.0/map(60),
from test.cpp(2):
/share/apps/gcc/6.3.0/include/c++/6.3.0/bits/stl_tree.h(1778):
error: identifier "_Compare" is undefined
_GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
^
What am I doing wrong, and how should I fix this.
From the comments above:
_Compare is one of the template parameters of the _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc> defined in that file, so surely it is defined.
A more likely reason is that the Intel compiler perhaps doesn't know about is_nothrow_move_assignable which is kind of recently added to the type_traits.
I had a user with a similar c++ difficulty using gcc-6.3.0 headers and the Intel 16.1.150 compiler. I opted for the compiler bug theory, tried with the Intel 17 compiler and things worked.
-doug
Well I checked with Intel agents and it turned out that Intel 16 do not support gcc6.3, the support comes in Intel 17.
The following is my code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
int main() {
int cpid=fork();
if(cpid==0) {
printf("%d %d\n",getpid(),getppid());
}
else {
printf("hello");
wait(NULL);
}
}
When I compile this using the following:
g++ -std=c++11 multiple.cpp -o multiple
It gives me the following
multiple.cpp: In function 'int main()':
multiple.cpp:34:23: error: no matching function for call to 'wait::wait(NULL)'
wait(NULL);
^
multiple.cpp:34:23: note: candidates are:
In file included from /usr/include/stdlib.h:42:0,
from multiple.cpp:4:
/usr/include/bits/waitstatus.h:66:7: note: wait::wait()
union wait
/usr/include/bits/waitstatus.h:66:7: note: candidate expects 0 arguments, 1 provided
/usr/include/bits/waitstatus.h:66:7: note: constexpr wait::wait(const wait&)
/usr/include/bits/waitstatus.h:66:7: note: no known conversion for argument 1 from 'int' to 'const wait&'
/usr/include/bits/waitstatus.h:66:7: note: constexpr wait::wait(wait&&)
/usr/include/bits/waitstatus.h:66:7: note: no known conversion for argument 1 from 'int' to 'wait&&'
But if I change the extension to .c and use gcc -o multiple multiple.c it works. Why is this happening?
You have not included the correct header file that the wait() documentation tells you need.
You need
#include <sys/wait.h>
Because wait is declared in sys/wait.h, which you do not include, and C++ requires all the functions to be prototyped before used. C, on the other hand, does not.
You did not include the header for the POSIX wait function, per the documentation:
#include <sys/wait.h>
It is surprising but not inconceivable that the equivalent standard library headers in your C toolchain just so happen to include this themselves, whereas the C++ ones you have used do not.
More likely, you are inadvertently making use of the terrible rule in C that says you can use some functions before they have been declared.
I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem