C++11 CMake: Regex fails - c++

I ran into the problem that gcc/g++ <= 4.8.X does not support RegEx (my first reaction was: WHAT?!).
After installing (Ubuntu 14.04 LTS) gcc-4.9 and g++-4.9 (which is supposed to support RegEx properly) I still the get same error:
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
[1] 13608 abort (core dumped)
My CMakeLists.txt looks like this (working with Jetbrains CLion as IDE):
set(CMAKE_CXX_COMPILER g++-4.9)
cmake_minimum_required(VERSION 3.1)
project(project1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
My code looks like this:
#include <iostream>
#include <string>
#include <fstream>
#include <regex>
using namespace std;
(...)
char encryptChar(char cinput)
{
std::string s = std::string(1, cinput);
// simplified regex (also crashes)
std::regex e = std::regex("[a-z]");
if(std::regex_match(s, e))
{
// do some stuff, if string matches conditions
}
return cinput;
}
Compiler/Linker do not complain about anything. Program runs fine without the regex-lines.
> g++-4.9 --version
>>> g++-4.9 (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
>>> Copyright (C) 2014 Free Software Foundation, Inc.
EDIT: After manually compiling the code with g++-4.9 -std=c++11 main.cpp the regex works. Why does the IDE/CMake version fails?

Finally, I found the problem:
My CMake version was 2.8-ish so CMake itself failed, Jetbrains CLion uses a custom CMake (shipped with the IDE) which is some 3.1-ish but also failed with RegEx.
I downloaded CMake 3.2.2 (newest version) and installed it (installation notes). Now compiling with CMake uses g++-4.9 properly and RegEx runs fine. In CLion I had to change the settings to ignore the custom CMake and use my systems CMake 3.2.2, now compiling with the IDE uses g++-4.9 properly, too and RegEx runs fine.

Related

How to configure g++ as the default compiler in Mac OS (M1)

So, I wanted to use some header files native to GNU C++:
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
I read that in MacOS, gcc and g++ are both linked to clang. So, we had to install the gcc using homebrew and use that.
But after installing gcc using homebrew. When I run
g++ --version
I get
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
But running g++-12 --version I get:
g++-12 (Homebrew GCC 12.2.0) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
My VSCode runs g++ (Apple One) to compile C/C++ files. For the goal I wanted to accomplish at the start, I read that we need to have the g++ (installed using homebrew) do the compiling.
So, I ran the following commands:
cd /opt/homebrew/bin
ls -s g++-12 g++
But now, even when I compile the following code:
#include <iostream>
int main()
{
std::cout << 1;
}
I get the following error:
In file included from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/bits/postypes.h:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iosfwd:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ios:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ostream:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iostream:39,
from test.cpp:1:
/opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/cwchar:44:10: fatal error: wchar.h: No such file or directory
44 | #include <wchar.h>
| ^~~~~~~~~
compilation terminated.
Now, removing the link using rm g++ reverts back to my original configuration. But that configuration can't run the headers I requested at the start. Is there any way to solve this?
Most of the above have mostly been taken from here. But I didn't find any solution. But, I have seen people using the same and getting successful.
EDIT:
I found a website where there was a solution. It was to compile not using g++. Rather use
g++-12 --sysroot=$(xcrun --show-sdk-path)
When I used this, it solved the problem. Can someone explain why this happened?
The header error likely indicates the g++ you installed via Homebrew may not be compatible with the Apple Xcode version installed in the macos system directories.
The solution is probably to reinstall one or both packages.
EDIT:
g++-12 --sysroot=$(xcrun --show-sdk-path) changes the search path for system header includes from the default (which was probably set when homebrew installed g++) to the one provided by the Xcode SDK currently installed.

coc-clangd has issues with gtkmm-4.0

I have the hello world program from the gtkmm documentation. I can compile the program without any issues using:
g++ -o example main.cc example.cc `pkg-config "gtkmm-4.0" --cflags --libs`
But the moment I go into neovim, coc-clangd has an error:
In included file: no template named 'is_base_of_v' in namespace 'std'; did you mean 'is_base_of'?
this happens at #include "gtkmm/application.h"
:CocInfo
vim version: NVIM v0.5.0
node version: v14.15.5
coc.nvim version: 0.0.80-37132cfc36
coc.nvim directory: /home/blah/.config/nvim/plugged/coc.nvim
term: tmux
platform: linux
clangd is 12.0.0
PS: I do have a compile_commands.json file, generated using intercept-build utility
I don't know if you have solved it now, I encountered the same problem as you. I thought it was a problem with clangd, but I checked the is_base_of_v and found that it only existed after C++17. If you use cmake to generate your project, just add:
set_property(TARGET ${target_name} PROPERTY CXX_STANDARD 17)
to your CMakeLists.txt to set the C++ standard to C++17.

OpenMP support for Mac using clang or gcc

I am using Mac OS and I created a toy CMake project with OpenMP.
main.cpp:
#include <iostream>
#include "omp.h"
using namespace std;
int main() {
cout << "Hello, World!" << endl;
#pragma omp parallel
cout << "Hello OpenMP from thread " << omp_get_thread_num() << endl;
return 0;
}
CMakeList.txt:
cmake_minimum_required(VERSION 3.12)
project(omp_test)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
add_executable(omp_test main.cpp)
When I tried mkdir build && cd build && cmake .. && make -j to build the project, I got clang: error: unsupported option '-fopenmp'. It works well on Ubuntu. How can I change the CMakeList.txt to enable OpenMP support? Or If the problem can be solved using g++, how to do it?
Thanks for your help!
I have solved my problem by changing the default compiler to g++. (Note: I have already installed gcc/g++ and the OpenMP library from Homebrew.)
I did not change main.cpp and CMakeList.txt, but when I built the project, I use
mkdir build && cd build && cmake -DCMAKE_CXX_COMPILER=g++-9 .. && make -j
then the error clang: error: unsupported option '-fopenmp' disappeared. Specifically, I added the option -DCMAKE_CXX_COMPILER=g++-9 to change the default compiler. Maybe the g++ version on your computer is not g++-9, then you can check it under the path /user/local/bin/.

wxWidgets runtime error (Mismatch version)

I have a problem at start the program:
Fatal Error: Mismatch between the program and library build versions detected.
The library used 3.0 (wchar_t,compiler with C++ ABI 1010,wx containers,compatible with 2.8),
and your program used 3.0 (wchar_t,compiler with C++ ABI 1009,wx containers,compatible with 2.8).
My cmake settings:
cmake_minimum_required(VERSION 3.0)
project(simple)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${wxWidgets_CXX_FLAGS} -Wall -std=c++14")
find_package(wxWidgets COMPONENTS net gl core base)
include("${wxWidgets_USE_FILE}")
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})
Version of wxWidgets 3.0.3.
If your desire is to have __GXX_ABI_VERSION=1002, specify -fabi-version=2 to GCC. To do this in your CMakeLists.txt, add:
add_definitions(-fabi-version=2)
This is a preferred approach compared to manually redefining __GXX_ABI_VERSION, which would violate C++ standards and potentially cause undefined behavior.
Note: -fabi-version=2 may not always correspond to __GXX_ABI_VERSION=1002 in future releases of GCC. Compile and run this quick C++ program to check it:
#include <iostream>
int main(void) {
std::cout << "__GXX_ABI_VERSION=" << __GXX_ABI_VERSION << std::endl;
return 0;
}
Compile this way:
g++ -fabi-version=2 -o check_fabi_version check_fabi_version.cpp
Run this way:
./check_fabi_version
Example output as of GCC 8.2.0:
__GXX_ABI_VERSION=1002
You can try to add to your program
#define __GXX_ABI_VERSION 1010
or just
sudo apt-get purge wx2.8-headers wx2.9-headers
I had two version of wxWidgets instaled. I deleted one of them and it works great.

