c++ - makefile 错误 lib/Scrt1.o 未定义对“main”的引用
问题描述
我浏览了大量类似的主题,但我无法识别解决方案
我尝试了我的和许多变化。即使低于最简单的代码编译也不起作用。我想跳过小事......你能帮帮我吗?
# Make file for test.c file dependencies external C libraries
CC = g++
C = gcc
FLAGS = -Wextra -g
INCLUDES = -lm
test: randomArray.o test.o
$(CC) $(FLAGS) $(INCLUDES) randomArray.o -o test
test.o: randomArray.o
$(C) $(FLAGS) $(INCLUDES) -c test.cpp
randomArray.o: randomArray.c
$(C) $(FLAGS) $(INCLUDES) -c randomArray.c
错误信息
make
g++ -Wextra -g -lm randomArray.o -o test
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [makefile:9: test] Error 1
#include <iostream>
#include "randomArray.h"
using namespace std;
int main(int argc, char const *argv[])
{
int *bit=randomArray(64);
for (int i = 0; i < 64; i++)
{
cout<<bit[i]<< "\n";
}
return 0;
}
解决方案
你忘了链接test.o
:
test: randomArray.o test.o
$(CC) $(FLAGS) $(INCLUDES) randomArray.o test.o -o test
另请注意,这test.o: randomArray.o
可能是错误的。它说test.o
取决于randomArray.o
,它没有。这取决于test.cpp
.
test.o: test.cpp
$(CC) $(FLAGS) $(INCLUDES) -c test.cpp
推荐阅读
- python - 在 Pytorch 中为模型注册参数的正确方法
- android - 华为In App Purchase沙盒测试,我已经购买过一次的商品无法购买
- qt - 在 qml 中将 a 添加到 Gradient Stop 时的奇怪行为
- google-cloud-platform - 使具有私有 IP 连接的 GCP Cloud SQL 等效于 Cloud SQL 代理
- vue.js - 在vue中将隐藏的div转换为pdf
- html - 使用 CSS 从表格的第二行替换行颜色
- python - 按条件列表刮取数据排序
- python - 如何多次打印存储在变量中的 map() 对象结果?
- python - 如何处理内存错误:用于做解码器
- c# - 到 Azure ACI 的基本容器化 ASP.NET 3.1 Core 应用部署失败