0

I have some test code:

foo.h

#ifndef FOO_H
#define FOO_H

void Foo(int iBar);

#endif FOO_H

foo.cpp

#include "foo.h"

void Foo(int iBar)
{
    // Do nothing...
}

main.cpp

#include "foo.h"

int main()
{
    Foo(1);

    return 0;
}

When I build the above code in VS2010 I get a linker error:

error LNK2019: unresolved external symbol "void __cdecl Foo(int)" (?Foo@@YAXH@Z) referenced in function _main

I can't figure out why my Foo() function would be unresolved at link-time. Can anybody give me some insight as to what I might be doing wrong?

BlazeCell
  • 145
  • 1
  • 8
  • 3
    Did you link in foo.obj into the exe? – OldProgrammer Oct 02 '13 at 02:39
  • @n0rd: I use VS2010, so when I have my solution open I click _Build->Build Solution_ in the menu. Each project in a solution has various settings that can be altered regarding build paths, dependency declarations, etc. I haven't altered my build settings beyond where I want my executable generated. – BlazeCell Oct 03 '13 at 23:57
  • are `main.cpp` and `foo.cpp` in the same project? – n0rd Oct 04 '13 at 00:38
  • @n0rd: Yes, all the files mentioned are in the same project. – BlazeCell Oct 04 '13 at 06:53
  • Are you sure `Foo` function is not defined in `foo.cpp` with `static` keyword? Anyway, run the "Visual Studio Command Prompt" go to you project directory, then under Debug or Release (depending on what you attempted to build) directory there should be foo.obj file. Run `dumpbin /symbols foo.obj` and check that a) `Foo` is exported, b) it has exactly the same mangled name as linker looks for (mentioned in error message). `dumpbin /symbols foo.obj | find "Foo"` might be helpful – n0rd Oct 04 '13 at 22:33

1 Answers1

0

So, after some trial and error I got my code to successfully build by moving the definition code from foo.cpp into foo.h.

For whatever reason, it appears that foo.cpp wasn't being tied into foo.obj. I have global logging utility functions declared and defined in log.h and log.cpp respectively, just like I have in my test 'foo' files above, yet I have no problem compiling and linking them to my program. I wonder what I'm doing that has one link without issue, while the other doesn't recognize the cpp file.

BlazeCell
  • 145
  • 1
  • 8