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.
asked Jul 4, 2016 at 9:20
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.
answered Jul 4, 2016 at 9:28
I use CMake to compile my ROOT-based projects.
If you have a project directory proj/ and it contains src/ and bin/, you’ll need 3 CMakeList.txt files, one in each directory.
A simple example CMakeList.txt in the main project directory:
cmake_minimum_required(VERSION 2.6)
project (SOME_PROJ_NAME)
add_subdirectory(src)
add_subdirectory(bin)
src/ directory is where you keep your .h and .cxx proj. library files. Example CMakeList.txt file:
# get all the *.cxx filenames, to compile them into a lib
file(GLOB SOME_PROJ_LIB_SRCS "${PROJECT_SOURCE_DIR}/src/*.cxx")
# include ROOT library and include files
include_directories(/path/to/root/dir/include/dir)
link_directories(/path/to/root/dir/lib/dir)
# and compile src into a library
add_library(Proj_lib_name ${SOME_PROJ_LIB_SRCS})
# here, list the ROOT libraries you require
target_link_libraries(Proj_lib_name dl Core Cint RIO Net Hist Graf Graf3d Gpad Tree Rint Postscript Matrix Physics MathCore Thread Gui pthread m)
bin/ directory is where you keep your app .cxx files and it has a CMakeList.txt file:
include_directories(${PROJECT_SOURCE_DIR}/src)
link_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(/path/to/root/dir/include/dir)
link_directories(/path/to/root/dir/lib/dir)
add_executable(example_app.exe example_app.cxx)
target_link_libraries(example_app.exe Proj_lib_name dl Core Cint RIO Net Hist Graf Graf3d Gpad Tree Rint Postscript Matrix Physics MathCore Thread Gui pthread m)
Finally, to compile ROOT-based code with CMake, out of source, you create a «build» dir in your top level project dir, so that your dir structure looks like this:
proj/
bin/
build/
src/
cd build
cmake ..
Your binaries will be located in build/bin/ directory
Hope this helps.
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
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.
At first after installing ROOT go to your ROOT installation directory and run ‘sh /bin/thisroot.sh’
Make sure your environment variables are set. In MacOS they are declared in ~/.bash_profile file. For example, I have:
export ROOTSYS=/Users/petrstepanov/root-6.04.08 export PATH=$ROOTSYS/bin:$PATH export LD_LIBRARY_PATH=$ROOTSYS/lib:$LD_LIBRARY_PATH export DYLD_LIBRARY_PATH=$ROOTSYS/lib:$DYLD_LIBRARY_PATH
after editing your ‘.bash_profile’ run from terminal ‘source .bash_profile’
g++ `root-config --cflags --libs` -L $ROOTSYS/lib -lRooFit -lRooFitCore -lHtml -lMinuit -lFumili your-program-name.cpp -o your-program-name.o
./your-program-name.o
1. Go to Project -> Properties -> C/C++ Build -> Environment. You should have ROOTSYS, LD_LIBRARY_PATH and DYLD_LIBRARY_PATH set there.
2. Go to Project -> Properties -> C/C++ Build -> Settings -> MacOS X C++ Linker -> Miscellaneous. On the right notice Miscellaneous text field. Paste there identical flags/parameters that worked for you when you compiled from console. I have all other settings under “MacOS X C++ Linker” default/empty.
3. Go to Project -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous. Paste same string there. Since this string has all necessary flags for the compiler I cleared default eclipse settings under “GCC C++ Compiler”. E.G. no include path, no language standart, none optimization.
That’s pretty much it. Below please see the some Eclipse errors that showed up in the console. I briefly state the solutions there
- Eclipse Output: atomic is not implemented
Solution you have to use C++11 compiler. This is achieved by adding following parameters -stdlib=libc++ -std=c++11 to the compiler directives. - Eclipse Output: symbol(s) not found for architecture x86_64
Solution: you probably forgot to include one of the libraries (g++ parameter). Doublecheck following flags: -L/path-to-root-libraries/ – make sure path is correct. Also try including include more libraries. The ones that I was missing were -lRooFit -lRooFitCore -lFumili. After adding these flags problem gone. - Eclipse Output: unknown type name ‘…’ (in my case – ‘RooAddPdf’ and ‘RooGausian’)
Solution: cyclic dependency in roofit #include “…” files. Remove all include lines in your code and start adding them as-demanded by compiler again. - Eclipse Output: can’t link with a main executable file for architecture x86_64
Solution: add “-c” to compiler flags – eclipse will not try to link your source file
http://stackoverflow.com/questions/6309773/link-error-on-mac-osx-10-6-7
P.S. You might need a list of g++ compiler flags:
https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Option-Summary.html#Option%20Summary