1.api.h
#ifndef API_H_
#define API_H_
#include <vector>
#include <string>
void api1(std::string& str, std::vector<std::string>& vecofstr);
#endif
.2. api.cpp
#include "api.h"
#include <iostream>
void api1(std::string& str, std::vector<std::string>& vecofstr) {
std::cout << str << std::endl;
for (size_t i=0; i<vecofstr.size(); i++) {
std::cout << vecofstr[i] << std::endl;
}
}
3.wrapper.h
#ifndef WRAPPER_H_
#define WRAPPER_H_
#define SIZE 2
#ifdef __cplusplus
extern "C" {
#endif
extern void wrapper1(char* p, char* [SIZE]);
#ifdef __cplusplus
};
#endif
#endif
4.wrapper.cpp
#include <string>
#include "wrapper.h"
#include "api.h"
#ifdef __cplusplus
extern "C" {
#endif
void wrapper1(char* p, char* ps[SIZE]) {
std::string str(p);
std::vector<std::string> vecofstr;
for (size_t idx=0; idx<SIZE; idx++) {
vecofstr.push_back(ps[idx]);
}
api1(str, vecofstr);
}
#ifdef __cplusplus
};
#endif
.5. test.c
#include "wrapper.h"
int main(void)
{
char* p = "hello world";
char* ps[] = {"world", "hello"};
wrapper1(p, ps);
return 0;
}
.6. compile
gcc -c api.cpp wrapper.cpp
gcc test.c -o test wrapper.o api.o -lstdc++
.7. run
./test
hello world
world
hello
std::vectoris passed in as a copy...API1_Wrapper(const char* str, const char** vecofstr, size_t vecSize);extern "C" {}.