Compiler error with filesystem library: clang and g++ - c++

I am writing a personal project in c++ which needs to access to files in some directories, hence I decided to use the filesystem library. I encountered some problems when I try to compile my project on MacOS and on Linux.
The code snippet is the following
#include <iostream>
#include <fstream>
int main(){
std::string path = "Inner";
std::cout << "Files in " << path << " directory :" << std::endl;
for (const auto & entry : std::filesystem::directory_iterator(path))
std::cout << entry.path() << std::endl;
return 0;
}
When I compile it on my MacBook Pro (clang version 11.0.3 (clang-1103.0.32.62)) with
g++ -o test test.cpp -std=c++17 -Wall
everything works fine. But as soon as I move to Linux (Ubuntu 19.04, g++ 8.3.0) I get the following error:
test.cpp: In function ‘int main()’:
test.cpp:8:33: error: ‘std::filesystem’ has not been declared
for (const auto & entry : std::filesystem::directory_iterator(path)){
I include then the filesystem library with #include <filesystem>:
#include <iostream>
#include <fstream>
#include <filesystem>
int main(){
std::string path = "Inner";
std::cout << "Files in " << path << " directory :" << std::endl;
for (const auto & entry : std::filesystem::directory_iterator(path))
std::cout << entry.path() << std::endl;
return 0;
}
compile it via g++ -o test test.cpp -std=c++17 -Wall -lstdc++fs and everything works fine on Linux too (note that I had to add -lstdc++fs).
Why is there this different behaviour on MacOS and on Linux? Does it depends on the compiler? What happens with Windows OS (I do not have any Windows PC at home)?
I found a related question and its answer here, but it does not seem to explain why in the first case (with clang) everything works fine also without including filesystem library.

Using 'g++' is not using clang you should use 'clang++'
Gcc should not be platform dependent but it might be different version
At any case, you should explicitly include header files needed, and std::filesystem is defined in "<filesystem>"
regarding the need to add "lstdc++fs' - this is a hint that actually g++ version is different and uses different llvm versions. As described in https://en.cppreference.com/w/cpp/filesystem
Notes:
Using this library may require additional compiler/linker options. GNU implementation prior to 9.1 requires linking with -lstdc++fs and LLVM implementation prior to LLVM 9.0 requires linking with -lc++fs

Related

Runtime error for CLang compiled program (Mac) reading double type with std::cin

I'm pretty new to C++ and working through some examples of the book "Programming Principles and Practices Using C++" (2nd Edition). I wrote the following simple Program (in file Main.cpp):
#include <iostream>
#include <string>
int main () {
double d = 0;
std::string s = "";
while (std::cin >> d >> s) {
std::cout << "--" << d << " " << s << "\n";
}
std::cout << "FATAL? "<< d << " " << u << "\n";
}
Compiling the program (on the command line) with CLang (Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.2.0 Thread model: posix):
clang++ -o Main -std=c++11 -stdlib=libc++ Main.cpp
works fine without any errors. However, when I run the program it behaves strange. I tested the following input:
123m
which results in
--123 m
which is fine (the same holds for entering 123 m). But, entering the following:
123a
results in:
FATAL? 0 m
The same happens for most other characters (e.g. b, c, ...). Entering 123 a works fine though (output: --123 a).
Using GNU g++ works on the other hand. Further, the problem does not come up on a Linux machine compiling the same program with CLang.
As stated before, I'm new to C++ and this seems to be a Mac OS X specific problem. Is this a bug in the Mac CLang implementation or am I doing something seriously wrong here :(?
Thanks in advance!
Ok, I found a solution to the problem in this question asked: CGAL: How can I successfully compile and link CGAL examples (on Mac OS X 10.9 Mavericks)
Compiling with clang++ as follows:
clang++ -o Main -std=c++11 -stdlib=libstdc++ Main.cpp
instead of:
clang++ -o Main -std=c++11 -stdlib=libc++ Main.cpp
solved the problem.
Anyway, as libc++ should be the preferred library to use with clang++ (as I just was told offline) I think it's time for a bug report.
std::basic_istream::operator>> calls std::num_get::get to extract the value from input. Until C++11, the behaviour of std::num_get::get was like that of scanf with the appropriate formatting string. C++11 onwards, std::num_get::get ends up calling strto* functions, which have a more flexible matching than the one based on scanf. In your example, 123[a-f] get interpreted as hex. Since all the input has been consumed by >>d, the >>s part of while(std::cin >> d >> s) leads to the parse failing.

OS X 10.9: cannot debug c++ dynamic library made with g++

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.

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.

Cannot get <random> library to work

I'm having trouble getting the library working on macosx. First off, I tried to compile the following code, saved as rand.cpp, taken from the c++ website
#include <iostream>
#include <random>
int main()
{
const int nrolls=10000; // number of experiments
const int nstars=100; // maximum number of stars to distribute
std::default_random_engine generator;
std::normal_distribution<double> distribution(5.0,2.0);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
double number = distribution(generator);
if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
}
std::cout << "normal_distribution (5.0,2.0):" << std::endl;
for (int i=0; i<10; ++i) {
std::cout << i << "-" << (i+1) << ": ";
std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl;
}
return 0;
}
Upon running this with g++ rand.cpp -o rand i get the following errors
rand.cpp:9: error: ‘default_random_engine’ is not a member of ‘std’
rand.cpp:10: error: ‘normal_distribution’ is not a member of ‘std’
Searching around it seems to be suggested that the issue is the compiler, apparently thus library is only available to gcc11. I found a way to update gcc using the macport package as shown here Update GCC on OSX but I still don't know how to use this new compiler. Running g++ rand.cpp -o rand returns the same errors even when I change the compiler with sudo port select --set gcc gcc40 or sudo port select --set gcc mp-gcc46. I also tried using g++ -std=c++11 rand.cpp -o rand which just returns
cc1plus: error: unrecognized command line option "-std=c++11"
Does anyone know what I am doing wrong?
Try it with Clang++, which should be installed in your mac, or a new version of GCC.
gcc42: I had this version installed, it didn't work, and didn't recognize -std=c++0x and -std=c++11.
gcc49: Installed this with brew, it gave the same error but -std=c++11 made it work.
Clang++: Worked like a charm without even specifying the standard (it probably defaults to c++11).
Also, check if you have the latest version of the command line tools, if not, download them from the Downloads for Apple Developers website.
What you're doing wrong
The version you installed doesn't have the -std=c++11 option, but it should work with -std=c++0x or -std=gnu++0x, that's what it says in the manual for the 4.6 version.

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