나는 다음과 같은 C++ 프로그램이 있습니다C++ 정적 라이브러리를 C 프로그램에 어떻게 연결합니까?
:Client.h
#ifndef Client_Client_h
#define Client_Client_h
#include "Client.h"
class Client {
public:
void f1();
void f2();
};
#endif
Client.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;
#include "Client.h"
void Client::f1(){
cout << "Client.f1()" << endl;
}
void Client::f2() {
cout << "Client.f2()" << endl;
}
엑스 코드 4.3 위를 컴파일 내게라는 정적 라이브러리 파일을 제공합니다
libClient.a
별도로 메인 C가 있습니다
#include <stdio.h>
//
//using namespace std;
int main(){
// how do I do something like: Client c; c.f1(); c.f2();
// and actually get output ?
printf("hello\n");
return 0;
}
f1() 및 f2()를 호출하기 위해 수행해야 할 단계는 무엇입니까? GCC를 사용하여 정적 라이브러리를 올바르게 연결하려면 어떻게해야합니까?
은 지금까지 나는 시도했다 :
gcc -lClient.a main.c
것은 나에게주는이 작동하는 것, 또는되지
ld: library not found for -lClient.a
collect2: ld returned 1 exit status
대상 프로그램이 C++이되어야 할 수도 있습니다. –
C 프로그램에서 C++ 기능을 호출하는 영리한 방법이 없습니까? – user1068636
라이브러리 내부의 함수로 래핑 할 수 있습니다. 'extern "C"void invoke_f1();'그리고 그것을 내 보냅니다. – Anthony