How to fix Linking error LNK2011: precompiled object not linked in; image may not run
![none](https://i1.social.s-msft.com/globalresources/Images/trans.gif?cver=0%0d%0a)
Question
-
Hi all,
I am linking a program with a static library ,which I compiled/linked without any error, and obtained Linking Error LNK2011. How to fix this problem? any suggestion? What is the best (or correct) way to link a static library which is using functions in other static library? Thanks in advance for your help.
Tom Lin
Answers
-
Thanks Nancy for your reply.
The first time I compiled/linked my static library, I did not 'deselect' Precompiled Header option. That's the error LNK1120 occured. After reviewing the MSDN article "Creating and Using a Static Library", I re-started it again. This time the error message is 'fatal error LNK1257: code generation failed'. Here is what I did
1. I compiled and linked A.lib according to the MSDN article.
2. I prepare a main program to link with A.lib library for testing purpose. It works fine. The following is the properties pages of the main program I changed:
a. add the path of header file, i.e., A.h, to Additional Include Directory.
b. add A.lib to Additional Dependencies.
c. add the path of A.lib to Additional Library directory.
3. Next I compiled/linked B.lib. This library does use functions in A.lib in its own functions.
a. I created B.lib according to MSDN article.
b. add the pathes of both header files, i.e., A.h and B.h to Additional Include Directory.
c. add A.lib to additional Dependenceis.
d. add the path of A.lib to additional Livrary directory.
(B.lib was compiled and linked without any error.)
4. When I prepare a main program to link with B.lib for testing. Again I modified the properties pages accordingly. But it failed to generate the *.exe. The error message is LNK1257.
Any suggestions or comments? Thanks in advance.
Tom Lin- Marked as answer by Tom C. Lin Wednesday, December 09, 2009 10:03 PM
All replies
-
-
Thanks Jinzai,
I have a group of functions which is used to calculate ,said, Earth's magnetic field. I put them in a class and compile the class as a static library. I then wrote a main program to test the libray. It works fine. Next I have another class which need to call the library for Earth's magnetic field. I put this class as another static libray. The new static library compiled just fine. But when I tried to link a main program to test this new library, the error LNK2011 occured. Did I do it correctly? Any suggestions or comments? Thanks for your help.
Tom Lin -
Hi Tom C. Lin,
LNK2011 means precompiled object not linked in. I guess you have not used the functionality from the static library in application correctly. Have you put this static library in Additional Include Directories ? To do this, in the Property Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General. Next to Additional Include Directories, type the path of the location of the MyLib.h header file.
For more information, please refer to MSDN article:
Creating and Using a Static Library (C++)
http://msdn.microsoft.com/en-us/library/ms235627.aspx
Best Regards,
Nancy
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. -
Thanks Nancy for your reply.
The first time I compiled/linked my static library, I did not 'deselect' Precompiled Header option. That's the error LNK1120 occured. After reviewing the MSDN article "Creating and Using a Static Library", I re-started it again. This time the error message is 'fatal error LNK1257: code generation failed'. Here is what I did
1. I compiled and linked A.lib according to the MSDN article.
2. I prepare a main program to link with A.lib library for testing purpose. It works fine. The following is the properties pages of the main program I changed:
a. add the path of header file, i.e., A.h, to Additional Include Directory.
b. add A.lib to Additional Dependencies.
c. add the path of A.lib to Additional Library directory.
3. Next I compiled/linked B.lib. This library does use functions in A.lib in its own functions.
a. I created B.lib according to MSDN article.
b. add the pathes of both header files, i.e., A.h and B.h to Additional Include Directory.
c. add A.lib to additional Dependenceis.
d. add the path of A.lib to additional Livrary directory.
(B.lib was compiled and linked without any error.)
4. When I prepare a main program to link with B.lib for testing. Again I modified the properties pages accordingly. But it failed to generate the *.exe. The error message is LNK1257.
Any suggestions or comments? Thanks in advance.
Tom Lin- Marked as answer by Tom C. Lin Wednesday, December 09, 2009 10:03 PM
-
Hello all, Just to let you all know that after I re-compiled all the libraries in VS2008, the problem went away. Now I can test all my static libraries. I think the problem was one of the library was compiled in VS2005. Thank you for your time and help. I truely appreciated it. Good Day.
Tom Lin -
Hi Tom,
Glad to hear it works fine.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Thursday, December 10, 2009 2:02 AM