c++ - 如何修复错误 "clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)"?

标签 c++ gcc compilation clang++

编译包含 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/

相关文章:

c - fftw 无法正确处理真实数据

gcc - C/C++ 到 MIPS 汇编

java - 当一个类改变时我需要编译哪些类?

c++ - 有没有一种方法可以禁用临时对象参数的功能?

c++ - OpenMP atomic 是否应用于行、变量名或实际内存地址?

c++ - 尝试实现相机

c - 共享库 (.so) 如何调用在其加载程序代码中实现的函数?

c++ - vector::erase 是否会对 vector 中的元素重新排序?

c++ - 如何在代码块下使用pqxx解决这个编译错误

c - 为 C 程序设置默认 NetBeans 选项(-std=c99、-Wall)