Hacène
Hacène

Reputation: 273

C++ Header files (basics)

I was trying to use header files and source files but ran into issues. I therefore made a simplified version of what I am trying to do and I am getting the same error in CodeBlocks (undefined reference to add(double, double) in main.cpp).

In add.cpp:

double add (double x, double y)
{
    return x+y;
}

in add.h:

#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED

double add(double, double);

#endif

in main.cpp:

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

int main()
{
    std::cout<<add(3,4)<<std::endl;
    return 0;
}

What am I doing wrong? Note that when I add #include "add.cpp" to add.h everything works fine. But according to the textbook I'm following, this shouldn't be needed.

Upvotes: 0

Views: 117

Answers (1)

Hac&#232;ne
Hac&#232;ne

Reputation: 273

I had to manually specify the files as build targets as they are not built by default. In CodeBlocks this is done by right-clicking the project, going to properties > Build Targets and checking all the files in Debug and Release.

Upvotes: 2

Related Questions