I have been trying to compile and run the following very simple bit of code
#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();
system("pause");
return 0;
}
I am compiling on Windows 10 with MinGW 6.3.0 (target x86_64-w64-mingw32) and the thread model (obtained using g++ -v) is POSIX.
I am not getting any compilation error with g++ -Wall -g test.cpp -o test.exe, however I am getting a runtime error when trying to run the exe (entry point of _ZNSt6thread15_M_start_threadESt10unique_ptrINS_3_StateESt14default_deleteIS1_EEPFvve cannot be found).
I also tried compiling with the -pthreador -lpthread flags, but I am getting the same runtime error.
Obviously this seems to be related to the use of std::thread, but I didn't get how to fix this. Am I missing a compilation flag or additional library enabling POSIX thread support on Windows?
Edit: I managed to get it working by changing the compilation command to
g++ -Wall -g test.cpp -o test.exe -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,Bdynamic
I tried your code after making the necessary fixes:
#include <thread>
#include <iostream>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
std::cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
and it worked fine.
The build command was the same as yours:
g++ -Wall -g test.cpp -o test.exe
The output was:
012345678910Press any key to continue . . .
But I did use MinGW-w64 GCC 12.1.0 (fro https://winlibs.com/) instead of the very old version 6.3.0.
Maybe the MinGW-w64 (or was it still the old MinGW) library you were using was simply too old...
My system: macOS Catalina(10.15.4)
Brew config
Clang: 11.0 build 1103
Git: 2.21.0 => /usr/local/bin/git
Curl: 7.64.1 => /usr/bin/curl
Java: 1.8.0_101
macOS: 10.15.4-x86_64
CLT: 11.4.1.0.1.1586360307
Xcode: N/A
Issue: can't include some C++17 headers: execution, filesystem
while can include the other like: any, variant.
Most of C++17 features like structured binding, template parameters deduction etc works.
Example. Trying to build a file with the following code snippet:
#include <numeric>
#include <vector>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <utility> //std::pair
#include <tuple> // std::tie
#include <algorithm> // std::clamp
#include <optional> // std::optional
#include <map>
// ... other stuff
#include <execution>
namespace parallel_algoritmhs {
void show() {
std::vector<int> v = {1,2,3,4,5,6};
std::for_each(std::execution::par, v.begin(), v.end(), [](auto& e) {e+=100;});
int main() {
//... use
}
I am building with:
g++ -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
Update:
I have tried suggested:
clang -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
clang++ -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
And getting the same result.
If I comment out code snippet related to different execution strategies, while leaving other code snippets like:
#include <any>
namespace any {
void show() {
std::any v = 42;
v = 4.2;
v = std::string{"hello"};
std::cout << std::any_cast<std::string>(v) << '\n';
}
}
namespace map_cpp17 {
void show() {
std::map<int, std::string> myMap{ { 1, "Gennady" }, { 2, "Petr" }, { 3, "Makoto" } };
auto node = myMap.extract(2);
node.key() = 42;
myMap.insert(std::move(node));
std::map<int, std::string> m1{ { 1, "aa" }, { 2, "bb" }, { 3, "cc" } };
std::map<int, std::string> m2{ { 4, "dd" }, { 5, "ee" }, { 6, "ff" } };
m1.merge(m2);
std::map<int, std::string> m; m.emplace(1, "aaa"); m.emplace(2, "bbb"); m.emplace(3, "ccc");
auto [it1, inserted1] = m.insert_or_assign(3, "ddd"); std::cout << inserted1; // 0
auto [it2, inserted2] = m.insert_or_assign(4, "eee"); std::cout << inserted2; // 1
}
}
Everything compiles
With
#include <execution>
getting:
fatal error: 'execution' file not found
#include <execution>
I wonder what might be the problem behind that and how to fix this.
Thank you!
Update 2:
brew info llvm:
llvm: stable 10.0.0 (bottled), HEAD [keg-only]
Next-gen compiler infrastructure
https://llvm.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/llvm.rb
==> Dependencies
Build: cmake ✔, python#3.8 ✘
Required: libffi ✘
==> Requirements
Build: xcode ✘
==> Options
--HEAD
Install HEAD version
==> Caveats
To use the bundled libc++ please add the following LDFLAGS:
LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
llvm is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
Update 3:
clang --version
Apple clang version 11.0.3 (clang-1103.0.32.59)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
You're not actually using the Clang you installed. The official LLVM Clang doesn't have a version 11.0! LLVM is only on version 10.0 (check brew info llvm). You're using Apple Clang, which comes with its own installation of libc++. Presumably to catch this exact type of problem, Apple Clang responds to clang --version/clang++ --version with the "future" version 11.0. However, Apple's distribution is actually a bit behind the state of the art, and it simply isn't updated to these features.
When you install LLVM with Homebrew, it doesn't automatically set it up the new clang/clang++ to be easily callable, because things might expect clang to specifically mean "Apple Clang" and break if that changes. You should add /usr/local/opt/llvm/bin (which Homebrew symlinks into the LLVM installation) to your PATH (which is where your shell and other programs search for programs). In an already running shell session
export PATH="/usr/local/opt/llvm/bin:$PATH"
will switch to using the newly installed LLVM Clang until you exit the session. Putting this line in a shell startup file, like ~/.bash_profile, will set up this adjustment to be applied every time you open a new shell.
Alternatively, like #Eljay does, you could just manually type out the full path to the new clang/clang++ (/usr/local/opt/llvm/bin/clang/-++), thereby bypassing the search into PATH, but that's a pain. In any case, you'll know you got it right if clang --version/clang++ --version gives you 10.0.0.
I am seeing a weird crash trying to use a wrapper template class around aligned struct/class. The following program is crashing when compiled with clang, while working properly with both gcc and visual studio.
struct __attribute__( ( aligned( 16 ) ) ) AlignedStruct
{
AlignedStruct( ) { }
float x,y,z,w;
};
template <typename P1> class Wrapper
{
public:
P1 Inner;
};
int main( )
{
Wrapper<AlignedStruct> * t1 = new Wrapper<AlignedStruct>( );
return 1;
}
The crash seems to be while calling AlignedStruct constructor (if I remove it, clang compiled code runs as well).
Can anyone explain what is going on here?
Thanks!
Edit:
The crash occurs with clang 3.4.2 under cygwin32 and cygwin64
and clang 3.4.1 under Ubuntu 14.04 (x32) and Ubuntu 14.10 (x32)
It does not, however, occur when testing with clang 3.4.1 under FreeBSD 10.1 (x32) and OS X Yosemite.
I've tried playing around with compilation flags but I can't seem to be able to make any difference with those, so I am just running clang++ file.cpp
I've read multiple posts here relating to dynamic libraries on os x and debugging with gdb. But I still can't figure out why I can't debug a simple test case.
The main issue is that when I start up GDB it never loads any shared libraries.
Update: I've tried this with GDB from macports, from homebrew, and built from source and the behavior is the same.
I have a class that I compile into a library.
Test.hpp
class Test {
public:
void set(int i);
void out() const;
private:
int i;
};
Test.cpp
#include "Test.hpp"
#include <iostream>
void Test::set(int ii) { i = ii; }
void Test::out() const {
auto j = i * 100;
std::cout << i << ", " << j << "\n";
++j;
std::cout << i << ", " << j << "\n";
}
I compile it and create a library with g++. Note: the behavior is the same with macports gcc and the gcc from xcode.
/opt/local/bin/g++-mp-4.8 -O0 -g -ggdb -Wall -c -std=c++11 -o Test.o Test.cpp
/opt/local/bin/g++-mp-4.8 -dynamiclib -o libTest.dylib Test.o
Then I test it with this simple main
#include "Test.hpp"
int main() {
Test t;
auto x = 4;
t.set(x);
t.out();
return 0;
}
This is compiled and linked with
/opt/local/bin/g++-mp-4.8 -O0 -g -ggdb -Wall -c -std=c++11 -o main.o main.cpp
/opt/local/bin/g++-mp-4.8 -L . -o testing main.o -lTest
Everything compiles and runs as expected. But when I try to debug this with gdb (installed from macports, or installed from source, the behavior is the same), I have problems.
As I step through main, if I call info sharedlibrary it always says "No shared libraries loaded at this time.", so it apparently never loads libTest.dylib. Therefore, I can't step into any of the Test member functions or create breakpoints anywhere in libTest.dylib.
Indeed ggdb installed from macports for some reason does not respect the DYLD_LIBRARY_PATH. However, if you "patch" your executable with the correct paths for the .dylibs you should be able to debug with ggdb. Take a look at this question and especially the answer by Akos Cz.
Consider this code (badcast.cpp):
#include <exception>
#include <typeinfo>
#include <stdio.h>
class foo {
public:
virtual ~foo() {}
};
class bar: public foo {
public:
int val;
bar(): val(123) {}
};
static void
cast_test(const foo &f) {
try {
const bar &b = dynamic_cast<const bar &>(f);
printf("%d\n", b.val);
} catch (const std::bad_cast &) {
printf("bad cast\n");
}
}
int main() {
foo f;
cast_test(f);
return 0;
}
FreeBSD 9.1:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap (core dumped)
$ g++ badcast.cpp -o badcast -frtti -fexceptions -Wall && ./badcast
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap (core dumped)
$ gcc -v
Using built-in specs.
Target: amd64-undermydesk-freebsd
Configured with: FreeBSD/amd64 system compiler
Thread model: posix
gcc version 4.2.1 20070831 patched [FreeBSD]
$ uname -a
FreeBSD freebsd9 9.1-RELEASE FreeBSD 9.1-RELEASE #0 r243825: Tue Dec 4 09:23:10 UTC 2012 root#farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64
Debian Linux 6:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
bad cast
OS X 10.8:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
bad cast
Why does catching bad_cast not work on FreeBSD?
As a wild guess, there’s a chance in FreeBSD you might be using LLVM’s new libc++, instead of the old GNU libstdc++. FreeBSD has been working towards switching over to the LLVM toolchain, away from GNU GPL licensed software.
Apple’s moving that way too, and in the past I’ve ran into issues developing for Mac using libc++ that libstdc++ didn’t have (especially with Boost).
You can use ldd to confirm what libraries you’re linking against:
ldd ./badcast
If it is linking against libc++, you might want to file the bug and test case with the LLVM project.