gcc compile, getting "clang: error: unsupported option '-fopenmp'" - gcc

I am running the command
gcc -o openmptest -fopenmp openmptest.c
however am getting the error:
clang: error: unsupported option '-fopenmp'
I am running this on Mac Mojave. In addition I am using VS code which is telling me
cannot open source file "omp.h"
for my include statement.

I think it could work if you install libomp by
brew install libomp
and then,
clang -Xpreprocessor -fopenmp -I/usr/local/include -L/usr/local/lib -lomp openmptest.c -o main
This should work, I was having the same issue and and managed to compile my code by using the above.

Related

omp.h' file not found while compiling source code

I am trying to compile this this source code (https://sites.google.com/site/bgcsoftware/) on a mac. I installed both hdf5 and gsl using homebrew.
Would you know what the problem might be?
Thank you in advance!
h5c++ -Wall -O2 -o bgc bgc_main.C bgc_func_readdata.C bgc_func_initialize.C bgc_func_mcmc.C bgc_func_write.C bgc_func_linkage.C bgc_func_ngs.C bgc_func_hdf5.C mvrandist.c -lgsl -lm
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
bgc_main.C:17:10: fatal error: 'omp.h' file not found
#include <omp.h>
^~~~~~~
1 error generated.
bgc_func_mcmc.C:12:10: fatal error: 'omp.h' file not found
#include <omp.h>
^~~~~~~
1 error generated.
It looks like clang is the actual compiler being used. When compiling OpenMP with clang you need to pass the -fopenmp flag.
Try adding the -fopenmp flag like this:
h5c++ -fopenmp -Wall -O2 -o bgc \
bgc_main.C bgc_func_readdata.C bgc_func_initialize.C \
bgc_func_mcmc.C bgc_func_write.C bgc_func_linkage.C \
bgc_func_ngs.C bgc_func_hdf5.C mvrandist.c -lgsl -lm
The -fopenmp flag tells the compiler replace the code marked with #pragma omp ... with generated parallel code and should automatically add the correct -I include flags behind the scenes.
You should be able to run
h5c++ --help | grep openmp
To see other openmp related flags, depending on your compiler/OS.
adding -fopenmp did not help. However, the original code did run when I installed:
brew install --build-from-source libomp

OpenMP with g++ MacOS on Sierra

I have a makefile with the following that I'm trying to make on macOS Sierra:
CPP = g++
CPPFLAGS = -m64 -fopenmp -O3
and I keep getting the error clang: error: unsupported option '-fopenmp'.
I did
brew install libomp
brew install llvm
brew install gcc
These all were successful but I still get the same error. Would appreciate any suggestions!
There are a number of options...
As you have installed llvm with homebrew, you can compile C++ code using OpenMP with:
/usr/local/Cellar/llvm/9.0.0/bin/clang++ -fopenmp main.cpp -o main
As you have installed GCC with homebrew, you can compile C++ code using OpenMP with:
/usr/local/bin/g++-9 -fopenmp main.cpp -o main
As you have installed libomp, you can use that with the Apple-supplied clang++ (note it is not in /usr/local because it is supplied by Apple) to compile C++ with OpenMP with:
/usr/bin/clang++ -I/usr/local/include -Xpreprocessor -fopenmp -L/usr/local/lib -lomp main.cpp -o main

Static linking library asan with gcc 4.8

I'm compiling my test executable in this way using gcc 4.8.3:
gcc -o test -g -L/lib64 -fsanitize=address -static-libasan
Result:
/usr/bin/ld: cannot find -lasan
collect2: error: ld returned 1 exit status
Libasan is installed and if I compile without the static option it works. How can I compile the library statically?
You need to install special package to get static GCC libraries. On Ubuntu you should do
$ sudo apt-get install libgcc-4.8-dev

Clang boost header not found

I am trying to compile a Python PCL module which builds some C++ source. I am getting this error:
$ python setup.py install
running install
running build
running build_py
running build_ext
skipping 'pcl/_pcl.cpp' Cython extension (up-to-date)
building 'pcl._pcl' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Python/2.7/site-packages/numpy/core/include -I/usr/local/Cellar/pcl/HEAD/include/pcl-1.8 -I/usr/local/Cellar/eigen/3.2.3/include/eigen3 -I/usr/local/Cellar/pcl/HEAD/include/pcl-1.8 -I/usr/local/Cellar/flann/1.8.4/include -I/usr/local/Cellar/pcl/HEAD/include/pcl-1.8 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c pcl/_pcl.cpp -o build/temp.macosx-10.6-intel-2.7/pcl/_pcl.o
pcl/_pcl.cpp:244:10: fatal error: 'boost/smart_ptr/shared_ptr.hpp' file not
found
#include "boost/smart_ptr/shared_ptr.hpp"
^
1 error generated.
error: command '/usr/bin/clang'
For whatever reason clang isn't looking in /usr/local/include where it most definitely would find the boost headers. As you can see it is linking all the other dependencies fine. What can I add that so clang will find boost?
On OSX 10.10, nothing fancy happening anywhere. Boost was probably installed by homebrew, but the files are all in /usr/local/include/boost as I'd expect.
Check the output of /usr/bin/clang++ -v some_test_file.cpp but chances are /usr/local/include isn't in the standard search path of the compiler.
You'll need to add -I/usr/local/include to CXXFLAGS or CPPFLAGS or whatever appropriate place in your build script or environment. It seems the python build script is failing to properly detect Boost.
If the above is not true (and /usr/local/include does show up in the output), make sure /usr/local/include/boost/smart_ptr/shared_ptr.hpp exists.

RcppArmadillo Compile Errors on OS X Mavericks

This is a follow-up to the question at Element-Wise Matrix Multiplication in Rcpp
I have been getting a number of different kinds of errors with RcppArmadillo since upgrading to Mavericks. I have Xcode 5.0.2 and Command Line Tools installed. Also, gfortran from Homebrew. But I keep encountering the error below --
> cppFunction("arma::mat schur(arma::mat& a, arma::mat& b)
{ return(a % b); }", depends="RcppArmadillo")
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_18474.so] Error 1
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include - I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include" -fPIC "-mtune=native -g -O2 -Wall -pedantic -Wconversion" -c fileaf992bfb8f84.cpp -o fileaf992bfb8f84.o clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -L/usr/local/lib -o sourceCpp_18474.so fileaf992bfb8f84.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -lgfortran /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/lib/libRcpp.a -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, :
Error 1 occurred building shared library.
WARNING: The tools required to build C++ code for R were not found.
Please install Command Line Tools for XCode (or equivalent).
# Contents of Makevars
$ cat ~/.R/Makevars
CC=clang
CXX=clang++
CXXFLAGS="-mtune=native -g -O2 -Wall -pedantic -Wconversion"
FLIBS=-lgfortran
Commenting FLIBS=-lgfortran does not help and results in even more error messages --
> cppFunction("arma::mat schur(arma::mat& a, arma::mat& b) { return(a % b); }", depends="RcppArmadillo")
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_50381.so] Error 1
Thanks in advance.
Update
Following suggestions from response from Kevin and Dirk below, I re-installed Rcpp, RcppArmadillo and inline from source and updated FLIBS to point to the actual directory. This solved the issue.
# Update FLIBS in ~/.R/Makevars
FLIBS=-L/usr/local/Cellar/gfortran/4.8.2/gfortran
#Re-Install from source
install.packages(c("Rcpp","RcppArmadillo","inline"),type="source")
#Restart R
EDIT: If you're a Homebrew user, you now instead need to use brew install gcc (gfortran is no longer provided separate of gcc), and you can then follow the instructions here to get set up.
You have to symlink the libraries to /usr/local/lib manually:
ln -s /usr/local/Cellar/gfortran/4.8.2/gfortran/lib/libgfortran.* /usr/local/lib/
I thought brew link gfortran would handle this, but apparently it only symlinks the gfortran program and not the actual libraries. So, unfortunately, you have to do it yourself.
(Replace 4.8.2 with whichever version of gfortran you're using from homebrew.)
Alternatively, if you want to keep from modifying /usr/local/lib, you can use
FLIBS=-L/usr/local/Cellar/gfortran/4.8.2/gfortran
in your ~/.R/Makevars file instead, so R knows where to find the gfortran libraries.
I can only suggest you study the numerous threads on the r-sig-mac list, the different answers here on SO as well as the posts on the rcpp-devel list.
As your error comes from the failed Fortran linking, maybe also review the standard page by Simon U. as well as the tools page it points too. AFAIK you should use the (older) gfortran 4.2.* from that page with R -- but then I am not an OS X user.
Edit in late 2016: We now have more detailed instructions in section 2.16 of the Rcpp FAQ.

Resources