I have a small program which I want to execute via the command line. I run it from inside the IDE and it runs fine. I copy the entire compilation command and paste it on the MSVS command prompt and I get unresolved symbol linker errors (I have done this before on some programs that didn't require passing of linker options).
I've read from the documentation that I need to specify /link linker-options
(from here). But when I do so, I get other errors. Then as instructed I went here, which should say how I should specify the linker options. But it doesn't, it's just a reference of those for link.exe
. Do I need to pipe the cl
command to link.exe
, do I need to execute it after cl
? I cannot find an example either.
Just for testing, the simple program main.cpp
is the following:
#include <iostream>
#include <Windows.h>
#include <string>
int main()
{
DWORD pid;
HWND hwnd = FindWindow(0, L"Calculator");
GetWindowThreadProcessId(hwnd, &pid);
if (hwnd) {
std::cout << "Window is open, id = " << pid;
}
else {
std::cout << "Window not found" << '\n';
}
system("Pause");
}
It works properly inside the IDE. Now, as I mentioned, I copy the complete commands from MSVS 2017 compiler section:/JMC /permissive- /we"4239" /GS /Zc:rvalueCast /W3 /Zc:wchar_t /ZI /Gm- /Od /Fd"x64\Debug\vc141.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++17 /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\GetProcessByName.pch" /diagnostics:classic
and linker part: /OUT:"J:\nik\Documents\Visual_Studio_Projects\GetProcess\x64\Debug\GetProcessByName.exe" /MANIFEST /NXCOMPAT /PDB:"J:\nik\Documents\Visual_Studio_Projects\GetProcess\x64\Debug\GetProcessByName.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X64 /INCREMENTAL /PGD:"J:\nik\Documents\Visual_Studio_Projects\GetProcess\x64\Debug\GetProcessByName.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"x64\Debug\GetProcessByName.exe.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /TLBID:1
and as Mike said, I combine them as follows cl [compile-options] main.cpp /link [linker-options]
and execute this command from the directory where main.cpp
is located.
The output is:
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.1
5.26726\include\xlocale(319): warning C4530: C++ exception handler used, but unw
ind semantics are not enabled. Specify /EHsc
C:\Users\nik\documents\Visual_Studio_Projects\GetProcess\GetProcessByName\m
ain.cpp : fatal error C1083: Cannot open compiler generated file: 'x64\Debug" /E
Hsc /nologo /Fox64\Debug".asm': Invalid argument
How do I properly "pass"/specify linker options to build the program from the command line and run it?