I have a problem in C++.
I've created a function called execute
int* execute(int tab[], int n)
{
for (int i = 0; i<n; i++)
{
for (int j = 0; j < n-1; j++)
{
if (tab[j] > tab[j+1])
{
int tmp = tab[j];
tab[j] = tab[j+1];
tab[j+1] = tmp;
}
}
}
return tab;
}
So, this is simple BubbleSort function. I have this function in file BubbleSortAlgorithm.cpp.
So, in main function of my program, I check if libBubbleSortAlgorithm.so exist. If not, then I must create this lib. This lib is created via popen. So I've ended up with file libBubbleSortAlgorithm.so. If I run command
nm libBubbleSortAlgorithm.so | c++filt
then I get something like this.
0000000000000ed0 T execute(int*, int)
U dyld_stub_binder
I presume this is ok. So, next in main program, I load this .so file in my program with dlopen and call this function like this
void *handle = dlopen(buff, RTLD_LAZY);
if (handle)
{
execute = dlsym(handle, "execute");
int tab[5] = { 5, 2, 4, 7, 1 };
int x = 5;
execute(tab, x);
}
But before main I've also wrote this
#ifdef __cplusplus
extern "C" {
#endif
void (*execute)(int*, int);
#ifdef __cplusplus
}
#endif
So, in Xcode7, I get this error: /Users/Tadej/Documents/Development/ALGatorC_final/ALGatorC/ALGatorC/main.cpp:96:49: Assigning to 'void (*)(int *, int)' from incompatible type 'void *'
Thank you in advance for help.
Regards, golobich
Edit: I've changed code like this:
#ifdef __cplusplus
extern "C" {
#endif
int* (*execute)(int*, int);
#ifdef __cplusplus
}
#endif
execute = (int*(*)(int*, int))dlsym(handle, "execute");
int tab[5] = { 5, 2, 4, 7, 1 };
int x = 5;
int *xs = execute(tab, x);
for (int i = 0; i<5; i++)
{
std::cout << xs[i] << ", ";
}
So, now, I have problem at runtime. At execute(tab, x), Xcode complain and say this: EXC_BAD_ACCESS(code=1, address=0x0). So, problem is that execute is NULL. Any help? :)