How to build openFrameworks as a shard library on macOS - c++

I'm trying to build OF as a shared library (.so) in macOS.
I added the following to my app's config.make file.
APPNAME = myApp.so
PROJECT_LDFLAGS = -rdynamic -shared -undefined dynamic_lookup
PROJECT_OPTIMIZATION_CFLAGS_RELEASE = -O3
But when I build the project using make, it actually generates an Application file myApp.so.app which is not a shared library.
How can I properly build OF as a shared library in macOS?

I could successfully build OF as a shared library with the following:
APPNAME = myApp.so
PROJECT_CFLAGS = -Wall
PROJECT_LDFLAGS = -dynamiclib -Wl,-undefined -Wl,dynamic_lookup
PROJECT_OPTIMIZATION_CFLAGS_RELEASE = -O3

Related

Cross compiling cppzmq for arm cannot find sodium.h

I have a C++ application and I need to cross compile it for armv7. It needs 3 libraries:
cppzmq
Eigen
nlohmann/json
What I have
A raspberry compute module 3+ with Debian 9 Buster
Manjaro Linux (or windows I guess)
$ uname -a
Linux user 5.15.71-1-MANJARO #1 SMP PREEMPT Wed Sep 28 11:24:27 UTC 2022 x86_64 GNU/Linux
A project set up with CLion and Meson by me following the basic cross compiling tutorial (see below for files content)
arm-none-linux-gnueabihf toolchain
meson txt file for setting up the toolchain
meson.build file
meson .wrap files for easier dependencies management
What is my issue
This is my first time doing cross compiling or working with C in general, I know I'm really near the solution but I'm wasting a little too much time on this.
I'm able to compile Eigen and Json
I added catch2 and libzmq to solve some issues and I can compile these too
libobsd and libsodium are compiled but not picked up by cppzmq
I think I need to pass the compiled libobsd and libsodium as libraries to cppzmq but I'm not sure. Error log cleaned up:
Executing subproject cppzmq
cppzmq| Project name: cppzmq
cppzmq| Project version: 4.8.1
cppzmq| C++ compiler for the host machine: /usr/bin/arm-none-linux-gnueabihf-g++ (gcc 10.3.1 "arm-none-linux-gnueabihf-g++ (GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)) 10.3.1 20210621")
cppzmq| C++ linker for the host machine: /usr/bin/arm-none-linux-gnueabihf-g++ ld.bfd 10.3-2021
cppzmq| C++ compiler for the build machine: c++ (gcc 12.2.0 "c++ (GCC) 12.2.0")
cppzmq| C++ linker for the build machine: c++ ld.bfd 2.39.0
cppzmq| Dependency libzmq from subproject subprojects/zeromq-4.3.4 found: YES undefined
cppzmq| Dependency threads found: YES unknown (cached)
cppzmq| Dependency catch2 from subproject subprojects/Catch2-3.1.0 found: YES 3.1.0
cppzmq| Build targets in project: 149
cppzmq| Subproject cppzmq finished.
meson 0.0.1
Subprojects
catch2 : YES
cppzmq : YES
eigen : YES
json : YES
libobsd : YES
libzmq : YES 2 warnings
User defined options
Cross files: /my_path/meson/raspberry-cm3+.txt
backend : ninja
Found ninja-1.11.1 at /usr/bin/ninja
Cleaning... 107 files.
[1/270] Compiling C++ object subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o
FAILED: subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o
/usr/bin/arm-none-linux-gnueabihf-g++ -Isubprojects/zeromq-4.3.4/libobjects.a.p -Isubprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4/include -Isubprojects/zeromq-4.3.4/__CMake_build -I../srcdir/subprojects/zeromq-4.3.4/__CMake_build -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=gnu++11 -O0 -g -fPIC -Wno-tautological-constant-compare -O3 -DNDEBUG -DZMQ_CUSTOM_PLATFORM_HPP -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE -MD -MQ subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o -MF subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o.d -o subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o -c ../srcdir/subprojects/zeromq-4.3.4/src/msg.cpp
In file included from ../srcdir/subprojects/zeromq-4.3.4/src/msg.cpp:31:
../srcdir/subprojects/zeromq-4.3.4/src/compat.hpp:42:10: fatal error: bsd/string.h: No such file or directory
42 | #include <bsd/string.h>
| ^~~~~~~~~~~~~~
compilation terminated.
[2/270] Compiling C++ object subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o
FAILED: subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o
/usr/bin/arm-none-linux-gnueabihf-g++ -Isubprojects/zeromq-4.3.4/libobjects.a.p -Isubprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4/include -Isubprojects/zeromq-4.3.4/__CMake_build -I../srcdir/subprojects/zeromq-4.3.4/__CMake_build -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=gnu++11 -O0 -g -fPIC -Wno-tautological-constant-compare -O3 -DNDEBUG -DZMQ_CUSTOM_PLATFORM_HPP -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE -MD -MQ subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o -MF subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o.d -o subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o -c ../srcdir/subprojects/zeromq-4.3.4/src/ipc_address.cpp
In file included from ../srcdir/subprojects/zeromq-4.3.4/src/ipc_address.cpp:31:
../srcdir/subprojects/zeromq-4.3.4/src/compat.hpp:42:10: fatal error: bsd/string.h: No such file or directory
42 | #include <bsd/string.h>
| ^~~~~~~~~~~~~~
compilation terminated.
[3/270] Compiling C++ object subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o
FAILED: subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o
/usr/bin/arm-none-linux-gnueabihf-g++ -Isubprojects/zeromq-4.3.4/libobjects.a.p -Isubprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4/include -Isubprojects/zeromq-4.3.4/__CMake_build -I../srcdir/subprojects/zeromq-4.3.4/__CMake_build -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=gnu++11 -O0 -g -fPIC -Wno-tautological-constant-compare -O3 -DNDEBUG -DZMQ_CUSTOM_PLATFORM_HPP -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE -MD -MQ subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o -MF subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o.d -o subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o -c ../srcdir/subprojects/zeromq-4.3.4/src/curve_mechanism_base.cpp
In file included from ../srcdir/subprojects/zeromq-4.3.4/src/curve_mechanism_base.cpp:32:
../srcdir/subprojects/zeromq-4.3.4/src/curve_mechanism_base.hpp:38:10: fatal error: sodium.h: No such file or directory
38 | #include "sodium.h"
| ^~~~~~~~~~
compilation terminated.
Files Details
raspberry-cm3+.txt to set up the cross compiling
[binaries]
c = '/usr/bin/arm-none-linux-gnueabihf-gcc'
cpp = '/usr/bin/arm-none-linux-gnueabihf-g++'
ar = '/usr/bin/arm-none-linux-gnueabihf-ar'
strip = '/usr/bin/arm-none-linux-gnueabihf-strip'
[host_machine]
system = 'linux'
cpu_family = 'arm'
cpu = 'armv7hl'
endian = 'little'
meson.build to set up the project
project('meson', 'cpp', version : '0.0.1')
cpp = meson.get_compiler('cpp')
os = target_machine.system()
cmake = import('cmake')
dep_eigen = subproject('eigen').get_variable('eigen_dep')
lib_zmq = cmake.subproject('libzmq')
dep_zmq = lib_zmq.dependency ('libzmq')
# lib_libsodium = cmake.subproject('libsodium')
# dep_libsodium = lib_libsodium.dependency ('sodium')
# lib_libsodium = cpp.find_library('libsodium', required: false)
# if lib_libsodium.found()
# dep_sqlitecpp = lib_libsodium
# endif
dep_catch = subproject('catch2').get_variable('catch2_dep')
dep_libobsd = subproject('libobsd').get_variable('libobsd_dep')
if cpp.has_header('zmq.hpp')
# cppzmq is a header-only dependency, so if we already have the
# header in place, we don't need to actually change anything
dep_cppzmq = [] #declare_dependency()
else
dep_cppzmq = subproject('cppzmq').get_variable('cppzmq_dep')
endif
if cpp.has_header('nlohmann/json.hpp')
dep_json = [] #declare_dependency()
else
dep_json = subproject('json').get_variable('nlohmann_json_dep')
endif
deps = [
dep_eigen,
# dep_libsodium,
dep_libobsd,
dep_catch,
dep_zmq,
dep_cppzmq,
dep_json
]
executable(
'demo',
'main.cpp',
dependencies :deps
)
Wrap Files
catch2, cppzmq, eigen, libobsd and json are the default ones from the meson wrap repository
libsodium.wrap (custom repo to use cmake)
[wrap-git]
directory = libsodium-1.0.18
url = https://github.com/robinlinden/libsodium-cmake.git
revision = master
depth = 1
clone-recursive = true
[provide]
libsodium = libsodium_dep
sodium = sodium_dep
libzmq.wrap
[wrap-file]
directory = zeromq-4.3.4
source_url = https://github.com/zeromq/libzmq/releases/download/v4.3.4/zeromq-4.3.4.tar.gz
source_filename = zeromq-4.3.4.tar.gz
source_hash = c593001a89f5a85dd2ddf564805deb860e02471171b3f204944857336295c3e5
[provide]
libzmq = libzmq_dep
Notes
I don't mind switching build tools to CMake for example
there is another toolchain on the AUR repository I don't know if it could change anything (trying right now)
???
profit

