Trying to compile a C++ code with Root (Cern) parameters included - c++

I working on trying to compile a code written in C++ and Root on my Mac. The code is from a colleague who works on a Linux laptop. In addition to the different OS's, I have both a different version of gcc and Root than her and I am not sure which difference is causing the code not to compile on my machine.
She has g++ 4.8 and root 5.(something). I have gcc 5.3.0 and root 6.06/02.
She has given to me the a line of code she uses to get her machine to compile the code
gcc -Wall -o executable_name code_name.cc `root-config --cflags --glibs`
But when I write on my machine, Terminal gives me the error
gcc: error: unrecognized command line option ‘-stdlib=libc++’
gcc: error: unrecognized command line option ‘-stdlib=libc++’
I need help generating the correct line to get gcc to compile the code.

The problem here is two-fold: You're on OSX and you are using GCC.
The problem with this is that root-config assumes that since you're on OSX you will be using the OSX-standard Clang compiler which have the -stdlib flag. Since you're not using Clang, but GCC (which doesn't have this flag) you get an error.
You have two possible solutions: Use clang++ instead of g++ to compile and build (requires you to install the compiler if it's not installed already, it comes with Xcode), or to modify the root-config script so it doesn't add -stdlib=libc++. There might be environment variables or flags that the root-config script checks that alter the behavior, but I don't know anything about the script, you have to check it for that.

Related

Clang compiler couldn't find C/C++ standard libraries when I gave a specific target although it works without giving target

I am currently using clang11 on ubuntu to compile any c/c++ code and it works fine but when I tried to compile any code (including any standard library) to assembly code for any specific target like x86_64 (even I have x86_64) riscv with giving a flag that --target=x86_64 or --target=riscv32 I got errors for any standard library that I included in my code. A simple example:
// ex.cpp
#include<iostream>
int main(){
int a = 5;
int b = 3;
std::cout << a - b;
}
Without giving flag for a spesific target works fine:
clang++-11 -S ex.cpp -o ex.s
With --target=riscv32 flag:
clang++-11 --target=riscv32 -S ex.cpp -o ex.s
gives this error:
ex.cpp:1:9: fatal error: 'iostream' file not found
also without standard libraries gives no error even I give a spesific target.
I am searching for a solution for days but I couldn't find any proper solution for this problem, most of them says try to include gnu libraries and subfolders like -I/usr/include/x86_64-linux-gnu/c++/ but it doesn't work for me.
Please don't say use g++ compiler, for adding an optimization I need clang.
Actually I am trying to compile my codes for riscv target, linking with g++ and running with spike (doesn't differ --target=... or -target ...):
clang++-11 -target riscv32-unknown-elf -march=rv32gc -fno-addrsig -S ex.cpp -o ex.s
~/riscv/bin/riscv32-unknown-elf-g++ ex.s -o ex
~/riscv/riscv-isa-sim/build/spike --isa=RV32GC ~/riscv/riscv-pk/build/pk ex
And it works fine without include a standard library.
Now, I want to ask that
Can I solve this problem simply?
or
Can I use clang directly from riscv bin utils like ~/riscv/bin/riscv32-unknown-elf-clang++ (I saw something like this on the net but couldn't find) adding and building a submodule to my riscv directory?
Edit: As #NateEldredge said, for x86_64 target triple should --target=x86_64-linux-gnu but for riscv as a target triple riscv32-unknown-elf I still have the same errors. Is there a proper target flag for riscv any other than --target=riscv32-unknown-elf? Maybe I am missing that point.
I solved my problem by linking compilations with riscv-gnu-toolchain built and also answered a similar question here in detailed: Using Clang to compile for RISC-V
Simply we need cross-compilation.
Further information you can also look here: https://github.com/lowRISC/riscv-llvm#how-can-i-build-upstream-llvmclang-and-use-it-to-cross-compile-for-a-riscv32-target

Cannot find -lubsan on using -fsanitize=undefined (mingw-w64)

I'm using mingw-w64 (gcc 7.3.0) and when I compile any C++ program using the following command:
g++ file.cpp -fsanitize=undefined
I get the following error:
...mingw32/bin/ld.exe: cannot find -lubsan
I'm able to successfully compile and run my programs if I remove the -fsanitize=undefined flag, though. After some research I found out that this means the library ubsan (Undefined Behavior Sanitizer) is missing, but I couldn't find anything about the library. How do I fix this?
This is well known issue with mingw see for instance this msys github issue. No proper solution known but there are several WAs.
Install WSL, ubuntu over WSL and you will have ubsan inside it
Build GCC under Windows from source enabling sanitizers build. They are present in GCC sources they are just not here in mingw.
Use -fsanitize=undefined -fsanitize-undefined-trap-on-error to just not use libubsan rich logging capabilities but get it trap on undefined instruction.
Hope one of this helps.

Error: ‘to_string’ is not a member of ‘std’ when compiling in command line with gcc 4.8.2, works in NetBeans?

I am doing tests for my code with GoogleTest (1.7.0). I must have the tests compiling in both NetBeans (8.0.2) and straight from command line.
NetBeans has GoogleTest integrated as suggested here: https://youtu.be/TS2CTf11k1U.
I have also build GoogleTest from the command line (in different place) with the instructions provided with the package.
The problematic line of code is (a and b are doubles):
std::string input = std::to_string(a) + " " + std::to_string(b);
which gives error: ‘to_string’ is not a member of ‘std’, when compiling with
g++ -isystem -std=c++0x ~/gtest-1.7.0/include -pthread ~/Task1/test_source.cpp libgtest.a -o test_module1_1
as instructed in GoogleTest documentation.
The strange thing here is that when using the "Test" from NetBeans (as seen on video), the tests compile and run correctly.
I am using Ubuntu 14.04 and I have already tried to update g++ (not to a higher version though), and I have also tried specifying the version with -std=c++11 instead of -std=c++0x.
In NetBeans there is this warning visible:
Library File /usr/include/c++/4.8/string
but there is an unresolved #include <stddef.h>
in included /usr/include/_G_config.h
I found out that this might be because multilib and I have also tried to fix that by checking that it is installed. And this doesn't really cause any warnings during compile so it might be just that the IDE is confused.
But conserning the real problem about compile errors:
Is there something wrong with the GoogleTest framework, the compiler or my code?
PS. Preferably don't suggest that I just don't use to_string as this system must be usable by students studying C++11 and really it shouldn't be that they can't use all the functionality that should be supported.
I think your problem is the order in which you're passing command line arguments to gcc
g++ -isystem -std=c++0x ~/gtest-1.7.0/include ...
^^^^^^^^^^^^^^^^^^^
The -isystem flag should be followed by a path that contains headers which you want gcc to treat as system headers. So the -std=c++0x flag is being incorrectly consumed by the -isystem flag. You probably want the command line to be
g++ -std=c++0x -isystem ~/gtest-1.7.0/include ...

why self built g++ compiler fails to compile my code

I wanted to use latest g++ compiler(4.9.1) on suse linux, but suse only supports an older g++ version. So, I took a latest source code from one of the gnu mirror sites and compiled it myself. Everything went fine. But when I tried to compile my test code using the built g++, the compilation fails with error,
"/root/home/include/c++/4.9.1/x86_64-unknown-linux-gnu/bits/os_defines.h:39:22: fatal error: features.h: No such file or directory".
I can find a "features.h" in "/root/home/include/c++/4.9.1/parallel", but I feel that it should be there in "/root/home/include/c++/4.9.1/" itself.
I copied "/root/home/include/c++/4.9.1/parallel/features.h" to "/root/home/include/c++/4.9.1/" just to see what happens. Now it complains with error "whcar.h" not found.
Have I missed something.
Here are the steps I followed to build g++.
1. /root/home/gcc_build/objdir# ../gcc-4.9.1/configure --prefix=/root/home/ --disable-multilib
2. /root/home/gcc_build/objdir# make -j16
3. /root/home/gcc_build/objdir# make install
4. /root/home/gcc_build/test# /root/home/bin/g++ --sysroot /root/home -m64 test.cpp
I resolved the issue by removing sysroot option and pointing c++ include and library path to my home directory. One thing I noticed was that the g++ source does not come with libc and c header files, and libc has to be installed seperately. But with sysroot option, g++ was trying to look all the header files in my home directory.
Below is the command I used to successfully compile the code.
/root/home/bin/g++ -I /root/home/include/c++/4.9.1 -L /root/home/lib64 -Wl,--rpath=/root/home/lib64 --std=c++0x -m64 test.cpp
Take a look at the GCC Directory Options. It is important to use the correct "specifier" as well (-isystem, -L, -B, -I etc)

Cannot compile using -std flag with g++

I'm running Windows 7 with cygwin installed and trying to have a play around with some of the newer C++ features. I'm aware that in order to enable these features I have to pass g++ the -std=c++0x flag, however that gives me the following error:
cc1plus: error: unrecognized command line option "-std=c++0x"
The command line I'm issueing that gives rise to that error is:
g++-3 hello.cpp -std=c++0x -o hello
The reason for the g++-3 is because windows has trouble with the symbolic link. I've used g++ in the cygwin terminal and the result is the same anyway.
Any ideas?
You need to be using a version of GCC which supports C++ 2011 features.
This page has a list of compilers and which features each one supports. If I were you, I'd try to use GCC 4.7 if at all possible.