1

I am a very new to programming and have a very basic question that may be answered in other threads however I think they are far too advanced for me to understand how. I have actually found many answers so far on this site but this is the first problem that forced me to create an account and ask. Anyway i am running a very basic example program on linux mint 18.3. Now I have seen this exact code work on a machine with windows 8 I believe so I was wondering if that could be the problem. I have created a class and when i plug in my object then build and run I get:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o||In function _start':| (.text+0x20)||undefined reference tomain'|

This is the entire code:

#include <iostream>
#include "Gladius.h"
using namespace std;

int main()
{
    Gladius io;
    return 0;
}

Thats it very basic. here is the .h

#ifndef GLADIUS_H
#define GLADIUS_H


class Gladius
{
    public:
        Gladius();

};

#endif // GLADIUS_H

and the .cpp for the class.

#include "Gladius.h"
#include <iostream>

using namespace std;
Gladius::Gladius()
{
    cout << "The Gladius is a short sword" << endl;
}

I know this seems extremely simple but I am just learning to code and i have been looking all over for an explanation why this isn't working yet I see it work on another pc exactly as is. Anyway any explanation would be greatly appreciated.

Here is what i found in command line If this answers your questions about what was in the cmd.

g++ -Wall -fexceptions -g -std=c++11 -Wall -I -c /home/gator/Documents/Spartan1/Gladius.cpp -o obj/Debug/Gladius.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function _start': (.text+0x20): undefined reference tomain' collect2: error: ld returned 1 exit status

Seaxneat
  • 11
  • 1
  • 1
  • 3
  • Nothing is wrong with this code. I would rather seek inconsistency in compiler/IDE setup. Have this happened to you before? Which compiler (and it's version) exactly are you using? Edit: or probably you have not linked file, with `main` implementation. – Poeta Kodu Feb 23 '18 at 02:48
  • It's a problem with makefile\build steps. should build steps, please – Swift - Friday Pie Feb 23 '18 at 02:52
  • I'm using GNU GCC compiler from code::blocks I just got the latest version I believe. – Seaxneat Feb 23 '18 at 03:21

6 Answers6

4

Know the compiler options(gcc/g++ compiler):

  • -c : Compile and assemble, but do not link
  • -o file : Place the output into file

So when you run

g++ filename.cpp -o executable_name

, you generate an application which can be executed.

The problem is you are compiling, assembling as well as linking when you are trying to compile "Gladius.cpp" and compiler is trying to search for main() definition.

So in your case, the compilation steps would be:

First compile "Gladius.cpp" and generate object file "Gladius.o":

g++ -Wall -fexceptions -g -std=c++11 -c Gladius.cpp

Next compile "main.cpp" and generate object file "main.o":

g++ -Wall -fexceptions -g -std=c++11 -c main.cpp

Generate executable by linking "main.o" and "Gladius.o"

 g++ -Wall -fexceptions -g -std=c++11 -o main main.o Gladius.o

Now you can run "main":

./main
Vishal-L
  • 1,307
  • 1
  • 9
  • 16
4

Your compiler's command line contains -I -c sequence.

This -I option "swallows" your -c option. -I requires an additional argument, which is an include directory name. You failed to supply that argument, which is why -I assumes that -c that follows it is the directory name. So that -I consumes that -c.

The compiler never sees that -c. Without -c it assumes that you want to compile and link your program. Since Gladius.cpp does not have main in it, you get the error at linking stage.

Here 's a simple demo of the same problem: http://coliru.stacked-crooked.com/a/8a37cd3e90a443e2

You need to figure out why you have an orphaned -I in your command line.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
3

If you are compiling this code using a command line like:

g++ -Wall -Wextra -Werror -O gladius.cpp -o output.exe

then make sure that you include all the .cpp files (not .h files) that contain code that your program needs.

g++ -Wall -Wextra -Werror -O gladius.cpp main.cpp -o output.exe

I explain this to beginners all the time as each .cpp being a bag of Lego's in a kit. You need all the bags that came with the box in order to build the kit. If you omitted main.cpp (or the file that contains main) then you will get the linker error that you are currently getting.

  • Thank u for the answers everyone wow This is really showing me how much of a newb I really am. I'm using code::clocks. How exzactly do I view My compiler commands. – Seaxneat Feb 23 '18 at 03:12
  • Btw I really do appreciate the feedback even though its a little over my head I'm confident I can figure it out. How exzactly does 1 view or change the code in the command line. Do I have to do it from a terminal in linux or can I do it right from code::blocks – Seaxneat Feb 23 '18 at 03:25
  • I believe u are def right however for some reason if I build the gladius.cpp or the main.cpp They both arnt appearing in the build log. I just can't figure out why they won't link. Should I manualy change the command? I'm nervous to do that because it says only do this if u really know what u are doing because u can destroy your compiler. I def do not feel confident just changing it unless I know I'm changing the correct thing – Seaxneat Feb 23 '18 at 04:07
0

What command are you using to compile, link, and then execute? It should look something like

$ g++ main.cpp gladius.cpp -odemo

$ ./demo

Lee Sailer
  • 39
  • 2
  • 1
    Well, I think you should not add questions inside your answer. There is comment section below his question. – Poeta Kodu Feb 23 '18 at 02:51
  • Sorry I am realizing how much of a newb I am to programming. I really dont know how to see what command I'm using to complie. The program I'm using is code::blocks – Seaxneat Feb 23 '18 at 03:15
0

check your command line for linking step.. You may forgot file with main as input, or you had forgot output file name after -o (and masked main.o in result)

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
-2

I had this very kind of problem myself, and though it may not be the conventional, "proper" solution, I simply renamed the ".c" file to ".cpp", and it all worked.

After all, I was compiling both c and c++ together with a c++ compiler (recommended by the library), and the c code already had the proper c++ #extern flags (see here for more on that).

Also related:

Andrew
  • 5,839
  • 1
  • 51
  • 72