c++ - 动态库C++基础实践
问题描述
我正在尝试动态链接测试。
mylib.cpp
#include<iostream>
#include<stdio.h>
using namespace std;
int hello(){
cout<<"Hello,World!"<<endl;
return 1;
}
然后编译
g++ -shared -o libmylib.so mylib.cpp
现在出现了libmylib.so
test.cpp
#include <iostream>
int hello();
int main() {
hello();
std::cout << "Test Finish!\n";
return 0;
}
尝试用这个编译,
g++ -o test test.cpp -L ./
错误来了
Undefined symbols for architecture x86_64:
"hello()", referenced from:
_main in test-37bd2a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我需要添加一些选项还是我的源代码有问题?
谢谢您的帮助。
解决方案
我需要添加一些选项吗
是的,与图书馆链接。
g++ ... -lmylib
推荐阅读
- python - Python for 循环在 lambda 中创建 100 x[i]
- r - ggplot2和一组抖动/闪避点
- python - 使用python获取子目录列表中的文件名
- javascript - 如何从 SXSSFWorkbook 创建 PIVOT 表 - APACHE POI
- javascript - 如何跨浏览器一致地导出 csv 数据?
- docker - Docker 撰写 yml 替代品
- python - Django Forms.py 将 EmailField 值更改为自动填充 value="{{ user.email }}"
- reactjs - React 在状态更改后重新渲染子组件
- c - 没有数据发送时如何跳过 recv() 函数
- javascript - 使用 WebUSB 从串行设备读取整个响应