我是 C++ 新手。我在包含标题时遇到问题。我正在尝试编译我的 a.cpp 文件并收到此错误。我使用 Sublime Text 作为文本编辑器。谁能帮我解决这个问题?先感谢您!
Undefined symbols for architecture arm64:
"print()", referenced from:
_main in a-3ccfb2.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的文件:
/* Filename: a.cpp */
#include <iostream>
#include "b.h"
using namespace std;
int main() {
print();
return 0;
}
/* Filename: b.h */
#ifndef _b_h
#define _b_h
void print();
#endif
/* Filename: b.cpp */
#include <iostream>
#include "b.h"
using namespace std;
void print() {
cout << "Hello, world!" << endl;
}
我用以下方式编译我的cpp:(这工作正常,除非我尝试包含我的头文件 - 例如:#include“bh”)
g++ -std=c++17 -Wall a.cpp -o a
g++ --版本
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin