Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Header Files - What to include [closed]

I have written a very simple class in C++, namely it is the Rectangle class from http://www.cplusplus.com/doc/tutorial/classes/. In particular here's the content of the Header file (Rectangle.h):

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle {

private:
    double m_x;
    double m_y;

public:
    Rectangle();
    Rectangle(double, double);
    void setXY(double, double);
    double getArea();
};

#endif

Here is the implementation (Rectangle.cpp):

#include "Rectangle.h"

Rectangle::Rectangle() {
    setXY(1, 1);
}

Rectangle::Rectangle(double x, double y) {
    setXY(x, y);
}

void Rectangle::setXY(double x, double y) {
    m_x = x;
    m_y = y;
}

double Rectangle::getArea(void) {
    return m_x * m_y;
}

Now, I'm supposed to include the Header of Rectangle in my main class, that is:

#include <stdlib.h>
#include <iostream>
#include "Rectangle.h"

using namespace std;

int main(void) {
    Rectangle a;
    cout << "Area : " << a.getArea() << "\n";
    return EXIT_SUCCESS;
}

But, then I get the error:

make all 
g++ -O2 -g -Wall -fmessage-length=0   -c -o Chung1.o Chung1.cpp
g++ -o Chung1 Chung1.o 
Chung1.o: In function `main':
/home/chung/eclipse_ws/Chung1/Chung1.cpp:8: undefined reference to `Rectangle::Rectangle()'
/home/chung/eclipse_ws/Chung1/Chung1.cpp:9: undefined reference to `Rectangle::getArea()'
collect2: ld returned 1 exit status
make: *** [Chung1] Error 1

The error is resolved if I include the file Rectangle.cpp instead. (I'm running on Eclipse)

Am I supposed to include the CPP file instead after all?

Here's my Makefile:

CXXFLAGS =  -O2 -g -Wall -fmessage-length=0    
OBJS =      Chung1.o    
LIBS =    
TARGET =    Chung1    
$(TARGET):  $(OBJS)
    $(CXX) -o $(TARGET) $(OBJS) $(LIBS)    
all:    $(TARGET)    
clean:
    rm -f $(OBJS) $(TARGET)    
run:    $(TARGET)   
        ./$(TARGET)

How can I modify it to compile the Rectangle class as well?

Solution: According to the answer from the user v154c1, it is necessary to compile individual cpp files and then include their headers in the main file or in any other file where this functionality is needed. Here's any example Makefile to do so:

CXXFLAGS =      -O2 -g -Wall -fmessage-length=0
#List of dependencies...
OBJS =          Rectangle.o Chung1.o
LIBS =
TARGET =        Chung1
$(TARGET):      $(OBJS)
        $(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all:    $(TARGET)
clean:
        rm -f $(OBJS) $(TARGET)
run:    $(TARGET)
        ./$(TARGET)
like image 734
Pantelis Sopasakis Avatar asked Nov 18 '12 16:11

Pantelis Sopasakis


People also ask

What should be included in header file in C?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .

What does close () do in C?

DESCRIPTION. The close() function shall deallocate the file descriptor indicated by fildes. To deallocate means to make the file descriptor available for return by subsequent calls to open() or other functions that allocate file descriptors.

What should be included in a header?

Headers and footers generally contain additional information such as page numbers, dates, an author's name, and footnotes, which can help keep longer documents organized and make them easier to read. Text entered in the header or footer will appear on each page of the document.

What do header files end with?

By convention, the names of header files end with . h (dot h). The . h suffix is used by header files that are provided with the operating system; however, the suffix is not required for user-generated header files.


2 Answers

You are not compiling and linking the Rectangle class.

Your compilation should look like:

g++ -O2 -g -Wall -fmessage-length=0   -c -o Chung1.o Chung1.cpp
g++ -O2 -g -Wall -fmessage-length=0   -c -o Rectangle.o Rectangle.cpp
g++ -o Chung1 Chung1.o Rectangle.o

If you're using Makefile, then just add the Rectangle.cpp the same way you use Chung1.cpp. The same goes for any IDE you may be using.

like image 119
v154c1 Avatar answered Nov 15 '22 19:11

v154c1


No, you are not supposed to include the .cpp. You have to compile it, this should produce a .o file that is then linked to the main executable. Your main is failing to find and link to this .o file for whatever reason. Without knowing the exact compilation and linking steps you are taking it is difficult to say more.

like image 28
juanchopanza Avatar answered Nov 15 '22 17:11

juanchopanza