Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2012 link errors with static libraries

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 ========
like image 675
Charles Clark Avatar asked Nov 19 '12 06:11

Charles Clark


1 Answers

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).

like image 105
Peter Huene Avatar answered Oct 12 '22 01:10

Peter Huene