C++ math.h on OS X 10.8.5 - c++

I have a C++ program that will not compile under OS X 10.8.5 with the g++ compiler. The problem seems to be with the math.h header file.
This is the version of g++ is
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/Users/densmore3/local/usr/local/bin/../libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../gcc-4.9-20141029/configure --enable- languages=c++,fortran
Thread model: posix
gcc version 4.9.2 20141029 (prerelease) (GCC)
There are 40-50 errors of the type below. The code compiled fine on 10.6. What is going on?
/Users/xxxx/local/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed/math.h:203:1: error: ‘__header_always_inline’ does not name a type
__header_always_inline int __inline_isfinitef(float);
Users/densmore3/local/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed/math.h:580:27: error: expected initializer before ‘__AVAILABILITY_INTERNAL__MAC_10_0_DEP__MAC_10_9’
extern float __inff(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_9, __IPHONE_NA, __IPHONE_NA);
Here is a piece of test code that gives the same error as my real code. The error goes away if I remove the math.h include statement.
#include <iostream>
#include <math.h>
#include <stdio.h>
//#include <complex>
//#include <vector>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
The compile command I am using is:
g++ test.cpp

gcc-4.8 is the correct version to use on OS X 10.8.

Related

std::ifstream doesn't work with -O3 optimization

I am trying to run this code with optimization key -O3 (-O1 and O2 also do not work)
main.cpp:
#include "my_file.hpp"
int main(int argc, char* argv[]){
test_another();
return 0;
}
my_file.hpp:
#include <string>
#include <fstream>
void test_another(){
std::ifstream bar;
bar.open("C:\\Users\\ische\\source\\repos\\OpenMpCpp\\test.in");
}
What I get: "Exception has occurred. Segmentation fault" in istream file when basic_istream() is called.
If function test_another() is moved to the main.cpp file - all works fine even with -O3 key. If program is compiled without any optimization key (O1, O2, O3) then it is a successful compilation.
I use Windows10, compiler is g++ from MinGW package: g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
Anyone has any ideas why is this happening?

Threads - Mac vs Linux

Great!
I just finished my implementation on Mac with g++ / clang
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
and tested my code on linux
g++ (Debian 4.7.2-5) 4.7.2
running a relatively simple threading operation. What worked on mac, fails on linux now with :
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
#include <cstddef>
#include <memory>
#include <stdlib.h>
#include <iostream>
#include <thread>
#include <vector>
#include <stdexcept>
std::vector<std::thread> threads;
std::vector<std::tuple<std::size_t, std::size_t>> parts = splitRows(maxNumberThreads, elements);
for (std::size_t threadIndex = 0; threadIndex < maxNumberThreads; threadIndex++)
{
threads.push_back(std::thread(compute<T>,parts[threadIndex], numbers, std::ref(*this),std::ref(other),std::ref(target)));
}
with the thread function defined as. Adding prints into compute it does not jump into the function at all... Any idea why this happens?
template<typename T>
void compute(const std::tuple<std::size_t,std::size_t> part,
const std::size_t numbers,
const MyClass<T>& m1,
const MyClass<T>& m2,
MyClass<T>& target){
I am compiling with
g++ -Wall main.cpp -o matrix.exe -std=c++11
but get the above runtime error. Any ideas how to fix this? I use std libraries only, nothing fancy...
You are not linking pthread properly, try following command,
g++ -Wall main.cpp -o matrix.exe -pthread -std=c++11
Hope this helps.

typeinfo name() and endl don't work together in Windows and mingw

When I run this simple code in Ubuntu (Ubuntu 13.10, 64 bits, g++ 4.8.1) :
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
int main(void)
{
const type_info &ti_trait = typeid(char_traits<char>::char_type);
cout << "Traits character type name : " <<
ti_trait.name() << endl;
return 0;
}
everything is OK, but in Windows (Windows 8 64 bits, mingw, g++ 4.8.1), I got "The program has stopped working" (the compilation works fine and -Wall produces no warning).
The same code compiled and executed in Visual Studio works correctly.
Any idea?
The solution is to compile with -static-libgcc -static-libstdc++ (see here for an explaination). Thanks to #sftrabbit.

Why are std::stoi and std::array not compiling with g++ c++11?

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

Error when compiling gcc 4.6.1 C++0x threading code on MacOSX Lion

When compiling the following code:
#include <iostream>
#include <thread>
using namespace std;
void hello()
{
cout << "Hello World!" << endl;
}
int main()
{
cout << "starting" << endl;
thread t(hello);
t.join();
cout << "ending" << endl;
return 0;
}
using:
$ g++-4.6.1 -std=c++0x -pthread threading.cpp
I get the following error:
threading.cc: In function ‘int main()’:
threading.cc:13:2: error: ‘thread’ was not declared in this scope
threading.cc:13:9: error: expected ‘;’ before ‘t’
threading.cc:14:2: error: ‘t’ was not declared in this scope
This is on MacOSX Lion with a custom built gcc 4.6.1. All the other c++0x features that are valid for gcc 4.6 works like a charm. Is this a MacOSX specific error?
std::thread (and the rest of the C++11 thread library) is only available for some of the platforms supported by gcc 4.6.1. Unfortunately for you, MacOSX is not one of those platforms.
My commercial Just::Thread library provides the C++11 thread facilities for 32-bit MacOSX with gcc 4.5, but gcc 4.6 is not supported yet.
See http://gcc.gnu.org/PR50196 - Mac OS X doesn't support some parts of pthreads that we rely on. Building the latest version won't help, but it might be fixed for GCC 4.7