-1

I have 2 cpp and 3 header files in my project. When I compile them in VS it works smoothly and I get no error message. But when I try to compile it on SSH network by this line:

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

It says:

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

Do not say "do not forget to write main function" because it is already there and my project works on VS. What to do then?

Here is related part from my codes. Program.cpp until 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;
}

It goes on but it is not necessary to paste them I guess. If you need to see other cpp file or header files I'll paste them as well.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
K.Yazoglu
  • 207
  • 3
  • 13

1 Answers1

5

The command:

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

tells g++ to compile and link the files:

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

and output an executable program called program.cpp.

This fails with the linkage error you have observed because main is defined in program.cpp, which you are not compiling or linking.

Try this instead:

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

or if you are on Windows:

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

And BTW, there is no need to list the header files on the commandline. They are included by the source files, I presume.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • this helped me. I got rid of undef. ref. error. I have bunch of "multiple definiton" errors right now but this is not related to this post I guess. Thanks! By the way, the source of these multiple definiton errors, can it be because we declared program twice? program and program.cpp ?? – K.Yazoglu Nov 06 '15 at 19:21
  • 1
    It's technically possible that he wants to compile the .h files as separate translation units, but it's very weird and goes against c++ file naming convention. In the end, it's either unnecessary or outright wrong. – SirGuy Nov 06 '15 at 19:22
  • @K.Yazoglu You have not "declared program twice". Your multiple definition errors are an unrelated problem which you may wish to make the subject of another question. You may also wish to [accept](http://stackoverflow.com/help/accepted-answer) my answer to this one. – Mike Kinghan Nov 06 '15 at 19:29
  • @MikeKinghan Recommend an edit to warn OP of the dangers of compiling, as opposed to including, header files. – user4581301 Nov 06 '15 at 19:58