首页 > 解决方案 > 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;
}

标签: c++makefile

解决方案


你忘了链接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  

推荐阅读