CMake, Clang and OpenMP on MacOS Big Sur - macos

I'm having trouble compiling a project requiring OpenMP using CMake and clang, I set my CMakeLists.txt like this
cmake_minimum_required(VERSION 3.20)
project(cmat C)
set(CMAKE_C_STANDARD 11)
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
add_library(cmat SHARED cmat.c Calculation/_Basic_Calculate_.c Calculation/_Basic_Calculate_.h)
And when I started to build my project, it gave error
Undefined symbols for architecture x86_64:
"_omp_get_thread_num", referenced from:
_NmMulMat in _Basic_Calculate_.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [libcmat.dylib] Error 1
make[2]: *** [CMakeFiles/cmat.dir/all] Error 2
make[1]: *** [CMakeFiles/cmat.dir/rule] Error 2
make: *** [cmat] Error 2
I've tried some of the other answers like this and this, but they just don't work for me.
The clang version for my Mac is 13.0.0 and CMake is of 3.21.3_1, I'm using Clion as IDE, and such command will work when compiling files by shell
clang -Xpreprocessor -fopenmp -I/usr/local/include -L/usr/local/lib -lomp filename.c -o output
Great appreciate for your help.

You need to add -fopenmp to the linker flags as well by providing -DCMAKE_EXE_LINKER_FLAGS=-fopenmp [and any other options like -g].
In CLion (which I don't use) this would be specified in Preferences ->
Build, Execution, Deployment -> CMake -> CMake options as far as I understand the documentation.
Also please note that explicitly linking with -lomp is probably the wrong strategy, that happening should be a side-effect of specifying -fopenmp.

Related

ld linking problem on Mac M1 arm64 architecture, using GNU11 compilers gcc, gfortran

I'm building a set of libraries on Mac M1 (arm64 architecture), Big Sur and use GNU11 compilers (gcc, gfortran, g++), as well as openmpi 4.1.2 for multi-processing. All is done as a command-line using a Terminal.app, which is running as Apple native, and NOT using Rosetta Translation. The steps taken: 1) GCC, g++, gfortran are installed using "brew install gcc#11", then symbolic links created in /opt/homebrew/bin/ for gcc -> gcc-11, g++ -> g++-11 ; 2) openmpi 4.1.2 installed, with mpicc based on gcc, mpixx based on g++, and mpifort/mpif90 based on gfortran; 3) command-line tools installed as "xcode-select --install".
Getting the problem with finally linking of the libraries:
...
/Users/Natalie/openmpi/bin/mpifort -L/opt/homebrew/opt/openssl#3/lib -fallow-argument-mismatch -fallow-invalid-boz -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -mmacosx-version-min=11.6 CMakeFiles/cmTC_9a888.dir/testFortranCompiler.f.o -o cmTC_9a888
Undefined symbols for architecture arm64:
"_sgemm_", referenced from:
_MAIN__ in testFortranCompiler.f.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
make[1]: *** [cmTC_9a888] Error 1
make: *** [cmTC_9a888/fast] Error 2 ```

Building Boost 1.57.0 with GCC 4.0: ld: can't map file, errno=22

I'm trying to build boost 1.57.0 with gcc 4.0 on mac. I first found this website, but I got a number of linker errors when I tried that. I then found this question which allowed me to fix those linker errors, but I'm still getting more that I can't solve. Here's a snippet of the boost build output that demonstrates the problem.
...failed gcc.compile.c++ bin.v2/libs/context/build/gcc-4.0.1/release/threading-multi/unsupported.o...
...skipped <p/boost_1_57_0/lib>libboost_context.dylib for lack of <pbin.v2/libs/context/build/gcc-4.0.1/release/threading-multi>unsupported.o...
gcc.link.dll /boost_1_57_0/lib/libboost_thread.dylib
ld: can't map file, errno=22 file '/System/Library/Frameworks/Python.framework/Versions/2.7/lib' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
"g++" -Wl,"/System/Library/Frameworks/Python.framework/Versions/2.7/lib" -Wl,"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config" -o "/boost_1_57_0/lib/libboost_thread.dylib" -shared "bin.v2/libs/thread/build/gcc-4.0.1/release/threading-multi/pthread/thread.o" "bin.v2/libs/thread/build/gcc-4.0.1/release/threading-multi/pthread/once.o" "bin.v2/libs/thread/build/gcc-4.0.1/release/threading-multi/future.o" "bin.v2/libs/system/build/gcc-4.0.1/release/threading-multi/libboost_system.dylib" "bin.v2/libs/atomic/build/gcc-4.0.1/release/threading-multi/libboost_atomic.dylib"
...failed gcc.link.dll /boost_1_57_0/lib/libboost_thread.dylib...
...skipped <pbin.v2/libs/context/build/gcc-4.0.1/release/threading-multi>libboost_context.dylib for lack of <pbin.v2/libs/context/build/gcc-4.0.1/release/threading-multi>unsupported.o...
...skipped <p/boost_1_57_0/lib>libboost_coroutine.dylib for lack of <pbin.v2/libs/context/build/gcc-4.0.1/release/threading-multi>libboost_context.dylib...
gcc.link.dll /boost_1_57_0/lib/libboost_date_time.dylib
ld: can't map file, errno=22 file '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
"g++" -Wl,"/System/Library/Frameworks/Python.framework/Versions/2.7/lib" -Wl,"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config" -o "/boost_1_57_0/lib/libboost_date_time.dylib" -shared "bin.v2/libs/date_time/build/gcc-4.0.1/release/threading-multi/gregorian/greg_month.o" "bin.v2/libs/date_time/build/gcc-4.0.1/release/threading-multi/gregorian/greg_weekday.o" "bin.v2/libs/date_time/build/gcc-4.0.1/release/threading-multi/gregorian/date_generators.o"
...failed gcc.link.dll /boost_1_57_0/lib/libboost_date_time.dylib...
gcc.link.dll /boost_1_57_0/lib/libboost_filesystem.dylib
ld: can't map file, errno=22 file '/System/Library/Frameworks/Python.framework/Versions/2.7/lib' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I believe something is wrong with the g++ commands, but I don't know what. Does anyone know how to fix this?
The linker, ld is being led to believe that
/System/Library/Frameworks/Python.framework/Versions/2.7/lib
is an input file in the linkage that it must read. Which it isn't;
it's a directory, so the attempt to read it as a file fails.
It's being led to believe this because this bit of your g++ linkage command:
-Wl,"/System/Library/Frameworks/Python.framework/Versions/2.7/lib"
tells it so. The g++ option:
-Wl,...
means: pass ... straight through to the linker. So the pathname is passed
though to the linker. Any pathname in the ld commandline is construed
as the name of an input file if not prefixed by any linker option to
indicate otherwise.
The same error is made immediately following with:
-Wl,"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config"
It seems likely that you want to tell g++ that the directories
/System/Library/Frameworks/Python.framework/Versions/2.7/lib
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config
are ones in which the linker can find libraries required by the linkage. (At least,
that's very likely what you want with the first one. I'm not so such about the
second).
To do that, pass g++ the options:
-L/System/Library/Frameworks/Python.framework/Versions/2.7/lib -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config
instead.

Gtk: build Gtk libs in Mavericks (Apple Mac OS) by compiling source code

I would like to use Gtk libs in a mac laptop. Its operation system is Mavericks. For my program, I don't care about Mac look and feel. For compiling my program, I don't like graphical interface but prefer to gcc and make command line tools (Linux style). Therefore, I don't want to install Gtk libs by installing Xcode (a monster taking too much space) plus port Gtk by macport. I prefer to compile source code to install Gtk libs. Followed are my steps.
Build gcc and make by installing Xcode command line tools commandlinetoolsosx10.9forxcode6.2.dmg from Apple Developer Website.
Build X development environment by installing XQuartz-2.7.8.dmg since X11 doesn't ship with Mac OS any more.
Download Gtk source packages and all its dependencies. I would install the packages in the order followed.
zlib-1.2.8 -> pkg-config-0.29 -> jpegsrc.v9a.tar.gz -> libpng-1.6.18 -> tiff-4.0.6 -> libiconv-1.14 -> gettext-0.19.6 -> freetype-2.6 -> fontconfig-2.11.94 -> libffi-3.2.1 -> pixman-0.33.4 -> cairo-1.14.4 -> glib-2.47.1 -> pango-1.38.1 -> atk-2.18.0 -> gdk-pixbuf-2.33.1 -> gtk+-2.24.28
Build environment variables for compilation.
GTK_DIR="$HOME/gtk"
CPPFLAGS="-I$GTK_DIR/include"
LDFLAGS="-L$GTK_DIR/lib"
PKG_CONFIG_PATH="$GTK_DIR/lib/pkgconfig"
export CPPFLAGS LDFLAGS PKG_CONFIG_PATH
LD_LIBRARY_PATH="$GTK_DIR/lib"
PATH="$GTK_DIR/bin:$PATH"
export LD_LIBRARY_PATH PATH
The compilation for package pkg-config-0.29
LDFLAGS="-framework CoreFoundation -framework Carbon"
./configure --with-internal-glib --prefix=$GTK_DIR
make
make install
The compilation for package gtk+-2.24.28
CPPFLAGS="-I$GTK_DIR/include -I$GTK_DIR/include/cairo -I$GTK_DIR/include/gdk-pixbuf-2.0 -I$GTK_DIR/include/pango-1.0 -I$GTK_DIR/include/atk-1.0 -I$GTK_DIR/include/freetype2 -I$GTK_DIR/include/gio-unix-2.0 -I$GTK_DIR/include/glib-2.0 -I$GTK_DIR/lib/glib-2.0/include"
LD_LIBRARY_PATH="$GTK_DIR/lib"
./configure --prefix=$GTK_DIR
make
make install
The compilation for other packages
LD_LIBRARY_PATH="$GTK_DIR/lib"
./configure --prefix=$GTK_DIR
make
make install
The compilation for all packages except the last one gtk+-2.24.28 made it. The error when compiling gtk+-2.24.28 is
/bin/sh ../../libtool --tag=CC --mode=link gcc -g -O2 -Wall -export-dynamic -framework CoreFoundation -framework Carbon -L/Users/wu/gtk/lib -o builder builder.o ../../gdk/libgdk-x11-2.0.la ../../gtk/libgtk-x11-2.0.la -L/usr/X11/lib -lXrender -lX11 -lm
libtool: link: gcc -g -O2 -Wall -o .libs/builder builder.o -L/Users/wu/gtk/lib ../../gdk/.libs/libgdk-x11-2.0.dylib -L/usr/X11/lib ../../gtk/.libs/libgtk-x11-2.0.dylib /Users/wu/Downloads/gtk/src/gtk+-2.24.28/gdk/.libs/libgdk-x11-2.0.dylib -lXinerama /Users/wu/gtk/lib/libpangocairo-1.0.dylib /Users/wu/gtk/lib/libpango-1.0.dylib /Users/wu/gtk/lib/libgthread-2.0.dylib /Users/wu/gtk/lib/libgdk_pixbuf-2.0.dylib /Users/wu/gtk/lib/libgio-2.0.dylib -lresolv /Users/wu/gtk/lib/libgobject-2.0.dylib /Users/wu/gtk/lib/libffi.dylib /Users/wu/gtk/lib/libcairo.dylib /Users/wu/gtk/lib/libpixman-1.dylib /Users/wu/gtk/lib/libfontconfig.dylib -lexpat /Users/wu/gtk/lib/libfreetype.dylib -lbz2 /Users/wu/gtk/lib/libpng16.dylib -lz -lSM -lICE -lXext /Users/wu/gtk/lib/libgmodule-2.0.dylib /Users/wu/gtk/lib/libglib-2.0.dylib /Users/wu/gtk/lib/libintl.dylib /Users/wu/gtk/lib/libiconv.dylib -lc -lXrender -lX11 -lm -pthread -framework ApplicationServices -framework Carbon -framework CoreFoundation
clang: warning: argument unused during compilation: '-pthread'
Undefined symbols for architecture x86_64:
"_atk_object_ref_relation_set", referenced from:
_test_widget in builder.o
"_atk_relation_get_relation_type", referenced from:
_test_widget in builder.o
"_atk_relation_get_type", referenced from:
_test_widget in builder.o
"_atk_relation_set_get_n_relations", referenced from:
_test_widget in builder.o
"_atk_relation_set_get_relation", referenced from:
_test_widget in builder.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[4]: *** [builder] Error 1
make[3]: *** [all-recursive] Error 1
make[2]: *** [all] Error 2
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
The error is a linking error about Undefined symbols for architecture x86_64. I googled the error and the suggestion is to set the right architecture in Xcode GUI. But it doesn't help since I don't have Xode GUI at all in laptop.
I also tried CC="gcc -arch i386", neither did it work. And I tried LDFLAGS="-framework CoreFoundation -framework Carbon -L$GTK_DIR/lib", neither did it work. For the last two try, I only recompiled the packages from cairo-1.14.4 to gtk+-2.24.28, not all the packages.
Any clues? Please help. Thank you.
Edit: using atk-2.20 solved the problem.
I was trying to do the same; adding -latk-1.0 to the LDFLAGS fixed the problem. It seems that the autotools/gcc/whatever is missing some lib/include flags. I have no idea why this is, but adding them in manually when calling ./configure works fine.
I think in this case that the complaint about the architecture is just a red herring.

How can I get the RInside example sandboxed_server to work on OS X Yosemite?

I'm having trouble getting the sandboxed_server example from RInside/examples/sandboxed_server working on xcode. I have OS X Yosemite 10.10.1.
I get the following error:
clang++ server/rinsideserver.o common/binarystream.o datatypes/bar.o datatypes/foo.o example_server.o -F/Library/Frameworks/R.framework/.. -framework R -llzma -lz -licucore -lm -liconv -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/Library/Frameworks/R.framework/Resources/lib -lRlapack /Library/Frameworks/R.framework/Versions/3.1/Resources/library/RInside/lib/libRInside.a -o example_server
Undefined symbols for architecture x86_64:
"RInside::set_callbacks(Callbacks*)", referenced from:
_main in example_server.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [example_server] Error 1
Command /usr/bin/make failed with exit code 2
I have not been able to get any example that uses callbacks to work. Examples (from standard examples) that don't use callbacks do work.
I've uncommented #define RINSIDE_CALLBACKS in RInsideConfig.h. I've reinstalled the source files through R/CRAN from source- as was suggested on another post.
I did modify the code in sandboxed_server to get CLOCK_MONOTONIC to work on mac- but nothing other than that.
I'd really like to get this example working. Any help would be greatly appreciated!

why does boost compilation fails with clang?

I am trying to compile boost C++ using clang.
./b2 --prefix=~/boost --includedir=~/boost/include --libdir=~/boost/lib toolset=clang cxxflags="-std=c++11 -stdlib=libc++ -isystem ~/libcxx/include/" linkflags="-stdlib=libc++ -L ~/libcxx/lib/"
When I see log file it gives error saying
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
__GLOBAL__I_a in exceptions.o
"boost::system::generic_category()", referenced from:
__GLOBAL__I_a in exceptions.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
my default clang is clang-mp-3.3. Can anyone please help me figure out what this problem is.
After searching for the reason of error in the web I found this which explain about compiling boost with clang. I am not sure why adding define=BOOST_SYSTEM_NO_DEPRECATED made it worked. Please do comment anyone who knows why it worked. I was then able to compile with no error but I still see some warnings but it worked for me for now.
The default clang I am using is (clang-mp-3.3)
$ clang -v
clang version 3.3 (tags/RELEASE_33/final)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
./bootstrap.sh --with-toolset=clang --without-libraries=mpi,python,graph_parallel
./b2 clean
./b2 toolset=clang cxxflags="-std=c++11 -stdlib=libc++" linkflags="-stdlib=libc++" define=BOOST_SYSTEM_NO_DEPRECATED

Resources