alex.dev
alex.dev

Reputation: 491

MSBuild error MSB4018 in VS2015: The "Link" task failed unexpectedly

After upgrading a solution with 25 projects from VS2012 Update 4 to VS2015 RTM (14.0.23107.0), I get the following error while building one of the projects:

(...) MSB4018: 'The "Link" task failed unexpectedly. (...) System.NullReferenceException: Object reference not set to an instance of an object.' (...)

Here the full output:

1>------ Build started: Project: Buttons, Configuration: Release Win32 ------
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018: The "Link" task failed unexpectedly.
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018: System.NullReferenceException: Object reference not set to an instance of an object.
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018:    at Microsoft.Build.CPPTasks.Link.ForcedRebuildRequired()
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018:    at Microsoft.Build.CPPTasks.TrackedVCToolTask.ComputeOutOfDateSources()
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018:    at Microsoft.Build.CPPTasks.TrackedVCToolTask.SkipTaskExecution()
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018:    at Microsoft.Build.Utilities.ToolTask.Execute()
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018:    at Microsoft.Build.CPPTasks.TrackedVCToolTask.Execute()
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(643,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__1.MoveNext()
========== Build: 0 succeeded, 1 failed, 24 up-to-date, 0 skipped ==========

I've tried already several suggested solutions related to MSB4018 without any success. The error 'The "Link" task failed unexpectedly' wasn't even indexed by Google yet. I guess this issue is somehow related to the type of the project because it's the only project in the solution that's used as a resource DLL (i.e. with no entry point, etc.). The only changes that have been made to the project file by VS2015 are "ToolsVersion: 4.0 => 14.0" and "PlatformToolset: v110_xp => v140_xp".

Does anyone has a solution for this?

Upvotes: 18

Views: 45300

Answers (4)

The mentalist Coder
The mentalist Coder

Reputation: 362

If the error statement is something like 'System.InvalidCastException: Unable to cast object of type 'System.Xml.XmlComment' to type 'System.Xml.XmlElement'' then you can try the following:

Unload the .csproj file and check for Project ToolsVersion. Make sure that projects ToolsVersion is lesser than 14 (for example 12).

Upvotes: 0

Kevin K
Kevin K

Reputation: 374

Another solution is to delete all generated files from the last build through an explorer window.

Delete all files in:

.\Project\bin

.\Project\obj

While you can "Clean" or "Rebuild" directly through Visual Studio, this only captures some of the files. By forcing to recompile all files, all memory locations will now correctly align and the issue should be resolved.

Upvotes: 7

xs2harpreet
xs2harpreet

Reputation: 61

VS 2015: Solution of the problem is as follows: Goto:

Project Properties->Linker->Debugging->Generate Debug Info,

Set this property either as Optimize for debugging (/DEBUG) or No

Note: I observed similar issue when it was blank(not set).

It worked for me.

Hope this information would be helpful.

Good Luck:).

Upvotes: 6

alex.dev
alex.dev

Reputation: 491

I've managed to fix this issue myself and would like to document the solution here for future reference.

The following error was caused by an empty XML element in the project file:

  • MSB4018: System.NullReferenceException: Object reference not set to an instance of an object.

The empty element was accessed by the 'Link' task, which just failed:

  • MSB4018: 'The "Link" task failed unexpectedly.

Removing the respective element fixed the issue:

<Link><GenerateDebugInformation></GenerateDebugInformation></Link>

Hint: As it can be seen in other posts, many of the MSB4018 errors seem to be related to project files containing unexpected values.

Upvotes: 20

Related Questions