加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

编写/使用C库

发布时间:2020-12-16 05:50:44 所属栏目:百科 来源:网络整理
导读:我正在寻找基本的例子/教程: 如何在C(.so文件为Linux,.dll文件为Windows)中编写/编译库. 如何在其他代码中导入和使用这些库. 解决方法 代码 r.cc: #include "t.h"int main(){ f(); return 0;} t.h: void f(); t.cc: #includeiostream#include "t.h" void
我正在寻找基本的例子/教程:

>如何在C(.so文件为Linux,.dll文件为Windows)中编写/编译库.
>如何在其他代码中导入和使用这些库.

解决方法

代码

r.cc:

#include "t.h"

int main()
{
    f();
    return 0;
}

t.h:

void f();

t.cc:

#include<iostream>
#include "t.h"    

void f()
{
    std::cout << "OH HAI.  I'M F." << std::endl;
}

但是怎么样,怎么样呢?

~$g++ -fpic -c t.cc          # get t.o
~$g++ -shared -o t.so t.o    # get t.so
~$export LD_LIBRARY_PATH="." # make sure t.so is found when dynamically linked
~$g++ r.cc t.so              # get an executable

如果在全局库路径中的某个地方安装共享库,则不需要导出步骤.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读