Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使应用程序符号对库可见?_C++_C_Linux_Gcc_Visibility - Fatal编程技术网

C++ 如何使应用程序符号对库可见?

C++ 如何使应用程序符号对库可见?,c++,c,linux,gcc,visibility,C++,C,Linux,Gcc,Visibility,我有一个应用程序,它用dlopen加载一个库,如下所示: #include <iostream> #include <dlfcn.h> void foo() { std::cout << "foo"; } int main() { void* libbar = dlopen("./libbar.so", RTLD_LAZY); if (!libbar) { std::cerr << dlerror

我有一个应用程序,它用
dlopen
加载一个库,如下所示:

#include <iostream>
#include <dlfcn.h>

void foo()
{
    std::cout << "foo";
}

int main()
{
    void* libbar = dlopen("./libbar.so", RTLD_LAZY);
    if (!libbar)
    {
        std::cerr << dlerror() << std::endl;
        return 1;
    }
    void(*bar)() = (void(*)())dlsym(libbar, "bar");
    if (!bar)
    {
        std::cerr << dlerror() << std::endl;
        return 1;
    }
    bar();
    dlclose(libbar); 
}
输出:

./libbar.so: undefined symbol: _Z3foov
预期产出:

foobar
如何使
foo
libbar
可见


我实际上使用C++,实际的问题是来自构造函数/成员函数的未定义的符号,但这应该非常相似。我正在使用GCC4.7在Linux上工作。

您应该编译main.cc并将其链接到

 g++ -rdynamic -Wall main.cc -o prog -ldl
该标志在链接时很重要

您最好声明
extern“C”
那些要传递给
dlsym
的函数


另请参见。

模块(库)之间的引用通常是单向的。我建议把foo作为一个论据传给酒吧是个好主意。
 g++ -rdynamic -Wall main.cc -o prog -ldl