编译包含 main.cpp、pattern.cpp 和 pattern.h 的 C++ 程序时(包含两个函数声明但没有类的头文件;这些函数在 pattern.cpp 中定义,并且 main.cpp 包含 #include "pattern .h”在顶部),输入:
clang++ main.cpp
错误消息是:
/tmp/cc-nrPup0.o: In function `main':
main.cpp:(.text+0x69): undefined reference to `pattern(int, int)'
collect2: ld returned 1 exit status
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
我该如何解决这个问题?我尝试输入 -v
但 clang 输出已终止,这是一个无效命令
最佳答案
看起来您只编译了 main.cpp
而没有编译 pattern.cpp
。当需要将可执行文件链接在一起时,将找不到 pattern.cpp
中定义的函数。 undefined reference 表明您的 main.cpp
正在某处使用 pattern(int, int)
。您可以看到为什么如果 pattern.cpp
从未被编译过,这会成为一个问题。
尝试编译:
clang++ -Wall -pedantic main.cpp pattern.cpp -o main
关于c++ - 如何修复错误 "clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18218673/