0

So I've got a project file in CodeBlocks for Project Euler, but I'm a bit confused about what I've done wrong in setting my code up. I have a main.cpp file for running my programs, and I prototype each problem's function before I use it in the main block. However, I have this error when trying to build it:

||=== Build: Debug in Project Euler (compiler: GNU GCC Compiler) ===| obj\Debug\main.o||In function main':| C:\Users\under\cpp-workspace\Project Euler\main.cpp|9|undefined reference top4()'| ||error: ld returned 1 exit status| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

This is what I have, so I'm not sure what's wrong?

main.cpp:

#include <iostream>

using namespace std;

void p4();

int main()
{
    p4();
    return 0;
}

p4.cpp:

#include <iostream>

using namespace std;

void p4()
{

    cout << "hello there" << endl;

}

I'm not sure what's wrong?

My question is not a duplicate, at least not that I can tell. The question this is supposedly a duplicate of never mentions the issue I'm having.

smac89
  • 39,374
  • 15
  • 132
  • 179

1 Answers1

0

I've checked, and p4() is a void function with no inputs, so I'm not sure what's wrong.

You have declared p4() but you haven't defined it. Add

void p4()
{
}

to your file for an empty definition. If you need to do more in p4, add whatever code you want to.


Update

You just need to add p4.cpp to the set of files in your project.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I just realized that I forgot to add the p4.cpp file here. In the project, the main.cpp and p4.cpp file are in the same project. I'll add it to the main post right now. – underspring3000 Jul 23 '17 at 04:37
  • Both files are in the same project though. – underspring3000 Jul 23 '17 at 04:47
  • @underspring3000, it can't be. Perhaps you are confused because the file is in the file system. Presence of a file in the file system does not automatically put the file in the project. – R Sahu Jul 23 '17 at 04:49
  • I can show you a screenshot of it right now. http://imgur.com/gallery/Lti4o – underspring3000 Jul 23 '17 at 04:54
  • @underspring3000 Any way you can do a full build and add the build output to your question? – user4581301 Jul 23 '17 at 04:56
  • @underspring3000, I am out of ideas. – R Sahu Jul 23 '17 at 04:59
  • Yo @underspring3000, instead of `void p4();` in main, use `extern void p4();`, and see if that solves the issue. It could be that the function definition is searched for in main.cpp instead of acknowledging that it could be defined somewhere else – smac89 Jul 23 '17 at 05:05
  • @smac89, that won't make any difference. – R Sahu Jul 23 '17 at 05:08
  • @RSahu I am too :( – underspring3000 Jul 23 '17 at 05:10
  • @underspring3000, one more idea is to use header files. Create a file called `p4.hpp` and put `void p4();` in that file. Remove `void p4();` from `main.cpp`, and replace it with `#include "p4.hpp"`. Compile and run. NOTE, if you have a c++ compiler installed on your machine, you can test this by running `c++ main.cpp p4.cpp -o app && ./app.exe`. The fact is that this is a codeblocks specific issue, and there is only so much that one can say to help you at this point. You need to raise this issue on a forum that deals with codeblocks – smac89 Jul 23 '17 at 05:13
  • @smac89 just tried and it doesn't work either :( – underspring3000 Jul 23 '17 at 05:16
  • @user4581301, yea like I said, this is very much related to codeblocks as I cannot reproduce by simply compiling on the command line (also I'm not on windows). See if this [link](http://wiki.codeblocks.org/index.php/Common_problems_and_solutions) helps, and look for more help that is specific to codeblocks – smac89 Jul 23 '17 at 05:19
  • @smac89 "You need to raise this issue on a forum that deals with codeblocks –" - or read one of the thousands of other questions on this site with the same problem – M.M Jul 23 '17 at 05:30
  • `extern void p4();` means exactly the same as `void p4();` – M.M Jul 23 '17 at 05:32