Makefile 질문드립니다. .so 라이브러리

idrukawa의 이미지


.SUFFIXES: .cpp .o
 
CC  = g++
 
INC = -I./
SRC = HbInf.cpp
#OBJ = $(patsubst %.cpp,%.o,$(wildcard *.cpp))
OBJ = $(SRC:.cpp=.o)
DEP = $(OBJ:.o=.d)
 
OPT = -g -O2 -D_REENTRANT -m32 -Wall
 
#OUT = a.out
OUT = libHbInf.so
 
 
all: $(OUT)
 
$(OUT): $(OBJ)
    $(CC) -shared -fPIC -o $@ $<
    #$(CC) -o $@ $< $(INC) $(LIB) $(OPT)
 
#---------------------
# create dependency
#---------------------
-include $(DEP)
 
%.d: %.cpp
    @set -e; rm -f $@; \
    $(CC) -M $< > $@.$$$$; \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    rm -f $@.$$$$
 
#---------------
# remove files
#---------------
clean: 
    \rm -rf *.o $(OUT) $(DEP) core.*

이렇게 Makefile을 작성하고 make를 해보면 에러가 발생합니다.

test/MsgQ] make
g++    -c -o HbInf.o HbInf.cpp
g++ -shared -fPIC -o libHbInf.so HbInf.o
Text relocation remains                         referenced
    against symbol                  offset      in file
std::__ioinit                       0x5bc       HbInf.o
std::__ioinit                       0x5c0       HbInf.o
std::__ioinit                       0x5f4       HbInf.o
std::__ioinit                       0x5f8       HbInf.o
<unknown>                           0x55c       HbInf.o
<unknown>                           0x560       HbInf.o
msgctl                              0x248       HbInf.o
msgctl                              0x52c       HbInf.o
memset                              0x3fc       HbInf.o
memset                              0x4dc       HbInf.o
msgget                              0x20c       HbInf.o
msgget                              0x258       HbInf.o
msgget                              0x2b0       HbInf.o
msgget                              0x344       HbInf.o
msgget                              0x4ec       HbInf.o
printf                              0x570       HbInf.o
std::ios_base::Init::Init()         0x5c4       HbInf.o
sleep                               0x494       HbInf.o
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const0x14        HbInf.o
msgrcv                              0x388       HbInf.o
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const0x9c              HbInf.o
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const0x128             HbInf.o
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const0x184             HbInf.o
time                                0x3e4       HbInf.o
std::ios_base::Init::~Init()        0x5fc       HbInf.o
msgsnd                              0x2f0       HbInf.o
unsigned int const& std::min<unsigned int>(unsigned int const&, unsigned int const&)0x44        HbInf.o
sendHb(stHbMsg_)                    0x470       HbInf.o
ld: fatal: relocations remain against allocatable but non-writable sections
collect2: ld returned 1 exit status
make: *** [libHbInf.so] Error 1

이 부분을

$(OUT): $(OBJ)
    $(CC) -shared -fPIC -o $@ $<
    #$(CC) -o $@ $< $(INC) $(LIB) $(OPT)

이렇게 수정하여 실행파일을 만들도록 하면 컴파일이 정상적으로 수행됩니다.

$(OUT): $(OBJ)
    #$(CC) -shared -fPIC -o $@ $<
    $(CC) -o $@ $< $(INC) $(LIB) $(OPT)

왜 이런지, 공유라이브러리를 만드는 방법이 무엇이 잘못되었는지 알려주세요~

ymir의 이미지

음.. 별 문제는 없어 보이는데..;; 컴파일러 버전이 어찌 되시는지..?
CXXFLAGS 에 -shared -fPIC 넣어주고 빌드해 보면 어찌 되나요?
추가로.. CC 도 괜찮긴 한데, c++ 인 경우에는 CXX 로 쓰시는게 좋습니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

idrukawa의 이미지

] gcc -v
Reading specs from /usr/local/lib/gcc/sparc-sun-solaris2.10/3.4.6/specs
Configured with: ../configure --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld --enable-shared --enable-languages=c,c++,f77
Thread model: posix
gcc version 3.4.6
입니다.

idrukawa의 이미지

] g++ -v
Reading specs from /usr/local/lib/gcc/sparc-sun-solaris2.10/3.4.6/specs
Configured with: ../configure --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld --enable-shared --enable-languages=c,c++,f77
Thread model: posix
gcc version 3.4.6
g++ 버전입니다.

ymir의 이미지

생각보다 버전이 낮네요..;;

소스 파일에 대충 cout 만 넣어서 테스트 해 봤는데..
4.4.5 에서는 수정없이 빌드 되고..
4.4.6/4.6.1 에서는 CXXFLAGS 추가해서 빌드 되었습니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

ymir의 이미지

삽질했네요.

4.4.5 시스템에서만 특이하게 -fPIC 을 요구하지 않은 것이었는데 착각했습니다.
CXXFLAGS 로 컴파일 옵션을 넣어주는게 맞습니다. (아래 태훈님 댓글 참조)
오브젝트 파일은 묵시적 타겟으로도 생성되기 때문에, 이 변수들만 맞춰주면 굳이 생성하지 않아도 상관 없구요.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

태훈의 이미지

오브젝트($(OBJ)) 생성하실때 -fPIC 옵션을 주시고, 오브젝트들을 공유 라이브러리로 링킹 할때는 -fPIC 옵션이 필요 없습니다.

위 Makefile에는 빠트리신건지 $(OBJ) 생성 규칙이 없으니, 예를들면

%.o: %.c
    $(CC) -fPIC -c $< -o $@

이런식으로 해주시면 됩니다.

Just do it!

태훈의 이미지

cpp니까 %.c -> %.cpp

$(OBJ): $(SRC) 형태로 작성하시도 되고...

Anyway~

Just do it!

idrukawa의 이미지

최종적으로 아래와 같이 수정하여 정상적으로 동적라이브러리가 생성되는 것을 확인하였습니다.
도움 주신분들 감사합니다.

CC  = g++
 
SRC = HbInf.cpp
OBJ = $(patsubst %.cpp,%.o,$(wildcard *.cpp))
INC = -I./
LIB =
DEP = $(OBJ:.o=.d)
 
OPT = -g -O2 -D_REENTRANT -m32 -Wall -shared -fPIC
 
OUT = libHbInf.so
 
 
all: $(OUT)
$(OUT): $(OBJ)
    $(CC) $(OPT) $(LIB) -o $@ $<
$(OBJ): $(SRC)
    $(CC) $(OPT) $(INC) -c $< -o $@
 
-include $(DEP)
 
 
depend:
%.d:%.cpp
    @set -e; rm -f $@; \
    gcc -M $(INC) $< > $@.$$$$; \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    rm -f $@.$$$$
 
 
clean: 
    \rm -rf *.o $(OUT) $(DEP) core.*

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.