natovan
natovan

Reputation: 21

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

Upvotes: 1

Views: 4068

Answers (2)

natovan
natovan

Reputation: 21

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.

Upvotes: 1

MMS 05
MMS 05

Reputation: 9

-> For Linking an Object File you need to Use link /out objectfile.obj. Its the proper Way Referred in the Microsoft Docs. -> Even thought If You Get that Annoying Error Try This Link https://learn.microsoft.com/en-us/cpp/build/reference/linking?view=vs-2019

->This Page Contains all the CMD Linking stuff https://learn.microsoft.com/en-us/cpp/build/reference/linker-options?view=vs-2019

You Can Also Use the /WHOLEARCHIVE, This Command Takes an Path to Library of the Object Files.

->This Page Contains /WHOLEARCHIVE Stuff https://learn.microsoft.com/en-us/cpp/build/reference/wholearchive-include-all-library-object-files?view=vs-2019

Theres also Another Method to Link Object Files /Fo command. This Command Again Takes the Path Value to the Object File

-> /Fo Stuff https://learn.microsoft.com/en-us/cpp/build/reference/fo-object-file-name?view=vs-2019

-> I am Adding This Link as I thought That There Might Be Some Sort of Wrong Commands Hiding in that line of commands. https://learn.microsoft.com/en-us/cpp/build/creating-precompiled-header-files?view=vs-2019 The Above One Contains the Precompiled Files in Visual Studio and MSVC++ Compiler

Upvotes: 0

Related Questions