2

I am extremely new to C++ so please bear with me. I am following a tutorial to learn C++ and I am also using Eclipse as advised. I did exactly what was asked in creating the header file and putting my prototype in it. But I am getting this weird error when I build my project. I've inserted my code, header file and the error in my console below.

#include <iostream>

#include "utils.h"

using namespace std;


int main() {

    doSomething();
    doSomething();

    return 0;
}

void doSomething() {
    cout << "Hello" << endl;
}

Next is my header file which I named utils.h

#ifndef UTILS_H_
#define UTILS_H_

void doSomething();

#endif /* UTILS_H_ */

And this is what I am getting from my console. I am not sure what it means or how to fix this ...

**** Build of configuration Debug for project Headers_and_Prototypes ****

make all

make: *** No rule to make target 'main.o', needed by 'Headers_and_Prototypes'. Stop.

Community
  • 1
  • 1
John
  • 37
  • 5
  • 5
    This is not a C++ problem, but an Eclipse configuration problem. – Jean-Baptiste Yunès Dec 24 '15 at 08:16
  • Try using another C++ IDE like Code::Blocks, and fortunately the problem won't appear. Hopefully. – Sean Francis N. Ballais Dec 24 '15 at 08:18
  • If you're a newbie and on Windows then it's better to use Visual Studio for c++ development. There is a community edition which is free. – rustyx Dec 24 '15 at 08:19
  • John, is the name of the file containing main name utils.cpp? In C++ declaration and definition of the methods should have the same file name. For example for the method doSomething() in your header utils.h, void doSomething(){} should either be in the header itself or utils.cpp – Nobody Dec 24 '15 at 08:44

3 Answers3

0

As far as I know you have to make a .cpp file for your header, which in your case should only have #include "utils.h" and I think it should also have the void doSomething().

JohnnyOnPc
  • 386
  • 1
  • 5
  • 18
0

If my guess is right file name of the program including utils.h is main.cpp. When you are building, the compiler is trying to look for definition of doSomething in utils.h or utils.cpp and it not able to locate the it as the definition is in main.cpp instead.

The definition for the doSomething() should go into utils.cpp.

Nobody
  • 91
  • 7
0

I am of same opinion with other two guys. Along with that, I would suggest not to create a make file project. This will hopefully help you to resolve the issue.

In summery

1)Create a blanck project

2) A header which contains.

a) The pre-processor definition and includes required.

b) The prototypes of the function with extern keyword.

c) Define any user defined data type in the header if required.

d) use #pragma once to include the header only once.

3)In cpp file provide the definition of all the function along with main.

Then first build the project and then run.

Anirban Roy
  • 111
  • 1
  • 1
  • 9