简体   繁体   English

我无法让我的程序工作我不断收到未定义的符号:c

[英]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. C++ mungs 函数名是因为它可以有多个同名的函数。

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 .如您所见,该函数以_Z1cPi存在。 I'm not sure if that's safe to use.我不确定这是否可以安全使用。 What you can do, however, is use extern "C" .但是,您可以做的是使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在 C 程序中链接我的 static 库时,为什么会出现“未定义符号”? - Why am I getting “undefined symbol” when I link my static library in my C program? C++ 我无法让我的 function 在我的程序中运行 - C++ I cant get my function to run in my program 我不断从cin.get()中获取程序空间 - I keep getting a space in my program from cin.get() 我无法让析构函数在我的代码中工作 - I cant get my destructor to work in my code 我正在为我祖母的生日编写 C++ 程序,但是我不断收到错误代码:错误 C2451 - I am working on a C++ program for my grandmother's birthday, however I keep getting error code: error C2451 为什么我的程序始终无法获取外部信息? - Why do I keep getting unresolved externals for my program? 在 fork() 之后,我的程序中不断获得相同的 pid - I keep getting the same pid in my program after fork() 为什么在编译 c++ 程序时出现“未定义的引用”错误? - Why am I get getting "undefined reference to" error when compiling a c++ program? 即使“用户”输入数字||,我在运行程序时仍会得到0。 C ++绘画工作 - I keep getting 0's when I run my program even though the “user” inputs numbers || c++ painting job 我无法让 GLEW 在 ubuntu 20.04 (C++) 中处理 netbeans - I cant get GLEW to work on netbeans in ubuntu 20.04 (C++)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM