以前作ったサンプル。
$ make
g++ -fPIC mydll.cpp -c -o mydll.o
g++ -shared -o libmydll.so.3 mydll.o
g++ -o main main.cpp ./libmydll.so.3
$ ./main
test ok.
get
test::get
put
test::put
ee
---make--------------------------------
comp:
g++ -fPIC mydll.cpp -c -o mydll.o
g++ -shared -o libmydll.so.3 mydll.o
g++ -o main main.cpp ./libmydll.so.3
# or
# libmydll.so.3 -> lib***.so**
# g++ -o main main.cpp -lmydll
# when /usr/lib/libmydll.so.3
#
compc:
g++ -fPIC mydll.cpp -c -o mydll.o
g++ -shared -o libmydll.so.3 mydll.o
gcc -o mainc mainc.c ./libmydll.so.3
# or
# libmydll.so.3 -> lib***.so**
# g++ -o main main.cpp -lmydll
# when /usr/lib/libmydll.so.3
#
clean:
rm -f *.so.* main *.o
---main.cpp--------------------------------
#include <stdio.h>
extern "C" {
//typedef struct _MEMBER{
extern void testfunc();
extern void get(char* str);
extern char *put();
//}MEMBER;
}
//MEMBER mem;
int main() {
testfunc();
get("ee");
printf("%s\n", put());
return 0;
}
---mydll.cpp--------------------------------
#include <stdio.h>
#include <string.h>
extern "C" {
void testfunc();
void get(char* str);
char *put();
}
class test{
private:
char buf[1024];
public:
test(){
//buf=0;
}
~test(){}
void get(char* str);
char* put();
};
void test::get(char* str){
printf("test::get\n");
strcpy(buf,str);
}
char* test::put(){
printf("test::put\n");
return buf;
}
test tes;
void testfunc() {
printf("test ok.\n");
}
void get(char* str) {
printf("get\n");
tes.get(str);
}
char* put() {
printf("put\n");
return tes.put();
}
PR