推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wsds
V2EX  ›  Python

Python 或 Java 调用 C 中动态库的函数,均提示函数 undefined symbol: xxx

  •  
  •   wsds · May 31, 2018 · 2576 views
    This topic created in 2916 days ago, the information mentioned may be changed or developed.

    有个奇怪的现象, 我要调用 c 里边的 add(),不知道是调用方法不对,还是怎么的。两种语言调用均提示函数找不到undefined symbol

    如下代码。(函数是在libso.so中,libso.so依赖于libso1.solibso2.so

    但是!! 但是!!!

    我命令行执行: nm -D libso.so 显示的函数名是这种:_ZN11add4InERKSs

    我用这个看似乱码的函数名_ZN11add4InERKSs调用,居然调用成功了!!!而且用这个函数名 javapython 均能调用成功,这是什么原因???求解释

    import com.sun.jna.Library;  
    import com.sun.jna.Native;  
      
    public class Java_call_c {  
    
     
    
        public interface CLibrary extends Library {
    
            CLibrary INSTANCE = (CLibrary)Native.loadLibrary("so1",CLibrary.class);
    	CLibrary INSTANCE1 = (CLibrary)Native.loadLibrary("so2",CLibrary.class); 
            CLibrary INSTANCE2 = (CLibrary)Native.loadLibrary("so",CLibrary.class);  
    	      
    	    int add();
        }  
    
    
        public static void main(String[] args) {
    
        	
        	System.out.println(CLibrary.INSTANCE2.add());
    
    	       
        }  
    }  
    
    
    5 replies    2018-05-31 18:48:05 +08:00
    enenaaa
        1
    enenaaa  
       May 31, 2018
    这个 so 是用 c++写的吧。
    函数声明加上 extern “ C ”
    wsds
        2
    wsds  
    OP
       May 31, 2018
    @enenaaa 加了
    enenaaa
        3
    enenaaa  
       May 31, 2018
    这种函数名符号是 C++的 ABI 样式。 加了 extern "C" 还是这种的话,说明没加对。
    wsds
        4
    wsds  
    OP
       May 31, 2018
    @enenaaa 重新打包了一下可以了!!谢谢各位
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1124 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 17:59 · PVG 01:59 · LAX 10:59 · JFK 13:59
    ♥ Do have faith in what you're doing.