0

I'm trying to write a program in C++ using Atom Editor on Ubuntu 18.04. I'm very new to using Linux and all my previous experience coding is on computers and IDEs that were already set up for me to use so this is hopefully a simple fix. As far as I'm aware I have installed GCC correctly. Executing which gcc g++ in Terminal returns

/usr/bin/gcc
/usr/bin/g++

and I'm using the gpp-compiler package with Atom to compile and run my programs within Atom.

My program currently consists of header file "Creature.h" with the constructor and methods

#ifndef CREATURE_H
#define CREATURE_H

class Creature {
    private:
        int fHealth;

    public:
        Creature(int Health);

        virtual ~Creature(){}

        void setHealth(int Health){fHealth = Health;}
        int getHealth(){return fHealth}

};
#endif

with the constructor defined in the implementation file "Creature.cpp":

#include "Creature.h"

Creature::Creature(int Health){
    setHealth(Health);
}

When I then create an instance of this class in my main program, e.g.

#include "iostream"
#include "Creature.h"

using namespace std;

int main(){

    Creature MyCreature(10);

    cout<<MyCreature.getHealth()<<endl;

    return 0;
}

and try to compile and run my program I get the error

/tmp/cc4H4p2l.o: In function `main':
<br/>main.cpp:(.text+0x47): undefined reference to `Creature::Creature(int)'
collect2:error: ld returned 1 exit status

From my limited understanding and the investigation I've done it seems to me that either the Creature.cpp file hasn't been compiled, or the resulting object files are not linked.

In the first case, I am not sure how to compile just the Creature.cpp file as if I try to do this within Atom it also tries to run the file and fails as obviously there is no main function. I also tried to do this in terminal by executing g++ -c Creature.cpp and still get the same error.

In the second case, I'm not really sure what this means or how to do it.

Thanks in advance for any help I'm able to get.

  • You should build like `g++ main.cpp Creature.cpp`, or in two steps `g++ -c Creature.cpp -o Creature.o && g++ main.cpp Creature.o` – 273K Apr 08 '20 at 22:32
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – 273K Apr 08 '20 at 22:32
  • 1
    So this allows me to run the main program from the command line. I'm looking for a way to achieve the same effect from within Atom, especially as I may end up with many more cpp files to link. From what I can understand I think this means I need to look into makefiles? – Nathan Mayes Apr 08 '20 at 22:49

1 Answers1

0

You can build your program using:

g++ *.cpp -Wall -Wextra

Avoiding writing all your ccp files names.

Warnings is a bonus, allways a good idea.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Thanks! Do you mind explaining what -Wall and -Wextra do here? Trying to learn as much as I can. Edit: Ah! Are these asking to print warning messages? – Nathan Mayes Apr 08 '20 at 22:53
  • @NathanMayes yes these are warning flags, they are not obligatory but is always good to use them as they detect ptoblems in the code that can be troublesome in the future. – anastaciu Apr 08 '20 at 22:55
  • Awesome. Thanks for your help. If I have a Makefile with this same line will it accomplish the same outcome? – Nathan Mayes Apr 08 '20 at 22:57
  • @NathanMayes, not exactly, for Makefile you will need to create the object files and then link them, this post has everything you need to know about make https://stackoverflow.com/questions/2481269/how-to-make-a-simple-c-makefile/2481326 – anastaciu Apr 08 '20 at 23:04
  • Amazing this is just what I needed :) Appreciate the help! – Nathan Mayes Apr 08 '20 at 23:08
  • @NathanMayes, glad to help, start with [this answer](https://stackoverflow.com/a/2481307/6865932) which has a simpler method, don't forger to [accept my answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if you feel it correctly addressed your question. – anastaciu Apr 08 '20 at 23:12