c++ - G++ 链接器忽略静态库
问题描述
包含标头,但不包含静态库 (influxdb)。
我的 tasks.json 文件:
{
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file (mt)",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": [
"-v",
"-LC:/influxdb-cxx/lib",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17",
"-lws2_32",
"-lInfluxDB"
],
"options": {
"cwd": "C:/msys64/mingw64/bin"
},
"problemMatcher": [
"$gcc"
]
},
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17"
],
"options": {
"cwd": "C:/msys64/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
],
"version": "2.0.0"
}
使用 -v 选项显示库路径和库名称被传递给链接器(-LC:/influxdb-cxx/lib 和 -lInfluxDB):
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/collect2.exe -plugin C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/liblto_plugin-0.dll -plugin-opt=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\Admin\AppData\Local\Temp\ccxBryht.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -m i386pep -Bdynamic -o c:/path/to/executable.exe C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/crtbegin.o -LC:/influxdb-cxx/lib -LC:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0 -LC:/msys64/mingw64/bin/../lib/gcc -LC:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib -LC:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../lib -LC:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib -LC:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../.. C:\Users\Admin\AppData\Local\Temp\ccjciO8C.o -lws2_32 -lInfluxDB -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/default-manifest.o C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/
在编译时,该库中的所有函数都出现“未定义引用”错误。但是链接器并没有说找不到该文件,所以它链接了库,我猜。该库文件包含所有函数。我已经使用 nm 进行了检查。
我也尝试过更改链接顺序,但没有帮助。
我的系统是Windows 7,编辑器是VS code,编译器是msys2 mingw64 g++。
库路径包含在 LIB 环境变量中,头文件路径包含在 INCLUDE 中,dll 路径包含在 Path 中。
解决方案
添加一个额外的引用-lInfluxDB
应该是命令行的末尾。我不知道如何在 tasks.json 中执行此操作。
我相信 GNU 链接器只转发解决依赖关系。即如果库 A 依赖于 B,则-lA
必须-lB
在命令行顺序中出现在前面。如果 A 和 B 都相互依赖,你可以做-lA -lB -lA
或阅读这个。
推荐阅读
- vue.js - 是否仍然可以同时使用 vuetify 和 vue-bootstrap?
- javascript - 在 d3 强制布局上从 svg 切换到画布
- c# -
用空格替换的 InnerText - android-gradle-plugin - MissingPropertyException:无法获取 SourceSet 容器的未知属性“测试”
- android - Android MediaCodec 异步解码/编码延迟取决于帧时序
- flutter - 何时将属性传递给状态或从有状态的 Widget 访问属性?
- excel - 从列中的所有 URL 检索信息
- java - 如何为 jpa 和 jdbc 数据源运行两个独立的事务管理器
- grails - Grails 和 Caasandra 支持版本
- r - 如何在我的绘图上排列按 Y 排序的 X 轴?