C++라이브러리를 C app에서 사용하기
글쓴이: papablue / 작성시간: 월, 2008/10/06 - 11:46오전
C++라이브러리를 아래와 같이 만들었습니다.
ar ruv ./libTestLibrary.a ./a.o ./b.o ./c.o
그리고 Test Program인 C 프로그램에서 library의 함수를 사용하고 싶은데
C++로 되어 있어 C++의 함수를 제대로 이용할 수가 없습니다.
gcc -o ./test ./tester.c ./libTestLibrary.a
질문) C프로그램에서 C++라이브러리를 문제없이 사용하는 방법을 알고 싶습니다.
(libray는 직접 제작한 것으로 수정이 가능합니다)
extern "C"로 해도 제대로 안됩니다.
가르침을 주세요~
Forums:
제가 알기로는
제가 알기로는 C++에서는 C로 작성된 라이브러리를 사용할 수 있지만,
C에서는 C++로 작성된 라이브러리를 사용할 수 없는 것으로 알고 있습니다.
혹시 제가 잘못 알고 있는건가요?
calling convention만
calling convention만 맞춰주면 쓸수 있을 겁니다.
저 역시 잘못알고 있는지 모르겠습니다만..
extern "C" 로 뽑은 C
extern "C" 로 뽑은 C 형태의 함수 calling convention 으로만 사용이 가능합니다. ( 함수 이름이 unqiue 해야하며, 인자의 기본값등이 정해질 수 없습니다. 적어도 C99 까진default argument value 의 사용은 불가능하니까요. )
정확히 안되시는 소스코드를 올려보시는게 좋을 듯 합니다.
함수 선언의 문제뿐만아니라, __cdecl , __stdcall , __thiscall 이라던가 하는 녀석들의 문제일 가능성도 있거든요.
또 함수 내부적으로 class 를 쓰는건 관계없습니다만, argument가 class 라던가 하면 안됩니다.( C 에서 argument로 class 를 넘길리는 물론 없겠습니다만, 함수 선언에 그런 것을 포함하면 곤란하게 됩니다. )
Neogeo - Future is Now.
Neogeo - Future is Now.
source code는 다음과 같습니다.
--- library program (C++ source) testCpp.cpp -------
--- test program (C source) testCppApp.c ------
--
물론 library에는 다른 C++ code들이 많이 있습니다.
실제로 call이 되어지는 library의 function만 발췌한 것입니다.
gcc -o ./testCppApp ./testCppApp.c ./libtestCpp.a
이와 같이 하면
../testCpp/ppc/a-be/libtestCpp.a(testCpp.o): In function `__static_initialization_and_destruction_0(int, int)':
testCpp.o(.text+0xa0): undefined reference to `std::_Winit::~_Winit()'
testCpp.o(.text+0xa0): relocation truncated to fit: R_PPC_REL24 std::_Winit::~_Winit()
testCpp.o(.text+0xac): undefined reference to `std::ios_base::Init::~Init()'
testCpp.o(.text+0xac): relocation truncated to fit: R_PPC_REL24 std::ios_base::Init::~Init()
testCpp.o(.text+0xb4): undefined reference to `std::ios_base::Init::Init()'
testCpp.o(.text+0xb4): relocation truncated to fit: R_PPC_REL24 std::ios_base::Init::Init()
testCpp.o(.text+0xc0): undefined reference to `std::_Winit::_Winit()'
testCpp.o(.text+0xc0): relocation truncated to fit: R_PPC_REL24 std::_Winit::_Winit()
../testCpp/ppc/a-be/libtestCpp.a(testCpp.o)(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
cc: C:/QNX630/host/win32/x86/usr/bin/powerpc-unknown-nto-qnx6.3.0-ld caught signal 1
와 같은 error가 발생합니다.
가르침을 주시기 바랍니다~ ^^
C++ 정적
C++ 정적 라이브러리를 쓰신다면 링크 단계에서 C++ 표준 라이브러리도 같이 링크를 해주셔야 합니다.
gcc 대신 g++ 로 컴파일해보세요.
from nightknight
감사합니다
링크옵션에 C++ 옵션을 주니 해결되었습니다.
감사드립니다~
댓글 달기