简体   繁体   中英

i cant get my program to work i keep getting undefined symbol: c

I'm trying to load all the libraries, and call a function from each one, to create a file and shate the fiddle thru the pointer, and write stuff in the main program with them, the close them.

This is my main function:

#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;

char LIBA[]="./LIBA.SO";
char LIBB[]="./LIBB.SO";
char LIBC[]="./LIBC.SO";
typedef void (*FUNC_T)(int*);
FUNC_T FUNC[2];

void ifnull(void *detect)
{
    if (detect == NULL)
    {
        cout << "ERROR:" << dlerror() << endl;
        exit(-1);    
    }
}

void ifnull2(FUNC_T detect)
{
    if (detect == NULL)
    {
        cout << "ERROR:" << dlerror() << endl;
       exit(-1);    
    }
}

int main()
{
    void *handle[2];
    int FD[2];
    handle[0]=dlopen(LIBA, RTLD_LAZY); ifnull(handle[0]);
    handle[1]=dlopen(LIBB, RTLD_NOW); ifnull(handle[1]);
    handle[2]=dlopen(LIBC, RTLD_NOW); ifnull(handle[2]);        
    FUNC[0]=(void(*)(int*))dlsym(handle[0], "c"); ifnull2(FUNC[0]);
    FUNC[1]=(void(*)(int*))dlsym(handle[1], "c"); ifnull2(FUNC[1]);
    FUNC[2]=(void(*)(int*))dlsym(handle[2], "c"); ifnull2(FUNC[2]);

    FUNC[0](&FD[0]); FUNC[1](&FD[1]); FUNC[2](&FD[2]);

    return 0;
}

This is inside the libs:

#include <stdio.h>

void c(int *fd)
{
    printf("ok A\n");
}

I keep getting

ERROR:./LIBA.SO: undefined symbol: c

please help

C++ mungs function names because it can have multiple functions with the same name.

So, for

#include <stdio.h>

void c(int *fd)
{
    printf("ok A\n");
}

we get

$ g++ -Wall -Wextra -pedantic -c -fPIC liba.cpp -o liba.so
liba.cpp: In function ‘void c(int*)’:
liba.cpp:3:13: warning: unused parameter ‘fd’ [-Wunused-parameter]
 void c(int *fd)
             ^~

$ nm liba.so
                 U _GLOBAL_OFFSET_TABLE_
                 U puts
0000000000000000 T _Z1cPi

As you can see, the function exists as _Z1cPi . I'm not sure if that's safe to use. What you can do, however, is use extern "C" .

For

#include <stdio.h>

extern "C" void c(int *fd)
{
    printf("ok A\n");
}

we get

$ g++ -Wall -Wextra -pedantic -c -fPIC liba.cpp -o liba.so
liba.cpp: In function ‘void c(int*)’:
liba.cpp:3:24: warning: unused parameter ‘fd’ [-Wunused-parameter]
 extern "C" void c(int *fd)
                        ^~

$ nm liba.so
0000000000000000 T c
                 U _GLOBAL_OFFSET_TABLE_
                 U puts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM