gpt4 book ai didi

c++ - 编写/使用 C++ 库

转载 作者:可可西里 更新时间:2023-11-01 16:30:44 27 4
gpt4 key购买 nike

我正在寻找以下方面的基本示例/教程:

  1. 如何用 C++ 编写/编译库(Linux 为 .so 文件,Windows 为 .dll 文件)。

  2. 如何在其他代码中导入和使用这些库。

最佳答案

代码

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

如果您将共享库安装在全局库路径中的某处,则不需要 export 步骤。

关于c++ - 编写/使用 C++ 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com