【问题标题】:How to compile with libquantum library如何使用 libquantum 库进行编译
【发布时间】:2021-08-18 12:37:51
【问题描述】:

我目前正在学习这个名为 libquantum 的量子计算 C/C++ 库。在执行成功安装并通过尝试源代码附带的一些演示进行确认后,我尝试编写自己的程序 (ma​​in.cpp)。

#include<quantum.h>
 
 int main(){
         quantum_reg qr;
         quantum_delete_qureg(&qr);
}

我试图编译这个程序:

g++ main.cpp -lquantum

但它失败了:

/usr/bin/ld: /tmp/cc6aR9vu.o: 在函数main': main.cpp:(.text+0x23): undefined reference to quantum_delete_qureg(quantum_reg_struct*)' collect2: 错误: ld 返回 1 个退出状态

我也试过了:

g++ main.cpp -L/usr/local/lib -lquantum

考虑 libquantum.so 存在,但同样的错误

这里发生了什么?

【问题讨论】:

    标签: c++ gcc makefile compiler-errors linker


    【解决方案1】:

    该库是 C 语言,似乎不了解 C++。链接错误中有一个类型,这表明链接器正在尝试以 C++ 模式链接它。

    尝试在 include 周围加上 extern "C"

     extern "C" {
         #include<quantum.h>
     }
    
     int main(){
             quantum_reg qr;
             quantum_delete_qureg(&qr);
    }
    

    发生了什么事?为了实现函数重载,C++ 对函数符号名称中的类型信息进行了编码,因此它们不再与二进制库中的 C 函数名称匹配,请参阅https://en.wikipedia.org/wiki/Name_mangling

    【讨论】:

    • 谢谢,这很有效,这是我第一次意识到这种 C/C++ 库的差异。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    相关资源
    最近更新 更多