I have a preexisting product that builds on Linux, and I'm trying to port it to Mac OS.
msoulier#merlin:~$ xcode-select -v
xcode-select version 2343.
msoulier#merlin:~$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
The problem is that it uses the tr1/tuple library, and for some reason,
that is not in the default include path.
msoulier#merlin:~$ ls /usr/include/c++/4.2.1/tr1/tuple
/usr/include/c++/4.2.1/tr1/tuple
So it's there, which should be in the include path based on the
--with-gxx-include-dir option above,
And yet
msoulier#merlin:~$ cat hello.cpp
#include <iostream>
#include <tr1/tuple>
using namespace std;
int main(void) {
cout << "Hello, World!" << endl;
return 0;
}
msoulier#merlin:~$ g++ -o hello hello.cpp
hello.cpp:2:10: fatal error: 'tr1/tuple' file not found
#include <tr1/tuple>
^
1 error generated.
Why doesn't this just work?
Thanks.
Short answer: call clang++ with -stdlib=libstdc++, and the tr1 headers will be there.
Long answer:
The reason for your error and the 2 sets of C++ includes is that macOS/Xcode has two different C++ standard libraries you can build against: an old GNU libstdc++, and the new and modern LLVM libc++.
As of macOS 10.12 Sierra, the default is now libc++ and libstdc++ is deprecated. libstdc++ is quite old, v4.2.1, and predates C++11 (hence the tr1 headers). If you're going to be maintaining this code long-term, it'd be worth the time to make it C++11 compliant (i.e. #include <tuple>)
Update: Xcode 10 no longer allows building against libstdc++. Either update your codebase to use standard C++11 headers, or use Xcode 9 if that's really not an option.
Related
I have a piece of code that looks like the following. Let's say it's in a file named example.cpp
#include <fstream>
#include <string> // line added after edit for clarity
int main() {
std::string filename = "input.txt";
std::ifstream in(filename);
return 0;
}
On a windows, if I type in the cmd the command g++ example.cpp, it will fail. It's a long list of errors I think mostly due to the linker complaining about not being able to convert from string to const char*.
But if I run the compiler using an additional argument like so: g++ -std=c++17 example.cpp, it will compile and work fine with no problems.
What happens when I run the former command? I'm guessing a default version standard of the C++ compiler gets called, but I don't know which? And as a programmer/developer, should I always use the latter command with the extra argument?
If your version of g++ is later than 4.7 I think you can find the default version of C++ standard supported like so:
g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
An example from my machine:
mburr#mint17 ~ $ g++ --version | head -1
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
mburr#mint17 ~ $ g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
#define __cplusplus 199711L
Some references:
Details on the g++ options used
Why this only works for g++ 4.7 or later
g++ man page actually tells what is the default standard for C++ code.
Use following script to show the relevant part:
man g++ | col -b | grep -B 2 -e '-std=.* This is the default'
For example, in RHEL 6 g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), the output:
gnu++98
GNU dialect of -std=c++98. This is the default for C++ code.
And in Fedora 28 g++ (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1), the output:
gnu++14
gnu++1y
GNU dialect of -std=c++14. This is the default for C++ code. The name gnu++1y is deprecated.
You can also check with gdb
$ g++ example.cpp -g Compile program with -g flag to generate debug info
$ gdb a.out Debug program with gdb
(gdb) b main Put a breakpoint at main
(gdb) run Run program (will pause at breakpoint)
(gdb) info source
Prints out something like:
Current source file is example.cpp
Compilation directory is /home/xxx/cpp
Located in /home/xxx/cpp/example.cpp
Contains 7 lines.
Source language is c++.
Producer is GNU C++14 6.3.0 20170516 -mtune=generic -march=x86-64 -g.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.
There is the standard used by compiler: Producer is GNU C++14
If you recompile your program using -std=c++11 (for example), gdb detects it:
Producer is GNU C++11
I believe that it is possible to tell by looking at the man page (at least for g++):
Under the description of -std, the man page lists all C++ standards, including the GNU dialects. Under one specific standard, it is rather inconspicuously stated, This is the default for C++ code. (there is an analogous statement for C standards: This is the default for C code.).
For instance, for g++/gcc version 5.4.0, this is listed under gnu++98/gnu++03, whereas for g++/gcc version 6.4.0, this is listed under gnu++14.
I'm guessing a default version of the C++ compiler gets called, but I don't know which?
This is only guessable by reading the documentation of your particular compiler version.
If using a recent GCC, I recommend first to understand what version are you using by running
g++ -v
or
g++ --version
and then refer to the version of the particular release of GCC. For example for GCC 7, read GCC 7 changes etc
Alternatively, run
g++ -dumpspecs
and decipher the default so called spec file.
BTW, you could ensure (e.g. in some of your common header file) that C++ is at least C++17 by coding
#if __cplusplus < 201412L
#error expecting C++17 standard
#endif
and I actually recommend doing it that way.
PS. Actually, think of C++98 & C++17 being two different languages (e.g. like Ocaml4 and C++11 are). Require your user to have a compiler supporting some defined language standard (e.g. C++11), not some particular version of GCC. Read also about package managers.
If you are using the GCC compiler, you can find it on the man pages:
man g++ | grep "This is the default for C++ code"
Typing g++ --version in your command shell will reveal the version of the compiler, and from that you can infer the default standard. So you can't tell directly but you can infer it, with some effort.
Compilers are supposed to #define __cplusplus which can be used to extract the standard that they purport to implement at compile time; but many don't do this yet.
(And don't forget to include all the C++ standard library headers you need: where is the one for std::string for example? Don't rely on your C++ standard library implementation including other headers automatically - in doing that you are not writing portable C++.)
Your question is specific to gnu compilers, so probably better to tag it appropriately, rather than just C++ and C++11.
Your code will compile with any compilers (and associated libraries) compliant with C++11 and later.
The reason is that C++11 introduced a std::ifstream constructor that accepts a const std::string &. Before C++11, a std::string could not be passed, and it would be necessary in your code to pass filename.c_str() rather than filename.
According to information from gnu, https://gcc.gnu.org/projects/cxx-status.html#cxx11, gcc.4.8.1 was the first version to fully support C++11. At the command line g++ -v will prod g++ to telling you its version number.
If you dig into documentation, you might be able to find the version/subversion that first supported enough features so your code - as given - would compile. But such a version would support some C++11 features and not others.
Since windows isn't distributed with g++, you will have whatever version someone (you?) has chosen to install. There will be no default version of g++ associated with your version of windows.
The default language standards for both C and C++ are specified in the GCC Manuals. You can find these as follows:
Browse to https://gcc.gnu.org/onlinedocs/
Select the GCC #.## Manual link for the version of GCC you are interested in, e.g. for GCC 7.5.0:
https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/
Click the topic link Language Standards Supported by GCC, followed by the topic C++ Language (or C language). Either of these topics will have a sentence such as:
The default, if no C++ language dialect options are given, is -std=gnu++14.
The default, if no C language dialect options are given, is -std=gnu11.
The above two examples are for GCC 7.5.0.
I have a piece of code that looks like the following. Let's say it's in a file named example.cpp
#include <fstream>
#include <string> // line added after edit for clarity
int main() {
std::string filename = "input.txt";
std::ifstream in(filename);
return 0;
}
On a windows, if I type in the cmd the command g++ example.cpp, it will fail. It's a long list of errors I think mostly due to the linker complaining about not being able to convert from string to const char*.
But if I run the compiler using an additional argument like so: g++ -std=c++17 example.cpp, it will compile and work fine with no problems.
What happens when I run the former command? I'm guessing a default version standard of the C++ compiler gets called, but I don't know which? And as a programmer/developer, should I always use the latter command with the extra argument?
If your version of g++ is later than 4.7 I think you can find the default version of C++ standard supported like so:
g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
An example from my machine:
mburr#mint17 ~ $ g++ --version | head -1
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
mburr#mint17 ~ $ g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
#define __cplusplus 199711L
Some references:
Details on the g++ options used
Why this only works for g++ 4.7 or later
g++ man page actually tells what is the default standard for C++ code.
Use following script to show the relevant part:
man g++ | col -b | grep -B 2 -e '-std=.* This is the default'
For example, in RHEL 6 g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), the output:
gnu++98
GNU dialect of -std=c++98. This is the default for C++ code.
And in Fedora 28 g++ (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1), the output:
gnu++14
gnu++1y
GNU dialect of -std=c++14. This is the default for C++ code. The name gnu++1y is deprecated.
You can also check with gdb
$ g++ example.cpp -g Compile program with -g flag to generate debug info
$ gdb a.out Debug program with gdb
(gdb) b main Put a breakpoint at main
(gdb) run Run program (will pause at breakpoint)
(gdb) info source
Prints out something like:
Current source file is example.cpp
Compilation directory is /home/xxx/cpp
Located in /home/xxx/cpp/example.cpp
Contains 7 lines.
Source language is c++.
Producer is GNU C++14 6.3.0 20170516 -mtune=generic -march=x86-64 -g.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.
There is the standard used by compiler: Producer is GNU C++14
If you recompile your program using -std=c++11 (for example), gdb detects it:
Producer is GNU C++11
I believe that it is possible to tell by looking at the man page (at least for g++):
Under the description of -std, the man page lists all C++ standards, including the GNU dialects. Under one specific standard, it is rather inconspicuously stated, This is the default for C++ code. (there is an analogous statement for C standards: This is the default for C code.).
For instance, for g++/gcc version 5.4.0, this is listed under gnu++98/gnu++03, whereas for g++/gcc version 6.4.0, this is listed under gnu++14.
I'm guessing a default version of the C++ compiler gets called, but I don't know which?
This is only guessable by reading the documentation of your particular compiler version.
If using a recent GCC, I recommend first to understand what version are you using by running
g++ -v
or
g++ --version
and then refer to the version of the particular release of GCC. For example for GCC 7, read GCC 7 changes etc
Alternatively, run
g++ -dumpspecs
and decipher the default so called spec file.
BTW, you could ensure (e.g. in some of your common header file) that C++ is at least C++17 by coding
#if __cplusplus < 201412L
#error expecting C++17 standard
#endif
and I actually recommend doing it that way.
PS. Actually, think of C++98 & C++17 being two different languages (e.g. like Ocaml4 and C++11 are). Require your user to have a compiler supporting some defined language standard (e.g. C++11), not some particular version of GCC. Read also about package managers.
If you are using the GCC compiler, you can find it on the man pages:
man g++ | grep "This is the default for C++ code"
Typing g++ --version in your command shell will reveal the version of the compiler, and from that you can infer the default standard. So you can't tell directly but you can infer it, with some effort.
Compilers are supposed to #define __cplusplus which can be used to extract the standard that they purport to implement at compile time; but many don't do this yet.
(And don't forget to include all the C++ standard library headers you need: where is the one for std::string for example? Don't rely on your C++ standard library implementation including other headers automatically - in doing that you are not writing portable C++.)
Your question is specific to gnu compilers, so probably better to tag it appropriately, rather than just C++ and C++11.
Your code will compile with any compilers (and associated libraries) compliant with C++11 and later.
The reason is that C++11 introduced a std::ifstream constructor that accepts a const std::string &. Before C++11, a std::string could not be passed, and it would be necessary in your code to pass filename.c_str() rather than filename.
According to information from gnu, https://gcc.gnu.org/projects/cxx-status.html#cxx11, gcc.4.8.1 was the first version to fully support C++11. At the command line g++ -v will prod g++ to telling you its version number.
If you dig into documentation, you might be able to find the version/subversion that first supported enough features so your code - as given - would compile. But such a version would support some C++11 features and not others.
Since windows isn't distributed with g++, you will have whatever version someone (you?) has chosen to install. There will be no default version of g++ associated with your version of windows.
The default language standards for both C and C++ are specified in the GCC Manuals. You can find these as follows:
Browse to https://gcc.gnu.org/onlinedocs/
Select the GCC #.## Manual link for the version of GCC you are interested in, e.g. for GCC 7.5.0:
https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/
Click the topic link Language Standards Supported by GCC, followed by the topic C++ Language (or C language). Either of these topics will have a sentence such as:
The default, if no C++ language dialect options are given, is -std=gnu++14.
The default, if no C language dialect options are given, is -std=gnu11.
The above two examples are for GCC 7.5.0.
I don't get this. GCC is supposed to support but accoriding to their
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.tr1
Status page "7 Regular Expressions are not supported".
But then at "28 Regular expressions" - they are checked as supported
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011
Could you please explain what is actually the standard and what is not?
GCC 4.9 does indeed support the C++11 <regex> functionality but not the tr1 version. Note that the difference is that parts (all?) of the latter exist within a tr1:: namespace while the C++11 <regex> is within namespace std. There's not much point to going backwards and adding in tr1 support when C++11 has been published for some time now.
Following information can be found from GCC 4.9 release notes:
"Support for various C++14 additions have been added to the C++ Front End, on the standard C++ library side the most important addition is support for the C++11 regex"
If you want to install the latest GCC4.9 version to try by yourself you can follow below SO link:
How do I compile and run GCC 4.9.x?
Here is the sample program which has compiled using gcc4.9 and subsequent run.
//Sample Program
#include <regex>
#include <iostream>
using namespace std;
int main() {
regex reg("[0-9]+");
if (regex_match("123000", reg)) {
cout << "It's a match!" <<endl;
}
return 0;
}
$g++ -std=c++11 foo.cpp -o foo
$ g++ -v
Using built-in specs.
COLLECT_GCC=/home/mantosh/gcc-4.9.0/bin/g++
COLLECT_LTO_WRAPPER=/home/mantosh/gcc-4.9.0/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /home/mantosh/objdir/../gcc-4.9.0/configure --disable-multilib --prefix=/home/mantosh/gcc-4.9.0
Thread model: posix
gcc version 4.9.0 (GCC)
$ ./foo
It's a match!
I'm writing a simple C++ program to demonstrate the use of locks. I am using codeblocks and gnu gcc compiler.
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int x = 0; // shared variable
void synchronized_procedure()
{
static std::mutex m;
m.lock();
x = x + 1;
if (x < 5)
{
cout<<"hello";
}
m.unlock();
}
int main()
{
synchronized_procedure();
x=x+2;
cout<<"x is"<<x;
}
I'm getting the following error: mutex in namespace std does not name a type.
Why am I getting this error?
Doesn't the compiler support use of locks?
I happened to be looking at the same problem. GCC works fine with std::mutex under Linux. However, on Windows things seem to be worse. In the <mutex> header file shipped with MinGW GCC 4.7.2 (I believe you are using a MinGW GCC version too), I have found that the mutex class is defined under the following #if guard:
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
Regretfully, _GLIBCXX_HAS_GTHREADS is not defined on Windows. The runtime support is simply not there.
You may also want to ask questions directly on the MinGW mailing list, in case some GCC gurus may help you out.
EDIT: The MinGW-w64 projects provides the necessary runtime support. Check out http://mingw-w64.sourceforge.net/ and https://sourceforge.net/projects/mingw-w64/files/. Also, as 0xC0000022L pointed out, you need to download the POSIX thread version (I missed mentioning it last time).
Use POSIX threading model for MINGW:
$ sudo update-alternatives --config i686-w64-mingw32-gcc
<choose i686-w64-mingw32-gcc-posix from the list>
$ sudo update-alternatives --config i686-w64-mingw32-g++
<choose i686-w64-mingw32-g++-posix from the list>
$ sudo update-alternatives --config x86_64-w64-mingw32-gcc
<choose x86_64-w64-mingw32-gcc-posix from the list>
$ sudo update-alternatives --config x86_64-w64-mingw32-g++
<choose x86_64-w64-mingw32-g++-posix from the list>
See also: mingw-w64 threads: posix vs win32
This has now been included in MingW (Version 2013072300). To include it you have to select the pthreads package in the MinGW Installation Manager.
Mutex, at least, is not supported in 'Thread model: win32' of the Mingw-builds toolchains. You must select any of the toolchains with 'Thread model: posix'. After trying with several versions and revisions (both architectures i686 and x86_64) I only found support in x86_64-4.9.2-posix-seh-rt_v3-rev1 being the thread model, IMO, the determining factor.
I encountered this same problem when using MingW-W64 7.2.0. I tested out several different Windows builds from the mingw-64 download page, and found that MinGW-W64 GCC-8.1.0 supports mutex and contains the pthread library. When installing, I selected the following options:
x86_64
posix
seh
My multi-threaded code based on pthreads now compiles and runs cleanly on both Windows and Linux with no changes.
This version is leaner than the 7.3.0 build I was using because it doesn't have a CygWin environment or package manager. I also copied mingw32-make.exe to make.exe so my Makefile wouldn't need to be modified. The installer creates a "Run terminal" link in the Windows Start Menu.
I got the same error with gcc4.7.7.
After adding "-std=c++0x", it is fixed.
my gcc version is 5.4 and i solved this problem when adding #include and also adding -std=c++11 at my CmakeLists.txt as below:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native -std=c++11")
I fixed it by following steps:
Project > Build option...
The default selected compiler: GNU GCC Compiler
On tab "Compiler settings / Compiler flags", check option
"Have g++ follow the C++11 ISO C++ language standard [-std=c++11]"
I don't know if it works for everybody, but in other way you just have to update your ndk. I'm using ndk-r11c and it works perfectly.
Many classes of the standard thread library can be replaced with the boost ones. A very easy workaround is to change the entire standard mutex file with a couple of lines.
#include <boost/thread.hpp>
namespace std
{
using boost::mutex;
using boost::recursive_mutex;
using boost::lock_guard;
using boost::condition_variable;
using boost::unique_lock;
using boost::thread;
}
And do not forget to link against boost thread library.
I am trying to include atomic in my C++ pthread program.
#include <atomic>
But, i got error:
error: atomic: No such file or directory
I tried :
#include <asm/atomic>
#include <atomic.h>
#include <linux/atomic>
#include <util/atomic>
#include <stdcatomic>
#include <catomic>
No one works.
My gcc is gcc version 4.1.2 20080704 (Red Hat 4.1.2-52)
The post :
#include <cstdatomic> "no such file" in ubuntu
std::atomic support in g++ 4.4.3
do not work either.
And #include <thread> also got : No such file or directory
Any help will be appreciated.
thanks !
UPDATE,
I am trying to install GCC 4.7 on Linux, but in "make check", I got error,
autogen -T /remote/mypath/gcc_4_7_2012_5_28/gcc_4_7_new_2012_5_29/trunk/fixincludes/check.tpl , remote/mypath/gcc_4_7_2012_5_28/gcc_4_7_new_2012_5_29/trunk/fixincludes/inclhack.def ,
make[2]: execvp: autogen: Permission denied, then I tried to install autogen, but
got: I need to install guile-devel, then when I installed guile-2.0.5-2.1.src.rpm ,
I got rpm -ivh guile-2.0.5-2.1.src.rpm, warning: guile-2.0.5-2.1.src.rpm: Header V3
RSA/SHA256 signature: NOKEY, key ID 3dbdc284
error: cannot write to %sourcedir /usr/src/redhat/SOURCES, I cannot get root
authorization.
Any help will be appreciated. thanks !
Your post is a little confusing, since you link to a post that says GCC 4.4 is required for atomic, yet you are expecting it to work on 4.1.x.
You'll need to upgrade to at least GCC 4.4 for atomic support. There is no way to enable support for atomic in GCC 4.1, though you can use boost::atomic instead. For future reference, this chart shows which version of GCC supports which C++11 features.
If for some reason you can't upgrade GCC, you could also try using a different compiler altogether (e.g. clang).
This table keeps track of gcc implementation status. Your compiler version doesn't support atomics yet.
In any case you need to use the std=c++11 (or c++0x in older versions) switch to enable C++11 features.
Indeed gcc 4.4 could not compile for me a code containing #include <atomic>.
This happened while compiling someone else's code (fasttext), so I prefered to avoid changing the code to #include <cstdatomic>.
I upgraded to gcc 4.8 and it worked.
If you're using CentOS 6.8, here is how to upgrade gcc.