2010-12-10 3 views
2

Xcode에서 .dylib를 만들고 사용하는 가장 기본적인 방법은 무엇입니까?간단한 .dylib 만들기 및 사용

파일 : MyLib.h

#include <string> 

namespace MyLib 
{ 
    void SayHello(std::string Name); 
} 

파일 : MyLib.cpp

#include <string> 
#include <iostream> 

#include "MyLib.h" 

void MyLib::SayHello(std::string Name) 
{ 
    std::cout << "Hello, " << Name << "!"; 
} 

내가 같이 컴파일 할 프로젝트를 가지고 여기

내가 지금까지 가지고 무엇을 동적 라이브러리이지만 다른 프로젝트에서 어떻게 사용합니까?

파일 : 나는이 같은 노력 MyLibTester.cpp

#include "libMyLib.dylib" 

int main() 
{ 
    MyLib::SayHello("world"); 
    return 0; 
} 

을하지만 대부분 Stray \123 in program의 라인을 따라 400 오류 (이상 준 <libMyLib.dylib> 나에게 file not found 오류를 준 사용

답변

2

당신은 그런 다음 dylib에 연결하는 프로그램에 대한 컴파일러 말할 필요

그래서

#include "MyLib.h"

쓰기 라이브러리 파일,하지만 헤더 (.H)를 포함하지 않는 파일. Xcode를 사용하는 경우 dylib 파일을 프로젝트로 끌어 올 수 있습니다.

0
.. m에

/usr/bin/g++ -c -fno-common -fPIC -Wall \ 
-Wno-invalid-offsetof -Wfatal-errors -fPIC \ 
-O3 -fomit-frame-pointer --inline -DTARGET_BUILD \ 
-I/usr/local/lib/felix/felix-1.1.6rc1/lib/rtl \ 
-I/usr/local/lib/felix/felix-1.1.6rc1/config/target \ 
./hello.cpp -o ./hello.os 

/usr/bin/g++ -fno-common -dynamiclib -O3 \ 
-fomit-frame-pointer --inline \ 
./hello.os -o ./hello.dylib \ 
-L/usr/local/lib/felix/felix-1.1.6rc1/lib/rtl \ 
-lflx_dynamic -lflx_gc_dynamic -lflx_judy_dynamic -lflx_exceptions_dynamic 

:

FWIW 내 생산 컴파일러는이 사용 hello.cpp의 ake hello.dylib를 사용하면 비 필수 비트를 제거 할 수 있습니다. 비 필수 비트는 시스템에있는 내용이기 때문에 나중에 제거 할 수 있습니다. 나중에 좀 더 고급 작업을 수행하려는 경우 도움이 될 수 있습니다. 컴파일시 -fPIC은 필수 항목입니다. -dynamiclib는 dylib를 만드는 것입니다.