编译器看不到我的主要功能(未定义对主要的引用)

Compiler doesn't see my main function ( Undefined reference to main)

我的项目中有 2 个 cpp 和 3 个头文件。当我在 VS 中编译它们时,它运行顺利并且没有收到任何错误消息。但是当我尝试通过这一行在 SSH 网络上编译它时:

g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h

它说:

In function `_start':
(.text+0x20): undefined reference to `main'

不要说 "do not forget to write main function" 因为它已经存在并且我的项目在 VS 上运行。那怎么办?

这是我代码中的相关部分。 Program.cpp 直到主要:

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include "lineoperations.h"
using namespace std;

line bankline;
bool operate(char);
void search(char[]);
void add(char[]);
void removee(char[]);
void transaction();
void printline();

int main(){
    bankline.create();
    bool end = false;
    while (!end) {
        end = bankline.decideFunction();
    }
    bankline.close();
    return EXIT_SUCCESS;
}

它还在继续,但我想没有必要粘贴它们。如果您需要查看其他 cpp 文件或头文件,我也会粘贴它们。

命令:

g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h

告诉g++编译和link文件:

lineoperations.cpp customer.h transaction.h lineoperations.h

并输出一个名为program.cpp.

的可执行程序

这会失败并出现您观察到的 linkage 错误,因为 mainprogram.cpp 中定义,您没有编译或 linking。

试试这个:

g++ -o program program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h

或者如果您在 Windows:

g++ -o program.exe program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h

顺便说一句,无需在命令行中列出头文件。它们包括在 源文件,我想。