Search code examples
c++visual-c++command-linelinkerprecompiled-headers

Visual C++. LNK2011: precompiled object not linked in. But I linked


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:\Users\yuryi\Desktop\C++\CMDOGL\lib" "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:\Users\user\Desktop\C++\CMDOGL\a.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

Solution

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