Makefile 질문드립니다. .so 라이브러리
글쓴이: idrukawa / 작성시간: 목, 2012/04/26 - 3:54오후
.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)
왜 이런지, 공유라이브러리를 만드는 방법이 무엇이 잘못되었는지 알려주세요~
Forums:
음 ..
음.. 별 문제는 없어 보이는데..;; 컴파일러 버전이 어찌 되시는지..?
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 』
] gcc -v Reading specs from
] 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
입니다.
] g++ -v Reading specs from
] 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++ 버전입니다.
음 ..
생각보다 버전이 낮네요..;;
소스 파일에 대충 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 』
음 ..
삽질했네요.
4.4.5 시스템에서만 특이하게 -fPIC 을 요구하지 않은 것이었는데 착각했습니다.
CXXFLAGS 로 컴파일 옵션을 넣어주는게 맞습니다. (아래 태훈님 댓글 참조)
오브젝트 파일은 묵시적 타겟으로도 생성되기 때문에, 이 변수들만 맞춰주면 굳이 생성하지 않아도 상관 없구요.
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
오브젝트 파일 생성 gcc 옵션 문제
오브젝트($(OBJ)) 생성하실때 -fPIC 옵션을 주시고, 오브젝트들을 공유 라이브러리로 링킹 할때는 -fPIC 옵션이 필요 없습니다.
위 Makefile에는 빠트리신건지 $(OBJ) 생성 규칙이 없으니, 예를들면
이런식으로 해주시면 됩니다.
Just do it!
cpp니까 %.c -> %.cpp $(OBJ):
cpp니까 %.c -> %.cpp
$(OBJ): $(SRC) 형태로 작성하시도 되고...
Anyway~
Just do it!
최종적으로 아래와 같이 수정하여 정상적으로
최종적으로 아래와 같이 수정하여 정상적으로 동적라이브러리가 생성되는 것을 확인하였습니다.
도움 주신분들 감사합니다.
댓글 달기