g++ or gcc Dynamic library linking with rpath on Mac OS

I tried to make a dynamic library with simple code. I ran as follows

$g++ -c mysum.cpp
$g++ -dynamiclib -install_name '@rpath/lib/libblah' mysum.o -o libblah.dylib
$mkdir lib
$mv libblah.dylib ./lib
$g++  example.cpp -lblah -L`pwd`/lib -Xlinker -rpath -Xlinker `pwd` -o example 

But I got this error => 'dyld: Library not loaded: @rpath/lib/libblah'

My code is like below


#include "mysum.h"
#include <iostream>

int main(){

    std::cout << ysum(1 ,3) << std::endl;
    std::cout << ydiff(2, 2) << std::endl;

    return 0;


#include "mysum.h"

int ysum(int a, int b){
    return a + b;

int ydiff(int a, int b){
    return a -b;


int ysum(int a, int b);
int ydiff(int a, int b);

What is the problem?

*What if I want to use a shared library? Is it right to add '-shared' like this?

$g++ -dynamiclib -shared -install_name '@rpath/lib/libblah' mysum.o -o libblah.dylib