CMake 3.x + CUDA - compilation busted

I've written the following groundbreaking GPU-powered application:
int main() { return 0; }
and I'm trying to build it using CMake. Here's my CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} " -std=c++11" )
find_package(CUDA QUIET REQUIRED)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} " -std=c++11")
cuda_add_executable(a a.cu)
With this file, and for some reason, the compilation fails; it looks like (although I'm not sure) the reason is that CMake is having NVCC use the CUDA include directory twice. The result:
/home/joeuser/opt/cuda/bin/nvcc -M -D__CUDACC__ /home/joeuser/src/kt3/a.cu -o /home/joeuser/src/kt3/CMakeFiles/a.dir//a_generated_a.cu.o.NVCC-depend -ccbin /usr/bin/ccache -m64 --std c++11 -Xcompiler ,\"-g\" -std=c++11 -DNVCC -I/home/joeuser/opt/cuda/include -I/home/joeuser/opt/cuda/include
nvcc fatal : redefinition of argument 'std'
CMake Error at a_generated_a.cu.o.cmake:207 (message):
Error generating
/home/joeuser/src/kt3/CMakeFiles/a.dir//./a_generated_a.cu.o
You might be asking why my MWE is not more terse. Why do I need those option-setting lines above if I don't use C++11 anyway? Well, if I remove them, I still get the double-include, but have an earlier failure involving ccache about which I will probably ask another question.
So is the problem actually the double-include, or is it something else? And what should I do?
Additional information:
I don't have root on this machine.
CMake version: 3.3.2 .
Distribution: Fedora 22 (sorry, I can't help it; but I also seem to be getting this on Debian Stretch as well).
CUDA install location: $HOME/opt/cuda , and its binary directory is in $PATH.
On another system, with a different configuration and distro (Fedora 20, CUDA 7.5 but in another local dir, possibly other differences) I do not get this behavior.
I think the problem is that the nvcc flags are propagated to your c/c++ compiler, so some compiler arguments are effectively passed twice. Try using SET(CUDA_PROPAGATE_HOST_FLAGS OFF)