C++错误 - 体系结构的未定义符号 x86_64:
C++ error - Undefined symbols for architecture x86_64:
我正在学习C++,并且有一个简单的Date类,我正在尝试为Date设置一个值。
以下是源代码文件 -
日期.h
class Date{
private:
int month;
int day;
int year;
public:
Date();
void setDate(int m, int d, int y);
};
和日期.cpp
#include "Date.h"
Date::Date()
{
month = 1;
day = 1;
year = 80;
};
void Date :: setDate(int m1, int d1, int y1){
month = m1;
day = d1;
year = y1;
};
但是,当我编译代码时,我收到错误消息 -
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
有人可以帮忙吗?
谢谢
你缺少一个主函数。 将其添加到新文件(如 main.cpp,并在编译和链接时将其包含在)或其他 .cpp 文件中。
int main(int argc, char *argv[]) { }
并将您的代码放在大括号中以运行程序。
每个 C/C++ 程序都必须有一个 main
函数,该函数无条件地用作程序执行的入口点。
int main(int argc, char** argv) {
Date d;
d.setDate(11, 19, 1984);
/* do something with this date... */
return 0;
}
一个常见的约定是将其放在 main.cc
/main.cpp
中,并确保在这种情况下,main.cpp
和 Date.cpp
都被编译并链接到同一个目标二进制文件中。 如果无法解析main(int, char**)
,链接器将无法继续。 如果这对您来说很明显,那么我只需要求您检查链接器命令行以确保包含包含main
的源/对象文件。
此外,随机C++最佳实践准则:您应该有一个非默认构造函数,该构造函数采用setDate
执行的参数,并通过初始值设定项列表将它们分配给成员变量。在这种情况下,默认构造函数(无参数)对于具体日期类没有意义。
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- VC++本机单元测试,找不到调试符号
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为x86而非x64编译时出错
- 检查TCHAR数组输入是否为带符号整数C++
- C++如何仅使用MOV在x86上实现发布和获取
- 用符号版本替换对函数的所有调用
- 在 x86-64 平台上为 C(++) 中的 64 位无符号参数计算 (a*b)%n FAST
- 为什么WinDbg无法通过Service Pack 2 x86零售符号下载从Windows Server 2003加载符