13

I recently upgrade to 2012 from 2005 (I'm pretty sure) and ran into link errors when building my solution. I ran out of ideas after hours of searching google and putsing around. I've set up dozens of projects, so I'm pretty certain I've done everything right, but to be fair, it's been a few years.

So, as a test I set up a new project. I called it App, a Windows application (.exe). I created a second project called Core and flagged it as a static library (.lib) in it's Configuration Type. Both are a part of the solution. In Core I added Test.cpp and Test.h which contain a small class that has a simple function in it. Then, inside the Window's applications WinMain.cpp file I have WinMain() where I call into this test class via CTest test; test.Result();

Along with this I've set the project App's Project Dependencies to be Core and finally added to App's Additional Include Directories the path to the Core code where Test.cpp/.h live.

I get the following link errors and cannot for the life of me figure out why. Does anyone know what step I may have missed or what changed in 2012 from previous versions? Thank you very very much in advance for the help!

1>------ Build started: Project: App, Configuration: Debug Win32 ------
1>WinMain.obj : error LNK2019: unresolved external symbol "public: __thiscall CTest::CTest(void)" (??0CTest@@QAE@XZ) referenced in function _WinMain@16
1>WinMain.obj : error LNK2019: unresolved external symbol "public: __thiscall CTest::~CTest(void)" (??1CTest@@QAE@XZ) referenced in function _WinMain@16
1>WinMain.obj : error LNK2019: unresolved external symbol "public: int __thiscall CTest::Result(void)" (?Result@CTest@@QAEHXZ) referenced in function _WinMain@16
1>D:\Work\Test_Linker_Stupidity\App\Debug\App.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ========
Hypershadsy
  • 398
  • 4
  • 13
Charles Clark
  • 153
  • 1
  • 2
  • 8

1 Answers1

27

Visual Studio 2005 did some magic with project dependencies where it would automatically link in any .lib outputs (I unfortunately was the developer that helped implement it). This appears to have been removed since, I suspect, Visual Studio 2010 when the old Visual C++ build system was replaced with MSBuild.

However, the "automatic linking of static library dependencies" feature can still be found via project references:

  • Right click on the App project and select "References..."
  • Click "Add New Reference".
  • Check the static library project and press OK.
  • Build.

You should now see the static library being automatically linked in. Note that project references also imply a project dependency.

If you prefer to use a project dependency instead, you'll need to add the static library to the linker's additional dependencies property on the "App" project, like you would for any other static library input.

Edit: also, you'll see a property called "Link Library Dependencies" on the project reference. This controls whether or not the .lib output of the referenced project gets linked in or not (defaults to true).

Peter Huene
  • 5,758
  • 2
  • 34
  • 35
  • 1
    Thanks Peter! I pinged some friends on FB last night and they pointed me in the same direction. It was surprising that I didn't find any other articles that referenced the same solution, so hopefully this one will be beneficial to others in the future. I'd also tried adding the libs the the input, that certainly worked, but was far less than ideal. I'd actually forgotten that I'd tried that solution, albiet briefly. Thanks again for the help! – Charles Clark Nov 20 '12 at 04:59
  • As a quick reference, this is the link I was pointed to: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d69aeb0d-e5fb-41e1-98ad-9b6c7b43a3ca/#c8b80c9a-2e8d-4a06-9d0f-a63ee15a208e – Charles Clark Nov 20 '12 at 04:59
  • Thanks ! I used visual 2005 and 2008 and dependencies worked great. Recently I migrated to 2012 and ran into this problem. Old dependencies from 2008 were migrated properly but new ones added in "Project Dependencies" window don't do much. That is so strange that this window doesn't work in the same any more. – Mikosz Nov 04 '13 at 00:05
  • 1
    In Visual Studio 2015 (potentially also in earlier versions) there is a new "References" node below the project node. Right-click on the "References" child node and select "Add Reference". – Helge Klein Mar 05 '16 at 23:04