클래스 및 템플릿을 사용하는 C++ 라이브러리 lib.h가 있다고 가정합니다.라이브러리와 c 및 C++ 연결
#include "lib.h"
//call methods from lib.h that use templates and classes
// and return an integer based off of the information gained from calling functions lib.h
extern "C" int foo(int param1, const int param2);
지금 나는 C 파일이라고 test.c.에서 오전 가정 : 나는 다음과 사용자 정의 C++ 헤더 myLink.h을 가지고 또한 가정 함수 foo()를 다음과 같이 호출하는 것이 합법적입니까?
//in test.c
int first = foo(5, 6);
또한 컴파일의 개체 코드/링커 단계에서 무엇이 진행되고 있습니까?
감사합니다.
"myLink.h"를 C 및 C++ 모두에서 컴파일 할 수 있도록 할 수 있습니다. '#ifdef __cplusplus'를 사용하여'extern "C"{'와'}'를 별도의 줄로 감싼다. 그렇게하면'int foo (int param1, const int param2); 부분이 일반 C 코드에서 볼 수 있고 나머지는 C++에서 볼 수 있습니다. 이것이 없으면 "test.c"는 함수를 선언하는 별도의 방법이 필요합니다. –