I have the following c++ program:
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;
}
在XCode 4.3中编译以上内容,会生成一个名为静态库文件:的文件。
libClient.a
另外,我有一个 main.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
extern "C" void invoke_f1();
并进行导出。 - Anthony-lClient
而不是-lClient.a
来加载libClient.a
。 - C. K. Young