【发布时间】:2021-04-26 10:57:32
【问题描述】:
我正在学习 C++20 模块功能,但每当我尝试在模块中包含头文件然后尝试链接它时,都会出现链接错误。我在使用 GLFW 库时遇到了错误,但我做了一个可重现的最小示例,以便您可以更轻松地帮助我。
以下是重现此问题的步骤:
- 获取带有函数定义的
.a文件:
// lib.h
void libraryFunction();
// lib.cpp
#include "lib.h"
void libraryFunction() {}
clang++ -c -o lib.o lib.cpp
ar rc lib.a lib.o
- 现在您有了
lib.a文件,编写并编译一个使用它的模块:
// test.cpp
export module test;
#include "lib.h"
export void testFunction() {
libraryFunction();
}
clang++ -std=c++20 -c -fmodules-ts -Xclang -emit-module-interface -o test.pcm test.cpp
- 写一个
main.cpp文件并尝试编译和链接它:
clang++ -std=c++20 -fmodules-ts -fprebuilt-module-path=. -o main main.cpp test.cpp -l:lib.a -L.
- 你应该得到这个错误:
/usr/bin/ld: /tmp/test-25d7c8.o: in function `testFunction()':
test.cpp:(.text+0x5): undefined reference to `_ZW4testE15libraryFunctionv'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
我该如何解决这个问题?当我 #include "lib.h" 并在 main.cpp 中调用 libraryFunction() 时,我没有收到错误,但我想要的是从模块调用库 API。这可能吗?
【问题讨论】:
标签: c++ static-libraries linker-errors c++20 static-linking