Error loading shared libraries after cross compiling: No such file or directory

I'm having problems to load share libraries after cross-compiling my C++ code using Docker Buildx, having a Raspberry Pi Zero W as the target.
After I perform the build, I copy the generated binary to a Raspberry Pi Zero already running and with OpenCV4 installed.
When I run the executable, the following error message is shown:
pi#raspberrypi:/mnt/system/$ ./software.run
./software.run: error while loading shared libraries: libopencv_freetype.so.4.2: cannot open shared object file: No such file or directory
Despite OpenCV4 being already installed, this particular lib wasn't in the /usr/lib. So, I copied it, run sudo ldconfig but, even after this procedure, my software still cannot find the lib.
I even added the /usr/lib to the path of the system, but, it didn't work.
pi#raspberrypi:/usr/lib $ sudo ldconfig -v | grep libopencv_free
ldconfig: Can't stat /usr/local/lib/arm-linux-gnueabihf: No such file or directory
ldconfig: Path `/lib/arm-linux-gnueabihf' given more than once
ldconfig: Path `/usr/lib/arm-linux-gnueabihf' given more than once
ldconfig: /lib/arm-linux-gnueabihf/ld-2.28.so is the dynamic linker, ignoring
ldconfig: /lib/ld-linux.so.3 is the dynamic linker, ignoring
libopencv_freetype.so.4.2 -> libopencv_freetype.so.4.2.0
pi#raspberrypi:/mnt/system/ $ file software.run
software.run: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=409a24c10761e2b9fd7a310cddfb09c86fb3a207, not stripped
pi#raspberrypi:/mnt/system $ echo $LD_LIBRARY_PATH
/usr/lib
Makefile:
CC = g++
STD = --std=c++14
SOFTWARE_SRC = $(wildcard src/software/*.cpp)
SOFTWARE_BIN = software.run
CV_LIBS = $(shell pkg-config --cflags --libs opencv4)
SOFTWARE_INC = -Iinclude -I/usr/include -I/usr/local/include
SOFTWARE_LDFLAGS = -lraspicam_cv -L/opt/vc/lib -lmmal -lmmal_core -lmmal_util -lwiringPi
all: software-out
software-out:
$(CC) $(STD) $(SOFTWARE_SRC) -o $(SOFTWARE_BIN) $(CV_LIBS) $(SOFTWARE_INC) $(SOFTWARE_LDFLAGS)
Other software that also uses OpenCV is working properly.
I also build a "Hello World" software just to validate my cross-compiling environment and it is working.
Thank you all in advance
EDIT
After several attempts, I was able to fix the problem by building the libs locally.
I couldn't identify what caused it, but, the libs generated by the Buildx environment weren't working properly in the Raspberry Pi Zero.
I'm building a truly cross-compiling environment to address this issue.

A problem with automake Mingw cross-compiling of a shared library linked to an installed static library

I am building a test hello-world executable module linking to a simple shared object library 'libshared' which, in turn, links to a static library 'libsodium' that was built in the 'depends' directory.
OS is Ubuntu 16.04.6 LTS.
When I build this for Linux the build is okay.
When I build this for Mingw this libtool error message is printed:
*** Warning: This system cannot link to static lib archive /home/ubuntu/repo/test-dll/depends/x86_64-w64-mingw32/share/../lib/libsodium.la.
*** I have the capability to make that library automatically link in when
*** you link to this library. But I can only do this if you have a
*** shared version of the library, which you do not appear to have.
and recipe for target 'libshared.la' failed.
The Makefile.am:
.PHONY: gen
.INTERMEDIATE: $
# shared lib
lib_LTLIBRARIES = libshared.la
libshared_la_SOURCES = \
src/example_dll.c
libshared_la_CPPFLAGS = -I$(top_srcdir)/src -I /src
libshared_la_CFLAGS = -DBUILDING_EXAMPLE_DLL -O2 -Wno-unused-parameter
libshared_la_LDFLAGS = -no-undefined
libshared_la_LIBADD = -lsodium
#libshared_la_LIBADD = $(prefix)/lib/libsodium.la
# executable
inst_PROGRAMS = testexe
instdir=$(prefix)/bin
testexe_SOURCES = \
src/example_exe.c
testexe_CFLAGS = -DBUILDING_EXAMPLE_DLL -O2 -Wno-unused-parameter
testexe_CPPFLAGS = -I$(top_srcdir)/src
testexe_LDADD = libshared.la
How I invoke configure and make (build_win.sh):
export HOST=x86_64-w64-mingw32
CXX=x86_64-w64-mingw32-g++-posix
CC=x86_64-w64-mingw32-gcc-posix
PREFIX="$(pwd)/depends/$HOST"
set -eu -o pipefail
set -x
cd depends/ && make HOST=$HOST V=1 NO_QT=1
cd ../
./autogen.sh
CONFIG_SITE=$PWD/depends/x86_64-w64-mingw32/share/config.site ./configure --prefix="${PREFIX}" --host=x86_64-w64-mingw32
CC="${CC} -g " CXX="${CXX} -g " make V=1
configure.ac params is trivial
The only way I managed to build it was if I manually moved libsodium.la into the root of the distribution and 'libsodium.a' in the '.libs' subdirectory and manually corrected libsodium.la parameters 'libdir to '' (was '/home/ubuntu/repo/test-dll/depends/x86_64-w64-mingw32/share/../lib/libsodium.a') and 'installed' to 'no' (was 'yes').
So my questions are:
Why is it trying to link libsodium as a shared library when it is in 'depends' directory and set as 'installed=yes' (I need to link it as static)?
Could I have correct automake settings for MINGW to link my shared library to static libs in 'depends' directory?
Just found a solution (or workaround) how to make this link on Mingw (and produce dll):
instead of libshared_la_LIBADD = -lsodium that causes libtool to search for the libsodium shared library, I just passed an option -Wl,-lsodium to the linker in libshared_la_CFLAGS.

Statically linking libtcod

I'm trying to statically link libtcod to my C++ project without success. I get many linking errors, including:
./tuto: error while loading shared libraries: libtcodxx.so.1: cannot open shared object file: No such file or directory
Has anyone had similar problems with that library?
There are dynamic libs provided in current libtcod (1.6.0) distribution.
You can link libtcod dynamically, but you need to create symbolic links:
cd /var/lib/libtcod
ln -s libtcod.so libtcod.so.1
ln -s libtcodxx.so libtcodxx.so.1
Compile with following command:
g++ src/*.cpp -o tuto -I/var/lib/libtcod/include -L/var/lib/libtcod -ltcod -ltcodxx -Wl,-rpath=/var/lib/libtcod -Wall
Which flags do you use while linking to the library? Did you used -static flag, do you specified -ltcod? If not, add -static -ltcod to the end of command line.
Or you can force tell gcc to link with static builded library:
gcc %YOUR_OTHER_FLAGS_ANDFILES% -l:libtcod.a -L%PATH_TO_TCOD_STATIC_BUILDED_FILE%

Linking a static library into Boost Python (shared library) - Import Error

I am building a Boost Python module (.so shared library file) which depends on another external library (STXXL)
While I can build and import the example Boost Python modules, I run into problems when STXXL is thrown into the mix. Specifically when running import fast_parts in python
I get ImportError: ./fast_parts.so: undefined symbol: _ZN5stxxl10ran32StateE
This says to me that the STXXL library isn't being linked, but I am not sure how that could be as I am linking against it and the linker isn't giving me any errors. It's worth noting I can successfully build and run standalone programs using STXXL and as far as I know the libraries are stored in a .a archive in the lib directory shown below. I have reduced my Makefile down to a single command as follows:
g++ -I/home/zenna/Downloads/stxxl-1.3.0/include -include stxxl/bits/defines.h -I/home/zenna/local/include -I/usr/include/python2.6 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -O3 -Wall -g -DFOO=BAR -pthread -L/home/zenna/Downloads/stxxl-1.3.0/lib/ -lstxxl -L/home/zenna/local/lib/ -lboost_python -lpython2.6 -fPIC -shared -o fast_parts.so partition.cpp
Any advice?
I'm assuming Linux, please comment if this is incorrect. What does the ldd output for libfast_parts.so look like? Does it indicate libstxxl.so is not found?
You might need to add /home/zenna/Downloads/stxxl-1.3.0/lib/ in your LD_LIBRARY_PATH or the rpath for libfast_parts.so.
-Wl,-rpath,/home/zenna/Downloads/stxxl-1.3.0/lib -L/home/zenna/Downloads/stxxl-1.3.0/lib -lstxxl