Error lnk2011 precompiled object not linked in image may not run

Hi all,
  • Remove From My Forums
  • 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

      Wednesday, December 9, 2009 10:03 PM

Permalink

Cannot retrieve contributors at this time

description title ms.date f1_keywords helpviewer_keywords ms.assetid

Learn more about: Linker Tools Error LNK2011

Linker Tools Error LNK2011

11/04/2016

LNK2011

LNK2011

04991ef5-49d5-46c7-8eee-a9d1d3fc541e

precompiled object not linked in; image may not run

If you use precompiled headers, LINK requires that all of the object files created with precompiled headers must be linked in. If you have a source file that you use to generate a precompiled header for use with other source files, you now must include the object file created along with the precompiled header.

For example, if you compile a file called STUB.cpp to create a precompiled header for use with other source files, you must link with STUB.obj or you will get this error. In the following command lines, line one is used to create a precompiled header, COMMON.pch, which is used with PROG1.cpp and PROG2.cpp in lines two and three. The file STUB.cpp contains only #include lines (the same #include lines as in PROG1.cpp and PROG2.cpp) and is used only to generate precompiled headers. In the last line, STUB.obj must be linked in to avoid LNK2011.

cl /c /Yccommon.h stub.cpp
cl /c /Yucommon.h prog1.cpp
cl /c /Yucommon.h prog2.cpp
link /out:prog.exe stub.obj prog1.obj prog2.obj

I’m trying to use precompiled headers for the first time. Using command line. Here is it:

cl /Yu"pch.h" src/main.cpp src/shader.cpp src/camera.cpp /std:c++17 /MT -EHsc glfw3.lib glew32.lib GlU32.lib OpenGL32.lib -I include /link "pch.obj" /LIBPATH:"C:UsersyuryiDesktopC++CMDOGLlib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /OUT:"C:UsersuserDesktopC++CMDOGLa.exe" /MACHINE:X64 /nologo

But it prints: pch.obj : LNK2011: precompiled object not linked in. image may not run. I can’t figure out how to link it. I think I did it using /link «pch.obj». Note: /Yc»pch.h» was successful

File structure:

 - main.cpp
 - pch.cpp
 - pch.h
 - shader.cpp
 - shader.h
 - camera.cpp
 - camera.h

pch.cpp is just #include "pch.h". pch.h includes everything needed in project. all other files include only pch.h.

Things I’ve tried:

  1. Googling for 2 hours (MS docs etc.)
  2. Tweaking command line in different ways

Build Succeeded's user avatar

asked Oct 22, 2020 at 12:29

natovan's user avatar

5

I solved this by this sequence of commands:

cl /c /Yc"pch.h" src/pch.cpp /std:c++17 /MT -EHsc -I include /link /nologo
cl /c /Yu"stdafx.h" src/main.cpp /std:c++17 /MT -EHsc -I include /link /nologo
cl /c /Yu"stdafx.h" src/camera.cpp /std:c++17 /MT -EHsc -I include /link /nologo
cl /c /Yu"stdafx.h" src/shader.cpp /std:c++17 /MT -EHsc -I include /link /nologo

And for fast compiling of source files using precompiled headers:

cl /Yu"pch.h" src/main.cpp src/shader.cpp src/camera.cpp /std:c++17 /MT -EHsc glfw3.lib glew32.lib GlU32.lib OpenGL32.lib -I include /link /out:a.exe stdafx.obj /LIBPATH:"lib" "kernel32.lib" "user32.lib" "gdi32.lib" "shell32.lib" /MACHINE:X64 /nologo

I don’t quite exactly understand how it works but it solved the problem.

answered Oct 25, 2020 at 15:43

natovan's user avatar

  • Question

    • Edited by
      svoipsupport
      Monday, February 6, 2012 3:28 PM
    • Moved by
      Yi Feng Li
      Friday, February 10, 2012 9:56 AM
      Third Party Issue (From:Visual C++ Language)

All replies

  • It’s linking error provide apropriate .lib file to your solution so that it can run. Same time you can use dependency walker to look for all the dependent dll on your project.

    Thanks


    Rupesh Shukla

  • Dear Rupesh Shukla,

    Thanks for your repay.

    Can you describe detail? i’m a newbie.

    I use VC6 compiled right, just face that issue when i use insure++
    compile.

    I guess the insure++ installed right, bcz we used that for several projects.

    Best wish.

    Sunny Tan

  • Hi svoipsupport,

    Welcome to the MSDN Forum.

    I have been watching this post for a while now. I’d like to let you know that Insure++ is not a product of Microsoft and that VC6 is out of our support scope. So this thread will be moved to «Off-Topic Posts».

    If you have questions about Insure++, I suggest you move to
    Parasoft Forum->Insure++ for more helpful information.

    Thanks for your understanding.
    Best regards


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by
      Helen Zhao
      Friday, February 10, 2012 10:05 AM

Problem Description:

Using visual studio 2015 to compile apr-iconv fails and prompts «linker tool error LNK2011: precompiled object not linked; image may not run» error.

Cause Analysis:

MSDN describes the error as follows (https://msdn.microsoft.com/zh-cn/library/3ay26wa2.aspx):

If a precompiled header is used, LINK requires that all object files created with the precompiled header must be linked. If you have source files used to generate precompiled headers for other source files, you must now include the object files created with the precompiled headers. For example, if you compile a file named STUB.cpp to create precompiled headers for other source files, you must link with STUB.obj, otherwise you will get this error. In the following command line, the first line is used to create the precompiled header COMMON.pch, which is used with PROG1.cpp and PROG2.cpp in the second and third lines. The file STUB.cpp only contains#includeLine (with PROG1.cpp and PROG2.cpp#includeSame line), and only used to generate precompiled headers. In the last line, STUB.obj must be linked to avoid LNK2011. To

cl /c /Yccommon.h stub.cpp  
cl /c /Yucommon.h prog1.cpp  
cl /c /Yucommon.h prog2.cpp  
link /out:prog.exe stub.obj prog1.obj prog2.obj  

In the libapriconv_ccs_modules and libapriconv_ces_modules libraries, the corresponding precompiled header files are used, but the corresponding obj compilation object is not added in the link option.

solution:

Modify the apr-iconvbuildmodules.mk.win file:

.c{$(OUTPUT_DIR)}.so:
	cl $(ALL_CFLAGS) /Fo$*.obj /Yuiconv.h /c $<
	link $(ALL_LDFLAGS) $(MODRES).obj $*.obj $(API_LIBS) /out:[email protected] 
		/base:@"..buildBaseAddr.ref",$(@F)
	if exist [email protected] 
		mt -nologo -manifest [email protected] -outputresource:[email protected];2 
		& del "[email protected]"
	del "$*.exp" & del "$*.lib"

   Add $(MODRES).obj item to link command.

Понравилась статья? Поделить с друзьями:
  • Error lnk2005 fortran
  • Error lnk2005 already defined in libcmtd lib
  • Error lnk2001 неразрешенный внешний символ main
  • Error lnk2001 неразрешенный внешний символ cxxframehandler3
  • Error lnk2001 unresolved external symbol winmain 16