K.Y*_*glu -1 c++ compiler-errors linker-errors undefined-reference
我的项目中有2个cpp和3个头文件.当我在VS中编译它时,它运行顺利,我没有收到错误消息.但是当我尝试通过这条线在SSH网络上编译时:
g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
Run Code Online (Sandbox Code Playgroud)
它说:
In function `_start':
(.text+0x20): undefined reference to `main'
Run Code Online (Sandbox Code Playgroud)
不要说"别忘了写主函数",因为它已经存在并且我的项目在VS上工作.该怎么办?
这是我的代码中的相关部分.Program.cpp直到main:
#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;
}
Run Code Online (Sandbox Code Playgroud)
它继续,但我猜没有必要粘贴它们.如果你需要查看其他cpp文件或头文件,我也会粘贴它们.
命令:
g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
Run Code Online (Sandbox Code Playgroud)
告诉g++编译和链接文件:
lineoperations.cpp customer.h transaction.h lineoperations.h
Run Code Online (Sandbox Code Playgroud)
并输出一个名为的可执行程序program.cpp.
由于您没有编译或链接,因此main定义了链接错误,因此失败了
program.cpp.
试试这个:
g++ -o program program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
Run Code Online (Sandbox Code Playgroud)
或者如果你在Windows上:
g++ -o program.exe program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
Run Code Online (Sandbox Code Playgroud)
而且,顺便说一句,没有必要在命令行上列出头文件.我认为它们包含在源文件中.