Lester Dela Cruz
Lester Dela Cruz

Reputation: 67

How can I link executable with dylib in Mac OS X command line?

I'm trying to figure out how to compile things on command line without a and ide. This is my first time building a project using command line. And, I get stomped on linking dependencies on a simple project.

I have this tree structure:

.
├── include
...
...
├── lib
│   ├── libopencv_calib3d.3.3.1.dylib
│   ├── libopencv_core.3.3.1.dylib
│   ├── libopencv_cudev.3.3.1.dylib
│   ├── libopencv_features2d.3.3.1.dylib
│   ├── libopencv_flann.3.3.1.dylib
│   ├── libopencv_highgui.3.3.1.dylib
│   ├── libopencv_imgcodecs.3.3.1.dylib
│   ├── libopencv_imgproc.3.3.1.dylib
│   ├── libopencv_videoio.3.3.1.dylib
│   ├── libopencv_ximgproc.3.3.1\ 2.dylib
│   └── libopencv_ximgproc.3.3.1.dylib
├── resources
│   ├── giraffe.jpg
│   ├── golden-retriver-puppy.jpg
│   ├── kangaroo.jpg
│   └── kitty.jpeg
└── sources
    └── main.cpp

and I have this code in the main:

#include "opencv2/ximgproc/segmentation.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <ctime>

static void help() {
    std::cout << std::endl <<
    "Usage:" << std::endl <<
    "./ssearch input_image (f|q)" << std::endl <<
    "f=fast, q=quality" << std::endl <<
    "Use l to display less rects, m to display more rects, q to quit" << std::endl;
}


int main(int argc, char** argv) {
    std::cout << "Test" << std::endl;
    // If image path and f/q is not passed as command
    // line arguments, quit and display help message
    if (argc < 3) {
        help();
        return -1;
    }

    return 0;
}

and in my mac terminal I built it with this command:

clang++ sources/main.cpp -o a -I./include -L./lib -lopencv_core.3.3.1 -lopencv_ximgproc.3.3.1 -lopencv_highgui.3.3.1 -lopencv_imgproc.3.3.1 -lopencv_imgcodecs.3.3.1

and it doesn't give me any errors.

but when I run ~$ ./a

I get the following error:

dyld: Library not loaded: @rpath/libopencv_core.3.3.dylib
  Referenced from: 
  /Users/lgdelacruz/Documents/GSoC/project/objectdetection/./a
  Reason: image not found
Abort trap: 6

I've been messing with these for days, and I can't seem to figure it out. How does the executable find the things to link to when it needs those definitions?

Upvotes: 4

Views: 4781

Answers (1)

lzhbrian
lzhbrian

Reputation: 56

You are dynamically linking libraries to compile your code, which needs to tell the system where to find the libraries both compile time and run time.

Right now, you are only telling the compiler to compile from libraries in the path specified by -L, but not telling the system where to find the libraries when executing it.

You probably need this:

export DYLD_LIBRARY_PATH=./lib:$DYLD_LIBRARY_PATH

where ./lib indicates where your libraries are.

Upvotes: 3

Related Questions