64,281
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <dlfcn.h>
#include<stdio.h>
#include<mysql/mysql.h>
int main(int argc,char ** argv) {
using std::cout;
using std::cerr;
char * word = argv[1];
cout << "C++ dlopen demo\n\n";
// open the library
cout << "Opening hello.so...\n";
dlopen("/usr/lib/mysql/libmysqlclient.so",RTLD_LAZY);
void* handle = dlopen("./hello.so", RTLD_LAZY);
int (*hello)(char *str);
if (!handle) {
cerr << "Cannot open library: " << dlerror() << '\n';
return 1;
}
// load the symbol
cout << "Loading symbol hello...\n";
// typedef void (*hello_t)();
// reset errors
dlerror();
hello = (int (*)(char *)) dlsym(handle, "test");
const char *dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol 'hello': " << dlsym_error <<
'\n';
dlclose(handle);
return 1;
}
// use it to do the calculation
cout << "Calling hello...\n";
(*hello)(word);
// printf("%s\n",(*hello("ffff")));
// close the library
cout << "Closing library...\n";
dlclose(handle);
}
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <mysql/mysql.h>
#include <cstdlib>
extern "C" void hello() {
std::cout << "helo" << '\n';
}
extern "C" void test(char *str){
std::cout<<"test1"<<'\n';
MYSQL conn_ptr;
std::cout<<"test3"<<'\n';
mysql_init(&conn_ptr);
std::cout<<"test2"<<'\n';
if(mysql_real_connect(&conn_ptr,"127.0.0.1","root","","test",0,NULL,0)){
printf("success\n");
mysql_close(&conn_ptr);
}
else {
fprintf(stderr,"error");
if(mysql_errno(&conn_ptr)){
fprintf(stderr,"%d : %s",mysql_errno(&conn_ptr),mysql_error(&conn_ptr));
}
printf("connection failed\n");
}
mysql_close(&conn_ptr);
//return EXIT_SUCCESS;
}