C++라이브러리를 C app에서 사용하기

papablue의 이미지


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"로 해도 제대로 안됩니다.

가르침을 주세요~

ktd2004의 이미지

제가 알기로는 C++에서는 C로 작성된 라이브러리를 사용할 수 있지만,
C에서는 C++로 작성된 라이브러리를 사용할 수 없는 것으로 알고 있습니다.

혹시 제가 잘못 알고 있는건가요?

klenui의 이미지

calling convention만 맞춰주면 쓸수 있을 겁니다.
저 역시 잘못알고 있는지 모르겠습니다만..

neogeo의 이미지

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.

papablue의 이미지

--- library program (C++ source) testCpp.cpp -------

#include "cstdlib.h"
#include "iostream.h"
#include "stdio.h"
#include "errno.h"
#include "stdlib.h"
 
extern "C"
int lib_program(char * szProgMode , char * filename)
{
	printf("TestApp \n");
	return 0;
}

--- test program (C source) testCppApp.c ------

#include "stdlib.h"
#include "stdio.h"
 
int main(int argc, char *argv[]) {
	int res = 0;
 
	printf("call library function\n");
	res = lib_program(argv[1], argv[2]);
 
	return res;
}

--
물론 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가 발생합니다.

가르침을 주시기 바랍니다~ ^^

NK의 이미지

C++ 정적 라이브러리를 쓰신다면 링크 단계에서 C++ 표준 라이브러리도 같이 링크를 해주셔야 합니다.
gcc 대신 g++ 로 컴파일해보세요.

from nightknight

papablue의 이미지

링크옵션에 C++ 옵션을 주니 해결되었습니다.
감사드립니다~

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.