How do I create a dynamically loadable version of RtAudio in OSX - c++

I need to create librtaudio.dylib, a dynamically loadable RtAudio library (http://www.music.mcgill.ca/~gary/rtaudio/). I'd like to write CFFI bindings (in SBCL) but I can't seem to compile a dylib that is loadable using CFFI. Here are the compiler and linker commands I'm using to create the dylib:
g++ -O2 -Wall -Iinclude -fPIC -DHAVE_GETTIMEOFDAY -D__MACOSX_CORE__ -c RtAudio.cpp -o RtAudio.o
g++ -dynamiclib -install_name /usr/local/lib/librtaudio.dylib -lSystem -compatibility_version 1.0 -current_version 1.0 RtAudio.o -o librtaudio.dylib -lpthread -framework CoreAudio -framework CoreFoundation

It seems that CFFI's C++ support is not adequate to load RtAudio, as it is a C++ lib. A workaround is to write a C wrapper around RtAudio and then write bindings to the wrapper.

Related

Where is the atomic library and why is it not linking?

I'm compiling an old version of OGRE ON CMake 3.6 to see if I can compile or revive an old piece of software, and Boost is one of the dependencies for OGRE. It's essentially for multi-threading and without it, OGRE 1.9.0 builds completely fine on the macOS 12.0 SDK but fails macOS 10.8 SDK (specified in ZSH as -DCMAKE_OSX_DEPLOYMENT_TARGET but that's another issue for later).
The CMake command: cmake -DOGRE_CONFIG_DOUBLE=ON -DOGRE_BUILD_SAMPLES=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_ARCHITECTURES="x86_64" -DCMAKE_PREFIX_PATH=/usr/local (yes, I'm compiling x86_64 on a arm64 machine running macOS 12.1 & XCode 13.1 if it matters)
When I build OGRE on the 12.0 SDK with Boost support (all is found here), it successfully checks for all libraries. As a note, Boost also has the atomic library but it's only needed as .hpp files and not an actual .a or whatever-lib file format CPP uses. Compiling OGRE goes all well until I see this dreaded message:
Copying OS X content lib/macosx/Ogre.framework/Versions/1.9.0/Resources/ogrelogo.png
28 warnings generated.
21 warnings generated.
[ 44%] Linking CXX shared library ../lib/macosx/Ogre.framework/Ogre
> ld: library not found for -latomic <
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Key questions: What is this atomic library? Is it only on an older SDK? Is it the hpp files? Here is the linker command shortened to where I think is the most important parts
cd /Users/meh/Downloads/project/ogre-1-9-0/OgreMain && /Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/OgreMain.dir/link.txt --verbose=1
/usr/bin/clang++ -msse -stdlib=libc++ -std=c++11 -Wall -Winit-self -Wno-overloaded-virtual -Wcast-qual -Wwrite-strings -Wextra -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers -Wno-long-long -O3 -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk -dynamiclib -Wl,-headerpad_max_install_names -framework AGL -framework IOKit -framework Cocoa -framework Carbon -framework OpenGL -framework CoreVideo -compatibility_version 1.9.0 -current_version 1.9.0 -o ../lib/macosx/Ogre.framework/Versions/1.9.0/Ogre -install_name #executable_path/../Frameworks/Ogre.framework/Versions/1.9.0/Ogre (ALL THOSE OGRE .O FILES) -L/usr/local/lib /usr/local/lib/libboost_thread.a /usr/local/lib/libboost_date_time.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_chrono.a /usr/local/lib/Release/libFreeImage.a /usr/local/lib/Release/libzziplib.a /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk/usr/lib/libz.tbd -latomic
I had to play around with the CMake file to not error listed as:
framework AGL
-stdlib=libc++ -std=c++11
I've done research and atomic shows up in C++11 so that shouldn't be a problem? There's some include files under the SDK but definitely none under /usr/local/lib... (I will have to add -L/usr/local/include as a prefix to -latomic later)

Shared library using a different version of another shared library to the main executable?

Problem
Say I've got a Linux executable MAIN that uses an old version of some shared library LIB.so (which it locates using LD_LIBRARY_PATH).
I'd like MAIN to also use my library MYLIB.so, except this uses a newer version of LIB.so.
MAIN and MYLIB.so both use functions that appear in both versions of LIB.so with the same name (but different implementations).
How do I get the application to also load the new version of LIB.so when it loads MYLIB.so?
What doesn't seem to work
I've tried compiling MYLIB.so with an RPATH option pointing to the new version of the library. However, while this correctly identifies the newer library when I run ldd MYLIB.so, when the application runs it only uses the old library implementation.
My compilation (using single file toy implementations for clarity) is as follows:
# compile old library implementation
g++ -c -Wall -Werror -fpic library_old.cpp
g++ -shared -o liblib.so library_old.o
# compile new library implementation
g++ -c -Wall -Werror -fpic library_new.cpp
g++ -shared -o new/liblib.so library_new.o
# compile my library against new liblib
g++ -c -Wall -Werror -fpic my_library.cpp
g++ -L`pwd`/new -shared -Wl,-rpath,`pwd`/new -o libmine.so my_library.o -llib
# compile application against old liblib
g++ -L`pwd` -Wall -Werror -o main main.cpp -llib -lmine
export LD_LIBRARY_PATH=`pwd`

Compiling FLTK program on Mac

I am trying to compile an FLTK program (http://www.fltk.org/index.php) on Mac OSX Mavericks. All the .h packages compile just fine, but I receive the following error:
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I tried both g++ and clang++ -stdlib=libstdc++ to compile the program, but received the same error both times.
I would greatly appreciate any input on this issue to eliminate this error message.
You want to use the fltk-config script but it isn't clear how to use it generally form their documentation. This is a general form that I use and what it is actually doing:
From the command line you can compile like this (this assumes you need the image libraries, opengl libraries and wish to link statically [half the point of FLTK])
g++ file1.cpp file2.cpp `fltk-config --use-forms --use-gl --use-images --ldstaticflags --cxxflags` -o output
This is equivalent to
g++ file1.cpp file2.cpp -I/usr/local/include -I/usr/local/include/FL/images -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT /usr/local/lib/libfltk_images.a /usr/local/lib/libfltk_png.a -lz /usr/local/lib/libfltk_jpeg.a /usr/local/lib/libfltk_gl.a -framework AGL -framework OpenGL -framework ApplicationServices /usr/local/lib/libfltk_forms.a /usr/local/lib/libfltk.a -lpthread -framework Cocoa -o output
So if you make sure the libraries are in /usr/local/lib and the headers in /usr/local/include that should work...
fltk-config is just a script that comes in the fltk-1.3.2 (or whatever) folder. Building FLTK from the make file should add that to your path. If not copy it or direct it to wherever it is. It does make me wonder though: have you definitely built the libraries?
This is what worked for me:
#!/bin/bash
rm ./a.out
clang++ main.cpp -o a.out -std=c++17 -stdlib=libc++ \
-I/usr/include/fltk-1.3.8/FL \
-L/usr/local/lib \
$(fltk-config --ldflags) \
&& ./a.out

Compile & link with SWIG on mac that will run on windows

I have a python program that uses a bit of c++ code that I used SWIG to compile and link. I compiled the c++ code with swig under mac osx. I would like to give the python program to someone who uses windows xp. Is there a way to compile the c++ on the mac so that it will run on windows? Here is the code I used to compile and link the mac version.
swig -classic -c++ -python pythoncpp.i
g++ -O2 -c -arch i386 -fPIC pythoncpp.cpp -I/path -I/path
g++ -O2 -c -arch i386 -fPIC pythoncpp_wrap.cxx -I/path -I/path -L/path
ld -bundle -flat_namespace -undefined suppress -L/path -lgsl -lgslcblas -lm -o _pythoncpp.so *.o

Problem linking my code with ARPACK on OSX (using MacPorts for ARPACK)

I am trying to compile a C++ program which invokes the ARPACK library.
My problem is that when everything is linked, some of the symbols in the ARPACK library do not get resolved. They are
__gfortran_transfer_integer
__gfortran_transfer_character
__gfortran_transfer_complex
__gfortran_compare_string
__gfortran_st_write_done
__gfortran_st_write
__gfortran_transfer_real
__gfortran_transfer_array
I did a brute force search on my lib directory, and found no library which provided all of these symbols. A couple of them are provided by libf77blas, and it looks like g95 has some similar symbols (with gfortran replaced by g95), but I am at a complete loss as to what else I might need to install. I am compiling my code with
g++-mp-4.5 -O3 -Wall -Wl,-search_paths_first -headerpad_max_install_names my.o -o my.out -L/opt/local/lib -larpack -lm -L/opt/local/lib -lgsl -lgslcblas -lm -lf77blas -llapack -larpack -lqblas -lsquack
and /opt/local/lib actually has all the libraries I reference.
Has anyone run into this problem, or can point to the solution?
add to linker -lgfortran .................