0

For the life of me I cannot figure out what is causing this... I keep getting unresolved external symbol error. However, if I put an empty definition in the header file it compiles correctly.

WINMAIN.CPP

#include "FILE_XXX.H"

int WINMAIN WinMain(...)
{
   EnableOpenTest(); // call like this
   return 0;
}

FILE_WORKS_CORRECTLY.H

#ifndef _FILE_WORKS_CORRECTLY_
#define _FILE_WORKS_CORRECTLY_

void EnableOpenTest() { }

#endif

However, when I do something like this (correctly), it does not work and I get a compile-time error.

FILE_DOES_NOT_WORK_CORRECTLY.H

#ifndef _FILE_DOES_NOT_WORK_CORRECTLY_
#define _FILE_DOES_NOT_WORK_CORRECTLY_

void EnableOpenTest();

#endif

FILE_DOES_NOT_WORK_CORRECTLY.CPP

#include "FILE_DOES_NOT_WORK_CORRECTLY.H"

void EnableOpenTest() { /* do work here */ }

UPDATE: Upon further investigation, I found the issue has to do with me having multiple projects in the same solution. I then try to reference a function in one project from another project. Obviously I'm doing this incorrectly.

keelerjr12
  • 1,693
  • 2
  • 19
  • 35
  • 3
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Jul 10 '16 at 18:53
  • Unrelated to your issue, but don't use leading underscore followed by an upper-case letter for any symbol name, those are reserved for "the implementation" (compiler and standard library). See [this old question and its answers for details](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Some programmer dude Jul 10 '16 at 18:56
  • 1
    As for your problem, when you get the errors, you *do* build with both source files? Both source files are in your actual project tree in Visual Studio (which I assume you use) and not only in the same directory on disk? – Some programmer dude Jul 10 '16 at 18:57
  • Correct. The CPP file is in the source part of the project and the H file is in the header file portion. – keelerjr12 Jul 10 '16 at 19:00
  • Does your WINMAIN.CPP include the file with EnableOpenTest? – Noam Rodrik Jul 10 '16 at 19:25
  • @Dreggman45 Yessir with the #include "blah.h" – keelerjr12 Jul 10 '16 at 19:27
  • I ran the code you gave me, it works alright... Can you show a part of your actual code so I can help you more definitely? – Noam Rodrik Jul 10 '16 at 19:43
  • @Dreggman45 It's a large-scale project actually (Falcon 4.0 source code). Upon further investigation, I found the issue has to do with me having multiple projects in the same solution. I then try to reference a function in one project from another project. Obviously I'm doing this incorrectly. – keelerjr12 Jul 10 '16 at 19:52

3 Answers3

0

The only mistake i see is that in the cpp file you need to include the return type as well. It should be

void EnableOpenTest()
{
    //Enter Code Here
}
Informat
  • 808
  • 2
  • 11
  • 25
0

Inside of FILE_DOES_NOT_WORK_CORRECTLY.CPP:

EnableOpenTest(){ /* do work here */ }

must be

void EnableOpenTest(){ /* do work here */ }

Looks like your compiler sets the missing return type to int instead of yelling at you with a error message.

You should turn on compiler warnings, it would allow you to notice such errors very quickly.


Also, inside of FILE_WORKS_CORRECTLY.H you have another error:

void EnableOpenTest() { }

must be

inline void EnableOpenTest() { }

Otherwise it will trigger a error message if this header is included twice (i.e. in more that one .cpp file).

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • Doh! Sorry, I do have it as void (I just didn't do a good job copying it over for this question). So we are back to square 1. – keelerjr12 Jul 10 '16 at 19:24
0

Solved it!

  1. Additional projects needed to be static library (main project .exe)
  2. Added References of library projects to main project

Obviously the file structure caused a lot of these issues.

keelerjr12
  • 1,693
  • 2
  • 19
  • 35