question

SimpleSamples avatar image
0 Votes"
SimpleSamples asked RLWA32-6355 commented

LNK2019: unresolved external symbol

I am currently still using VS 2017.

It has been man years since I have created a native DLL and now when I try to I was getting a linker error I cannot figure out. So I tried to follow the instructions in Walkthrough: Create and use your own Dynamic Link Library (C++) and I am getting the same problems. The following are the linker errors from the walkthrough.

 1>MathClient.obj : error LNK2019: unresolved external symbol __imp__fibonacci_init referenced in function _main
 1>MathClient.obj : error LNK2019: unresolved external symbol __imp__fibonacci_next referenced in function _main
 1>MathClient.obj : error LNK2019: unresolved external symbol __imp__fibonacci_current referenced in function _main
 1>MathClient.obj : error LNK2019: unresolved external symbol __imp__fibonacci_index referenced in function _main
 1>G:\Documents\Samples\Microsoft\Walkthroughs\MathClient\Debug\MathClient.exe : fatal error LNK1120: 4 unresolved externals

So let me back up a bit. For the header MathLibrary.h the following works:

 #include "..\..\MathLibrary\MathLibrary\MathLibrary.h"

However when I cut the path out and put that path in the Additional Include Directories it does not find the #include. I had the same problem with my initial project of a DLL and a console driver for executing the DLL.

I have followed the instructions in the walkthrough and added the MathLibrary.lib to the Additional Dependencies and the path for the library to the Additional Library Directories. When I use that path for dumpbin it shows the following symbols.

_fibonacci_current
_fibonacci_index
_fibonacci_init
_fibonacci_next

The following is the project properties for the DLL.

133783-t.jpg

The following is the Configuration Manager for it.

133746-t.jpg

Can anyone explain why I am getting the unresolved external symbols? If the malfunction is me then I sure do not see it. What can I do to diagnose it?

My system is due for a major rebuild. I should go for VS 2022 and perhaps I will give up on this problem until I do.

I have put my projects into GitHub.

https://github.com/SimpleSamples/MathLibrary
https://github.com/SimpleSamples/MathClient



c++windows-api-general
t.jpg (5.4 KiB)
t.jpg (14.7 KiB)
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The walkthrough instructions are for 32-bit builds of the dll and the console application. Make sure that you are not editing the project properties for 64-bit configurations. Also, are you sure you entered the right path for the folder containing the DLL's import library?

I followed the walkthrough instructions for the dll project and the console application with VS2017 and both projects built successfully.

0 Votes 0 ·

Thank you. I am sure I did not change the properties but just to be sure I have added s couple of images to my original question. I am not surprised it works for you; I think there is something unusual in my system. As for the path for the DLL's import library, I have checked many times and in many ways. If the path was incorrect then it would complain about not finding the library, correct? Something I did not try is adding a #pragma for the library, that is easy enough to try.

0 Votes 0 ·

Using #pragma comment(lib, "MathLibrary.lib") the error is LNK1104: cannot open file 'MathLibrary.lib' so that says something. I think I properly passed the lib file in the properties as in:
133774-t.jpg


0 Votes 0 ·
t.jpg (26.4 KiB)

Set the "Show Progress" property in the linker settings for the console application. Then when you build the client you should see the details of the linker's search for libraries. Maybe that will give you a clue about where it is looking for MathLibrary.lib.

133748-linker.png


0 Votes 0 ·
linker.png (53.2 KiB)
Show more comments

I put my projects into GitHub. I updated the original question with links to the repositories.

0 Votes 0 ·

If you create an empty Solution, add both of projects, add a reference to MathLibrary project using Add References dialog in MathClient, use Configuration Manager to make sure that both of projects are built for Debug, x64, then Additional Include Directories, #pragma and XCOPY do not seem necessary. Though, you can define Additional Include Directories to remove the relative path from #include.


0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

You had a bad fully qualified path set in the x64 linker property for Additional Library Directories.

Change the path to -

133757-samlinker.png



samlinker.png (53.4 KiB)
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried many things. The path I had is one of multiple attempts. The following shows that the path was valid.

133769-t.jpg

I did however change the Additional Library Directories as you specified here so it is now:

133806-t.jpg

That does not work. As you can see above, VS did not create a X64 folder. I apologize for providing the project using a path not valid in other systems but I had tried the IntDir macro and it is not valid for the directory structure that VS created. I think the path I did have should work in my system.


0 Votes 0 ·
t.jpg (45.8 KiB)

I reproduced the folder structure from your posted image and downloaded your two solutions from git to it.

Before x64 build of MathLibrary -

133800-before-x64-build.png

Then I built the x64 configuration for MathLibrary without making any changes to the properties -

133931-after-x64-build.png

Notice that x64 folders were created. If your VS2017 is not creating the x64 folders then that is one problem.

Now I build the x86 configuration for MathLibrary -

133848-after-x86-build.png

Building the x64 configuration of MathClient results in LNK2019 errors because you cannot link an x64 project against an import library from an x86 DLL build. That is the second issue which is caused by hard-coded path in the MathClient project properties.

The x64 debug build for MathLibrary shows the dll and import lib created -

133905-paths-x64.png

What paths does your x64 debug build for MathLibrary show?


0 Votes 0 ·
paths-x64.png (13.2 KiB)

@SimpleSamples Sorry for the missing images, my comment was hosed by this site.

After x64 build

133913-after-x64-build.png

After x86 build -

133885-after-x86-build.png


0 Votes 0 ·

This is the directory structure that VS created for me.
134126-t.jpg
As we can see it is missing the x64 directory. So yes that is a problem.

In the project properties for the library the Output File is $(OutDir)$(TargetName)$(TargetExt) and that expands to:

 G:\Documents\Samples\Microsoft\Walkthroughs\MathLibrary\x64\Debug\MathLibrary.dll

Yet the build produces:

 1>   Creating library G:\Documents\Samples\Microsoft\Walkthroughs\MathLibrary\Debug\MathLibrary.lib and object G:\Documents\Samples\Microsoft\Walkthroughs\MathLibrary\Debug\MathLibrary.exp
 1>MathLibrary.vcxproj -> G:\Documents\Samples\Microsoft\Walkthroughs\MathLibrary\Debug\MathLibrary.dll

Yes, the forum software here is having problems with images but I hope the text shown above is better than an image of that. It is strange that it builds without the x64.

I might do a repair install of VS 2017 but the better solution is to go for VS 2022. That involves installing a new SSD and other things so it will take time.








0 Votes 0 ·
t.jpg (28.8 KiB)
Show more comments
XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered SimpleSamples commented

I have build the both two projects successfully except modifying the MathClient Additional Include Directories, Additional Library Directories, Post-Build Event because my folder name is MathLibrary-master.
133871-image.png


image.png (12.2 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you. If I understand what you are saying then it should work in my system too. The thing I do not know is why it is not working for me.

0 Votes 0 ·