Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Basic C++ Class

Tags:

c++

g++

I'm trying to compile a very basic C++ program and there is something wrong. Whatever it is, I'm sure it's very obvious. I have three very short files.

main.cpp:

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

using namespace std;

int main()
{
    Player rob;
    cout << "Iran" << endl;
    return 0;
}

Player.h

#ifndef PLAYER_H
#define PLAYER_H

class Player {
public:
    Player();
private:
    int score;
};

#endif

Player.cpp

#include "Player.h"

Player::Player(){
    score = 0;
}

The command I'm using to compile is g++ main.cpp -o main And the error I'm being issued by the compiler is:

/tmp/ccexA7vk.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `Player::Player()'
collect2: error: ld returned 1 exit status

Note: All these files are in the same directory.

like image 294
Quinn McHugh Avatar asked Jul 27 '16 21:07

Quinn McHugh


People also ask

What are the steps in compiling C program?

Compilation process in C involves four steps: pre-processing, compiling, assembling, and linking. The preprocessor tool helps in comments removal, macros expansion, file inclusion, and conditional compilation. These commands are executed in the first step of the compilation process.

What are the 3 steps of the compilation process?

There are three basic steps involved in compiling a C program: preprocessing, compilation of C source code to machine code (or assembly) (also called object code), and linking of multiple object files into a single binary executable program.

What is the code to compile C program?

Run the gcc command to compile your C program. The syntax you'll use is gcc filename. c -o filename.exe . This compiles the program and makes it executable.

What is the output of C compiler compiling?

Compiling: Compiling is the second step. It takes the output of the preprocessor and generates assembly language, an intermediate human readable language, specific to the target processor.


1 Answers

As mentioned in the comments, you are not feeding Player.cpp into compiler. You should give all the cpp files to the compiler.

g++ main.cpp Player.cpp -o main
like image 196
Siavoshkc Avatar answered Nov 14 '22 23:11

Siavoshkc