1

I have problems in using the irrklang library. I downloaded the .zip file from https://www.ambiera.com/irrklang/downloads.html. I have a MacBookPro with Mojave 10.14.6.

I tried to compile the following source code:

#include <stdio.h>
#include <irrKlang.h>
using namespace irrklang;
#pragma comment(lib, "irrKlang.lib") // link with irrKlang.dll
int main(int argc, const char** argv){
    printf("\nHello World!\n");
    return 0;
}

I have a directory which contains

  • main.cpp
  • include -> the headers files
  • bin -> it includes the subdirectories dotnet-4-64, macosx-gcc, linux-gcc-64, winx64-visualStudio
  • bell.wav, getout.ogg
  • Makefile

The Makefile is pretty simple (I adapted the one from the examples directory of the original .zip file):

CPP = g++
OPTS = -dynamiclib -I"include" -L"bin/macosx-gcc" -lirrklang -pthread

all:
    $(CPP) main.cpp -o example $(OPTS)

clean:
    rm example

I added the -dynamiclib option because otherwise the linker search for the library in /usr/lib.

When I run Make everything seems to work fine, no errors, but if I try to execute ./example I get the following error:

-bash: ./example: cannot execute binary file

I searched on the web, the only hint I found was to check with file ./example the compatibility with my OS: the result is

./example: Mach-O 64-bit dynamically linked shared library x86_64

and as expected the binary file actually is executable in this OS. I can not see the problem, does someone have any suggestion?

Eddymage
  • 1,008
  • 1
  • 7
  • 22

2 Answers2

1

The dynamiclib option makes gcc produce a shared library instead of an executable. As you can see in the output of file ./example, it's indeed a dynamically linked shared library, not an executable.

You should use the -L option for specifying library paths. Try passing an absolute path to the -L option.

sparik
  • 1,181
  • 9
  • 16
  • Dear @sparik, thanks, your're right. I tried to modify the Makefile by writing `-L"bin/macosx-gcc` where the `libirrklang.dylib` is. Make is running fine, when I try to execute the file I get `dyld: Library not loaded: /usr/local/lib/libirrklang.dylib` ` Reason: image not found` Same thing if I write the absolute path. It seems that it is searching for the library again in `/usr/local/lib`, am I right? – Eddymage Mar 30 '20 at 22:23
  • Did you try specifying an absolute path for the -L option? – sparik Mar 31 '20 at 07:15
  • Yes, I tried `-L"Users/USERNAME/Documents/Code/Soundtest/bin/macosx-gcc"` and also `-L"~/Documents/Code/Soundtest/bin/macosx-gcc"`: again no errors in compiling, but I get the error message written in my previous comment when trying to execute the binary. – Eddymage Mar 31 '20 at 08:13
1

In addition to the other answer here, there's one more thing you need to do. Dynamic libraries on macOS contain a 'load path' which tell the OS where to find said library to load it when an executable linked about it is launched. This load path is read from the dylib and baked into your executable at link time, which is why you're getting the problem you report in the comments.

Apple provide a utility called install_name_tool to patch the load path in the executable after linking, so from what you have posted you probably want something like:

install_name_tool -change libirrklang /bin/macosx-gcc/libirrklang example

There's a good writeup about this here:

https://medium.com/@donblas/fun-with-rpath-otool-and-install-name-tool-e3e41ae86172

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Dear @Paul, thanks a lot! Using the link you posted and following the info about `install_name_tool`, `otool` and similar, I found what I had to do: - in the bin/macosx-ggc directory I typed `install_name_tool -id "@rpath/libirrklang.dylib" libirrklang.dylib - in the `sound` directory (where `example` binary is) I typed `install_name_tool -add_rpath "@executable_path/lib/" libirrklang.dylib` and everything works like a charm! Thanks! – Eddymage Apr 01 '20 at 20:40