I'm trying to code the BST ADT, and the specification we were given requires use of 'auto' that is only included in C++14. I'm trying to compile, but I keep getting errors that 'auto' is only included in C++14, so I'm just wondering if there's a different way to compile the code so that it includes C++14? In every previous project I've done (over the last three semesters) I've been able to compile the file (say called main.cpp) just by using the code:
g++ -o main main.cpp
I've tried the following compile code
g++ -std=c++14 -o main main.cpp
but when I do that, I get like 100 errors that look like
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:1002:86: error: member reference base type 'std::__1::basic_string::__self_view' (aka 'int') is not a structure or union
append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:1002:99: error: member reference base type 'std::__1::basic_string::__self_view' (aka 'int') is not a structure or union
append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
What do these even mean?
I figured out the reasons behind the errors (turned out to be templating mistakes), but in the end compiling with g++ -std=c++14 -o main main.cpp is working. I still don't really understand any of the compilation stuff though, so if someone could explain a little bit/ provide a link I would appreciate
(this is not an answer, but too long for a comment)
Where did you get your g++ from? On most modern Mac OS installs, it is a symlink for clang++. What does g++ --version print?
Certainly you're using libc++, not libstdc++. The __1 in the type names is a giveaway, along with the path to <string>.
And the error message ('std::__1::basic_string::__self_view' (aka 'int') is weird, too. basic_string::__self_view is a string_view, not an int.
if you use Unix based operating systems such as Linux and OS X try this:
clang++ -g -std=c++1y main.cpp -o main
Related
Apparently, CERN's "Root" software is compatible (as of version 6.12) with C++17. However, I have been completely unable to get this to work with the newest version (6.20) and all previous questions I found on this topic are from a few years ago.
Does anyone with Root experience know if there is a particular compiler option to get it to work with C++17?
Examples of errors I get when trying to compile a trivial "Hello world" program:
The following is (I think) due to the "TFile.h" header implicitly loading "TString.h":
/usr/local/bin/root_v6.20.02/include/ROOT/RStringView.hxx:32:84: error: conflicting declaration of template ‘template<class _CharT, class _Traits> using basic_string_view = std::experimental::__ROOT::basic_string_view<_CharT, _Traits>
An example of another (more mysterious, to me at least) error is:
/usr/include/c++/7/ext/concurrence.h:53:16: error: ‘_Lock_policy’ does not name a type
static const _Lock_policy __default_lock_policy =
^~~~~~~~~~~~
In file included from /usr/include/c++/7/iostream:38:0,
from test.cpp:1:
/usr/include/c++/7/ext/concurrence.h: In function ‘void std::__throw_concurrence_lock_error()’:
/usr/include/c++/7/ext/concurrence.h:102:5: error: ‘__concurrence_lock_error’ was not declared in this scope
{ _GLIBCXX_THROW_OR_ABORT(__concurrence_lock_error()); }
^
/usr/include/c++/7/ext/concurrence.h:102:5: note: suggested alternative:
In file included from /usr/include/c++/7/memory:74:0,
from /usr/local/bin/root_v6.20.02/include/ROOT/TypeTraits.hxx:15,
from /usr/local/bin/root_v6.20.02/include/TString.h:29,
from /usr/local/bin/root_v6.20.02/include/TNamed.h:26,
from /usr/local/bin/root_v6.20.02/include/TKey.h:15,
from /usr/local/bin/root_v6.20.02/include/TBasket.h:28,
from /usr/local/bin/root_v6.20.02/include/ROOT/TIOFeatures.hxx:14,
from /usr/local/bin/root_v6.20.02/include/TTree.h:30,
from /usr/local/bin/root_v6.20.02/include/TNtuple.h:24,
from test.cpp:2:
/usr/include/c++/7/ext/concurrence.h:67:9: note: ‘__gnu_cxx::__concurrence_lock_error’
class __concurrence_lock_error : public std::exception
I am using the latest version of g++ and Root, on Linux, and have the following options in my Makefile:
CXX = g++
CXXFLAGS = -march=native `root-config --cflags --libs` -std=c++17
LDFLAGS = `root-config --cflags --libs`
Any comments about a possible workaround would be appreciated, or perhaps there is some additional option I need to add in the Makefile? It would also be helpful to know if it is advisable to stick to C++11/14 when using Root libraries.
You can use root only with the c++ standard with which your root version has been compiled. This is a known inconvenience and I believe the developers would be happy if/when they get this sorted out. The build configuration root-config --cflags should set the appropriate c++ standard. This might very well be c++17 (it is for me)
pseyfert#robusta:~ > root-config --cflags
-pthread -std=c++17 -m64 -fdiagnostics-color -march=native -Wextra -Wall -Wshadow -I/home/pseyfert/coding/root-install/include
If you compile root from source, then CMAKE_CXX_STANDARD=17 or cxx17=ON (doc) are the build options you want to set to compile root with c++17 and use c++17 in all downstream projects.
If you use a binary installation of root that switch needs to be done by whoever provides the build.
I'm currently trying to compiling my program with the -std=c++98 flag on OS X 10.10:
clang++ -std=c++98 -pedantic -W -Wall -Werror *.cpp
g++ -std=c++98 -pedantic -W -Wall -Werror *.cpp
Strangely when I compile with OS 10.10 no error are showed while some are showed with a GNU/Linux distribution.
With the GNU/Linux distribution I have some errors because I use s.open(file); instead of s.open(file.c_str()); but on OS X no error, even by using s.open(file);.
Maybe there is just a link between this error and the filesystem of each OS?
You're correct. This code shouldn't compile under C++98:
#include <fstream>
#include <string>
int main() {
std::string file("/tmp/foo.txt");
std::ifstream s;
s.open(file);
}
It's technically a bug in libc++, but it's one that I doubt that they will want to fix.
If you wanted, you could compile with libstdc++ as the standard library, which doesn't have this feature implemented (at least the version that apple distributes doesn't).
[10:13am][wlynch#watermelon /tmp] clang++ -std=c++98 -stdlib=libstdc++ foo.cc
foo.cc:7:12: error: no viable conversion from 'std::string' (aka 'basic_string<char>') to 'const char *'
s.open(file);
^~~~
/usr/include/c++/4.2.1/fstream:518:24: note: passing argument to parameter '__s' here
open(const char* __s, ios_base::openmode __mode = ios_base::in)
^
1 error generated.
The problem is that on OS X you're still using a C++11 implementation of the standard library, which includes such API changes as the ostream::open method accepting std::strings.
Changing the C++ standard with -std=c++98 does not affect the standard library being used, and libc++ does not implement C++98 (e.g., there's no #ifdef to remove those open(std::string) APIs when in C++98 mode) whereas I think libstdc++ does hide non-c++98 APIs when building in C++98 mode.
Problem Solved => see the update at the end
I'm trying to use std::function but it looks like just include <functional> does not provide the definition. I have tried to compile following code:
#include <functional>
std::function<int(int)> f = nullptr;
with c++11 as compile option:
% clang++ -c -std=c++11 t.cc
cause:
t.cc:3:6: error: no type named 'function' in namespace 'std'
std::function<int(int)> f = nullptr;
~~~~~^
t.cc:3:14: error: expected unqualified-id
std::function<int(int)> f = nullptr;
^
2 errors generated.
what am I missing? I know C++ well but new to clang++/C++11 thus I lack of important knowledge, I guess.
I'm using clang++ on MacOS X 10.8.
Update 1
I have tried a sample at cppreference.com site but it won't compile too. Giving some option solve the problem?
Update 2
Tried above sample from cppreference.com with clang++ -c -std=c++11 -stdlib=libc++11 x.cc, and compiler still says:
x.cc:1:10: fatal error: 'functional' file not found
#include <functional>
^
1 error generated.
Where is functional? I guess I should give -stdlib=libc++11 or whatever but it does not work too:
clang: error: invalid library name in argument '-stdlib=libc++11'
How I can find list of argument for -stdlib? (note: in man page, only available options are libc++ and libstdc++ both of them don't work)
Or functional just does not work?
This is not about the definition of the function. You don't have a linker error. You have a compiler error. The problem is, presumably, that the BSD/GNU/Darwin standard library installed in the real sysroot doesn't support C++11. You have to use the one that comes with Clang by specifying the -stdlib=libc++ compiler flag.
For C++11, it's best to always invoke clang as: clang++ -std=c++11 -stdlib=libc++
I use this most of the time, so I set the environment variable $CXX to this value. That way, I'm getting the dialect and library option in both compilation and linking. -std=c++11 is insufficient, as clang will still use the (old) system gcc headers in /usr/include/c++/4.2.1.
-stdlib=libc++ will use the clang headers in /usr/lib/c++/v1 such as <functional>.
There's a similar question with an answer by Howard Hinnant, who is (IIRC) an Apple engineer.
I'd like to try the new Hinnant's short_alloc allocator that, as far as I can understand, replaces the old stack_alloc allocator. However, I can't manage to compile the vector example. g++ says:
~# g++ -std=c++11 stack-allocator-test.cpp -o stack-allocator-test
In file included from stack-allocator-test.cpp:6:0:
short_alloc.h:11:13: error: ‘alignment’ is not a type
short_alloc.h:11:22: error: ISO C++ forbids declaration of ‘alignas’ with no type [-fpermissive]
short_alloc.h:11:22: error: expected ‘;’ at end of member declaration
As far as I can tell, g++ complains about line 10 and 11:
static const std::size_t alignment = 16;
alignas(alignment) char buf_[N];
It seems that the compiler doesn't like the "expression version" of alignas but it expects just the "type-id version".
I'm using g++ 4.7.2 under Ubuntu 12.10.
~# g++ --version
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Probably I'm missing something obvious, but I can't figure it out. Any help would be appreciated. (Please don't tell me I have to upgrade to a newer g++, I'm too lazy to do that :)
g++-4.7.2 doesn't support alignas. From http://gcc.gnu.org/projects/cxx0x.html:
Alignment support | N2341 | GCC 4.8
Try using g++-4.8.0 or clang; alternatively you may be able to use the __attribute__((aligned)):
__attribute__((aligned (8))) char buf_[12];
Note that __attribute__((aligned)) only accepts certain integer constant expressions (literals, template parameters); it doesn't accept static const variables.
The offending code:
template <class Bar,
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
size_t foo(Bar const& b) { omitted... }
It compiles fine on gcc 4.7.2 with -std=c++11. On clang 3.0 I get the following error:
foo.hpp:35:28: error: non-type template argument of type 'unsigned long' is not an integral constant expression
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As far as I can tell, I am supposed to be able to use numeric_limits in this way in c++11. Is clang wrong here, or am I unaware of something?
EDIT:
Compilation flags are: clang++ -o foo.o -c -W -Wall -Wextra -Werror -std=c++11 -stdlib=libc++ -g -I. foo.cpp
Your code compiles just fine with clang++ 3.2, see here.
I would say there is nothing wrong with your code but you should upgrade to a newer version of clang.
Note: The code doesn't compile with the Intel C++ Compiler 13.0.1 due to a compiler bug (thanks #Xeo):
Compilation finished with errors:
source.cpp(6): internal error: assertion failed: ensure_il_scope_exists: NULL IL scope (shared/cfe/edgcpfe/il.c, line 7439)
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
^
compilation aborted for source.cpp (code 4)
To use C++11 library features with clang you need to use the libc++ standard library implementation, otherwise you get the ancient library from GCC 4.1.2, which doesn't support C++11
See https://stackoverflow.com/a/14790442/981959 and https://stackoverflow.com/a/14150421/981959 and many other questions.