C++: use of undeclared identifier 'u8' - c++

const char* hi = "hi"; // ok
const char* hi2 = u8"hi"; // compile error
It causes
error: use of undeclared identifier 'u8'
My compiler supports C++ 11.
~/Project/cpp  c++ -v
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin15.0.0
Thread model: posix

Your compiler doesn't default to C++11 though.
[9:46pm][wlynch#watermelon /tmp] c++ -c blah.cc
blah.cc:2:19: error: use of undeclared identifier 'u8'
const char* hi2 = u8"hi"; // compile error
^
blah.cc:2:21: error: expected ';' after top level declarator
const char* hi2 = u8"hi"; // compile error
^
;
2 errors generated.
[9:47pm][wlynch#watermelon /tmp] c++ -c -std=c++11 blah.cc
[9:47pm][wlynch#watermelon /tmp]

Related

Why doesn't std::rotl() compile on my Clang 13.1.6 installation?

I've installed brew on my new laptop and when attempting to compile my code with Clang 13.1.6 I get an identifier error using std::rotl(). What could be the reason for this? I believe it works fine on my Windows computer.
This is my code:
#include <bit>
int main()
{
std::rotl(100, 1);
}
This is the error message I get:
antonadamson#AirsomthorAnton ~ % g++ test.cpp -o -std=c++20 test
test.cpp:7:3: error: use of undeclared identifier 'rotl'
rotl(100, 1);
^
1 error generated.

C++ Union Curly Bracket Initinitializer: "error: expected member name or ';' after declaration specifiers"

I'm new to C++ and read about curly bracket initializer which is available in C++ 11. I try to create a simple union which looks like this
union UExample {
UExample(const uint12_t value = 0) : raw{value} {}
uint12_t raw;
};
When I try to compile the file I get this error
./stack.h:18:57: error: expected member name or ';' after declaration specifiers
UBankedAddress(const uint12_t value = 0) : raw{value} {}
^
./stack.h:18:49: error: expected '('
UBankedAddress(const uint12_t value = 0) : raw{value} {}
^
./stack.h:18:55: error: expected ';' after expression
UBankedAddress(const uint12_t value = 0) : raw{value} {}
Dose anyone has an idea to solve this?
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I found a solution for this problem. It relates to the g++ compiler on MacOS. clang++ should be used instead.
Before (not working):
g++ stack.cpp -o stack
After (working):
clang++ -std=c++11 -stdlib=libc++ stack.cpp -o stack

unable to compile c++ code in macbook due to __GLIBC__ issue

g++ -std=c++11 -Wall -g threads.cpp -o threads.out
In file included from threads.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:401:32: error: use of undeclared identifier '_ISspace'
static const mask space = _ISspace;
^
Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:402:32: error: use of undeclared identifier '_ISprint'
static const mask print = _ISprint;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:403:32: error: use of undeclared identifier '_IScntrl'
static const mask cntrl = _IScntrl;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:404:32: error: use of undeclared identifier '_ISupper'
static const mask upper = _ISupper;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:405:32: error: use of undeclared identifier '_ISlower'
static const mask lower = _ISlower;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:406:32: error: use of undeclared identifier '_ISalpha'
static const mask alpha = _ISalpha;
can you please help me to resolve this __locale issue while compiling the c++ code.
#include <iostream>
#include <thread>
using namespace std;
void fun(void)
{
cout << "Vaule " << 10 << endl;
}
int main()
{
thread t1(fun);
thread t2(fun);
return 0;
}
compiling command:
g++ -std=c++11 -Wall -g thread.cpp -o thread.out
Two things fix the problem you're having.
Thing the first, add the compiler option -pthread. My compile command: clang++ -Wall -Wextra -std=c++11 -pthread main.cpp
Thing the second, join your threads before ending the program.
t1.join();
t2.join();
Something it does not fix, is that your std::cout statement will likely be jumbled because the threads simply dump their data into the single stream as they please. For an example, my output was the following:
Vaule Vaule 10
10
In order to fix that, you'll likely need to place a lock(mutex) around the std::cout statement.
As I said in my comment, I do not recommend using g++ unless you installed it yourself. The command you're using is an alias that doesn't behave, because of some text you left out.
❯ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Contrast with clang++
❯ clang++ --version
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Of note in the g++ section is the 'Configured with' line. It uses a Standard Library from gcc 4.2.1, which is pre-C++11. You should not have left that information out.

Using unordered_map on my g++ (5.1.0) compiler in command prompt shows error

I have recently downloaded MinGW into my computer but on using certain containers and iterators like unordered_map and auto it shows an unexpected error.
my code is as follows :
#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;
int main()
{
unordered_map<string, int> umap;
umap["GeeksforGeeks"] = 10;
umap["Practice"] = 20;
umap["Contribute"] = 30;
for (auto x : umap)
cout << x.first << " " << x.second << endl;
return 0;
}
it gives the following error :
C:\Users\naima\Documents\cpp>g++ -o try2 try2.cpp
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/unordered_map:35:0,
from try2.cpp:2:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
try2.cpp: In function 'int main()':
try2.cpp:9:5: error: 'unordered_map' was not declared in this scope
unordered_map<string, int> umap;
^
try2.cpp:9:25: error: expected primary-expression before ',' token
unordered_map<string, int> umap;
^
try2.cpp:9:27: error: expected primary-expression before 'int'
unordered_map<string, int> umap;
^
try2.cpp:11:5: error: 'umap' was not declared in this scope
umap["GeeksforGeeks"] = 10;
^
try2.cpp:15:15: error: 'x' does not name a type
for (auto x : umap)
^
try2.cpp:19:5: error: expected ';' before 'return'
return 0;
^
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ';' before 'return'
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ')' before 'return'
The compiler told you exactly what was wrong. It usually will.
This file requires compiler and library support for the ISO C++ 2011 standard. This
support is currently experimental, and must be enabled with the -std=c++11 or
-std=gnu++11 compiler options.
You just have to compile with the proper flag, -std=c++11. I don't know if you are version-matching against what graders use or what, but there are very few good reasons to be on a minGW compiler where support for an 8 year old standard is still considered experimental.
You can see that it works as expected here: https://godbolt.org/z/JQxL00
If you remove the -std=c++11 flag, it will fail to compile and give you the same error message.
You might also notice that I altered the includes to only include what I use. This results in a much faster compile time, smaller executable, and an easier to understand piece of code (Since it is plain to see what standard features are being used). You also avoid polluting your namespace.

Unable to setup gtkmm

I am trying to setup gtkmm on my Windows 7 system. I have installed Glade 3.6.1 and gtkmm Development Environment 2.22.0-2.
When I tried to run the sample code below, I am unable to compile and run.
#include <gtkmm.h>
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Main::run(window);
return 0;
}
The compile log:
Compiler: Default compiler Building Makefile:
"C:\Dev-Cpp\Makefile.win" Executing make... make.exe -f
"C:\Dev-Cpp\Makefile.win" main.o g++.exe -c main.cpp -o main.o
-I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -mms-bitfields -IC:/GTK/include/gtkmm-2.4 -IC:/GTK/lib/gtkmm-2.4/include -IC:/GTK/include/atkmm-1.6 -IC:/GTK/include/giomm-2.4 -IC:/GTK/lib/giomm-2.4/include -IC:/GTK/include/pangomm-1.4 -IC:/GTK/lib/pangomm-1.4/include -IC:/GTK/include/gtk-2.0 -IC:/GTK/include/gdkmm-2.4 -IC:/GTK/lib/gdkmm-2.4/include -IC:/GTK/include/atk-1.0 -IC:/GTK/include/glibmm-2.4 -IC:/GTK/lib/glibmm-2.4/include -IC:/GTK/include/glib-2.0 -IC:/GTK/lib/glib-2.0/include -IC:/GTK/include/sigc++-2.0 -IC:/GTK/lib/sigc++-2.0/include -IC:/GTK/include/cairomm-1.0 -IC:/GTK/lib/cairomm-1.0/include -IC:/GTK/include/pango-1.0 -IC:/GTK/include/cairo -IC:/GTK/include -IC:/GTK/include/freetype2 -IC:/GTK/include/libpng14 -IC:/GTK/lib/gtk-2.0/include -IC:/GTK/include/gdk-pixbuf-2.0 -IC:/GTK/include/libglademm-2.4
In file included from
C:/GTK/include/glibmm-2.4/glibmm/containerhandle_shared.h:24,
from C:/GTK/include/glibmm-2.4/glibmm/arrayhandle.h:23,
from C:/GTK/include/glibmm-2.4/glibmm.h:82,
from C:/GTK/include/gtkmm-2.4/gtkmm.h:87,
from main.cpp:1: C:/GTK/include/glibmm-2.4/glibmm/ustring.h:861: error: ISO C++ forbids
declaration of wostringstream' with no type
C:/GTK/include/glibmm-2.4/glibmm/ustring.h:861: error: expected;'
before "StreamType" C:/GTK/include/glibmm-2.4/glibmm/ustring.h:865:
error: `StreamType' does not name a type
C:/GTK/include/glibmm-2.4/glibmm/ustring.h:906: error: expected
constructor, destructor, or type conversion before '&' token
C:/GTK/include/glibmm-2.4/glibmm/ustring.h:906: error: expected ,' or
;' before '&' token C:/GTK/include/glibmm-2.4/glibmm/ustring.h:912:
error: expected constructor, destructor, or type conversion before '&'
token C:/GTK/include/glibmm-2.4/glibmm/ustring.h:912: error: expected
,' or;' before '&' token
C:/GTK/include/glibmm-2.4/glibmm/ustring.h: In member function void
Glib::ustring::FormatStream::stream(const T&)':
C:/GTK/include/glibmm-2.4/glibmm/ustring.h:1057: error:stream_'
undeclared (first use this function)
C:/GTK/include/glibmm-2.4/glibmm/ustring.h:1057: error: (Each
undeclared identifier is reported only once for each function it
appears in.)
C:/GTK/include/glibmm-2.4/glibmm/ustring.h: In member function void
Glib::ustring::FormatStream::stream(const char*)':
C:/GTK/include/glibmm-2.4/glibmm/ustring.h:1063: error:stream_'
undeclared (first use this function)
C:/GTK/include/glibmm-2.4/glibmm/ustring.h: In member function void
Glib::ustring::FormatStream::stream(char*)':
C:/GTK/include/glibmm-2.4/glibmm/ustring.h:1069: error:stream_'
undeclared (first use this function)
make.exe: * [main.o] Error 1
Execution terminated
Any help with the setup would be greatly appreciated.
Thanks!