본문 바로가기

Tools/Eclipse

osx에서 dyld 링크하기

[출처] : 


Eclipse on Mac dyld: Library not loaded: Image not found

2013-09-17 04:50 — Ycul Ssim — 출처⇊ 1 답변

All:

I have successfully set up two projects (Hello and World) in one workspace (HelloWorld). These are simple projects that are supposed to work together as a main project and a shared library project. This is the structure:

HelloWorld
    Hello
        src
            Hello.cpp
        Debug
            src
            Hello - [x86_64/le]
    World
        src
            World.cpp
            World.h
        Debug
            src
            libWorld.dylib - (x86_64/le]

I have followed all instructions and finally was able to get them to compile with no errors. However, when I try to run the Hello project I receive the following error:

dyld: Library not loaded: libWorld.dylib
  Referenced from: /Users/pdl/Development/HelloWorld/Hello/Debug/Hello
  Reason: image not found

I posted the code below. It is super simple and the problem (I believe) is somewhere in the Eclipse configuration.

Thank you in advance for your help.

-------------------------------------------- Source Code ----------------------------------

Hello.cpp

#include <stdio.h>
#include "World.h"

int main() {
  printf("Hello %s\n", getWorld().c_str());
  return 0;
}

World.cpp

#include "World.h"

std::string getWorld() { return "World"; }

World.h

#include <string>

std::string getWorld();

회사 소개

우리는 Q & A 사이트에 오신 것을 환영합니다EclipsePeople

당신이 당신의 플러그인에 의문을 가지고 있다면 영어로, 여기에 문의하시기 바랍니다 Eclipse

당신은 우리의 웹 사이트에 더 도움을 필요로하는 경우에 저희에게 이메일을 보내십시오.



Wohc Selrahc answered on 2014-09-17 06:25#1

dyld: Library not loaded: libWorld.dylib
Referenced from: /Users/pdl/Development/HelloWorld/Hello/Debug/Hello
Reason: image not found

This means that your program is using a dynamic library called libWorld.dylib, although you linked your dynamic library to your program during compiling. But you have to tell your program where is the dylib in run-time.

There are two solutions:

Solution 1: set up dynamic library environment variables for your project in Eclipse:

1.Right click your project name, select Run As->Run Configuration

2.Under Environment tab click New

3.Put DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH in Name box

4.Put the path where your libWorld.dylib file is in Value box

For example: if libWorld.dylib file is in /opt/local/lib/my_dylib folder, than put the path in theName box

Solution 2: set DYLD_LIBRARY_PATH in your bash configuration file

1.Normally in Mac OS, configuration file is .profile under ~/ folder, if you don't have this file than create a new file with the same name

2.Edit that file:

Add this line in your .profile file: export DYLD_LIBRARY_PATH=PATH_TO_YOUR_DYLIB:$DYLD_LIBRARY_PATH

in my example the PATH_TO_YOUR_DYLIB is opt/local/lib/my_dylib, so you just add: export DYLD_LIBRARY_PATH=/opt/local/lib/my_dylib:$DYLD_LIBRARY_PATH in .profile file

3.Problem solved, in this solution, you don't have to set up dylib path for all eclipse project

PS. DYLD_LIBRARY_PATH is an environment variable to specify the path of your dynamic library

Difference between DYLD_LIBRARY_PATH and DYLD_FALLBACK_LIBRARY_PATH please refer to this post